Skip to content

Commit

Permalink
fix(routes): address @conform-to breaking changes
Browse files Browse the repository at this point in the history
This patch removes the `stripEmptyValue` option as empty values are now
always stripped by Conform [[1]].

This patch also updates the custom error surfacing to pass in arrays
instead of error strings [[2]].

[1]: edmundhung/conform#227
[2]: edmundhung/conform#228

Ref: https://github.com/edmundhung/conform/releases/tag/v0.8.0
  • Loading branch information
nicholaschiang committed Sep 30, 2023
1 parent 1a7270f commit a8a4eae
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions app/routes/_header.$username.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function action({ request, params }: ActionArgs) {
if (user.id !== userId) throw new Response('Forbidden', { status: 403 })

const formData = await request.formData()
const submission = parse(formData, { schema, stripEmptyValue: true })
const submission = parse(formData, { schema })

if (!submission.value || submission.intent !== 'submit')
return json(submission, { status: 400 })
Expand All @@ -104,7 +104,7 @@ export async function action({ request, params }: ActionArgs) {

if (error) {
log.error('Error updating avatar for @%s: %s', params.username, error.stack)
submission.error.avatar = 'Failed to upload avatar; try again later'
submission.error.avatar = ['Failed to upload avatar; try again later']
return json(submission, { status: 500 })
}

Expand Down Expand Up @@ -232,7 +232,7 @@ function AvatarForm() {
const [form, { avatar }] = useForm({
lastSubmission,
onValidate({ formData }) {
return parse(formData, { schema, stripEmptyValue: true })
return parse(formData, { schema })
},
})
const user = useLoaderData<typeof loader>()
Expand Down
6 changes: 3 additions & 3 deletions app/routes/_header.join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ export async function action({ request }: ActionArgs) {

let existingUser = await getUserByName(submission.value.name)
if (existingUser) {
submission.error.name = 'A user already exists with this name'
submission.error.name = ['A user already exists with this name']
return json(submission, { status: 400 })
}

existingUser = await getUserByUsername(submission.value.username)
if (existingUser) {
submission.error.username = 'A user already exists with this username'
submission.error.username = ['A user already exists with this username']
return json(submission, { status: 400 })
}

existingUser = await getUserByEmail(submission.value.email)
if (existingUser) {
submission.error.email = 'A user already exists with this email'
submission.error.email = ['A user already exists with this email']
return json(submission, { status: 400 })
}

Expand Down
2 changes: 1 addition & 1 deletion app/routes/_header.login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function action({ request }: ActionArgs) {
)

if (!user) {
submission.error.emailOrUsername = 'Incorrect email or password'
submission.error.emailOrUsername = ['Incorrect email or password']
return json(submission, { status: 400 })
}

Expand Down
10 changes: 5 additions & 5 deletions app/routes/_layout.profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function action({ request }: ActionArgs) {
if (userId == null) return redirect('/login?redirectTo=/profile')

const formData = await request.formData()
const submission = parse(formData, { schema, stripEmptyValue: true })
const submission = parse(formData, { schema })

if (!submission.value || submission.intent !== 'submit')
return json(submission, { status: 400 })
Expand All @@ -77,23 +77,23 @@ export async function action({ request }: ActionArgs) {
where: { name: submission.value.name, id: { not: userId } },
})
if (existingUser) {
submission.error.name = 'A user already exists with this name'
submission.error.name = ['A user already exists with this name']
return json(submission, { status: 400 })
}

existingUser = await prisma.user.findFirst({
where: { username: submission.value.username, id: { not: userId } },
})
if (existingUser) {
submission.error.username = 'A user already exists with this username'
submission.error.username = ['A user already exists with this username']
return json(submission, { status: 400 })
}

existingUser = await prisma.user.findFirst({
where: { email: submission.value.email, id: { not: userId } },
})
if (existingUser) {
submission.error.email = 'A user already exists with this email'
submission.error.email = ['A user already exists with this email']
return json(submission, { status: 400 })
}

Expand All @@ -118,7 +118,7 @@ export default function ProfilePage() {
const [form, { name, username, description, email, password }] = useForm({
lastSubmission,
onValidate({ formData }) {
return parse(formData, { schema, stripEmptyValue: true })
return parse(formData, { schema })
},
})
const [state, setState] = useState(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ function VideoForm() {

const [form, { video }] = useForm({
onValidate({ formData }) {
return parse(formData, { schema, stripEmptyValue: true })
return parse(formData, { schema })
},
onSubmit(event, { formData }) {
event.preventDefault()
const submission = parse(formData, { schema, stripEmptyValue: true })
const submission = parse(formData, { schema })
const videoFile = submission.value?.video
setFile(videoFile)
if (videoFile) fetcher.submit({}, { action, method: 'POST' })
Expand Down

0 comments on commit a8a4eae

Please sign in to comment.