Skip to content

Commit

Permalink
Merge pull request #209 from usdevs/fix/user-control-form-toast
Browse files Browse the repository at this point in the history
fix: handle errors in user control form
  • Loading branch information
zsh-eng authored Aug 12, 2024
2 parents 884a63b + f12089c commit 2f27ddd
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions components/admin/users/UserControlForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,35 @@ function UserControlForm({ users, mutateUsers, mutateOrgs }: UserControlFormProp
fetchUrl = process.env.NEXT_PUBLIC_BACKEND_URL + `user/${values.id}`
method = 'PUT'
}
const { responseJson, responseStatus } = await makeFetchToUrlWithAuth(
fetchUrl,
auth.token,
method,
JSON.stringify(values),
)

if (responseStatus === 200) {
toast(
makeSuccessToast(
values.id === -1 ? `User created successfully!` : `User edited successfully`,
),
)
mutateOrgs()
mutateUsers()
onClose()
} else {
toast(
makeErrorToast(
'Oh snap! There was an error when making the user',
JSON.stringify(responseJson.message),
),
try {
const { responseJson, responseStatus } = await makeFetchToUrlWithAuth(
fetchUrl,
auth.token,
method,
JSON.stringify(values),
)

if (responseStatus === 200) {
toast(
makeSuccessToast(
values.id === -1 ? `User created successfully!` : `User edited successfully`,
),
)
mutateOrgs()
mutateUsers()
onClose()
} else {
toast(
makeErrorToast(
'Oh snap! There was an error when making the user',
JSON.stringify(responseJson.message),
),
)
}
} catch (err) {
console.error(err)
toast(makeErrorToast('Oh snap! There was an error when making the user', (err as Error).message))
}

setSubmitting(false)
Expand Down

0 comments on commit 2f27ddd

Please sign in to comment.