Update Team: Use try/catch when displaying toast because the redirect from Next.js Server Action returns an error
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import { useTransition } from 'react';
|
import { useTransition } from 'react';
|
||||||
|
|
||||||
|
import { isRedirectError } from 'next/dist/client/components/redirect';
|
||||||
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
@@ -46,18 +48,36 @@ export const UpdateTeamAccountNameForm = (props: {
|
|||||||
data-test={'update-team-account-name-form'}
|
data-test={'update-team-account-name-form'}
|
||||||
className={'flex flex-col space-y-4'}
|
className={'flex flex-col space-y-4'}
|
||||||
onSubmit={form.handleSubmit((data) => {
|
onSubmit={form.handleSubmit((data) => {
|
||||||
startTransition(() => {
|
startTransition(async () => {
|
||||||
const promise = updateTeamAccountName({
|
const toastId = toast.loading(t('updateTeamLoadingMessage'));
|
||||||
slug: props.account.slug,
|
|
||||||
name: data.name,
|
|
||||||
path: props.path,
|
|
||||||
});
|
|
||||||
|
|
||||||
toast.promise(promise, {
|
try {
|
||||||
loading: t('updateTeamLoadingMessage'),
|
const result = await updateTeamAccountName({
|
||||||
success: t('updateTeamSuccessMessage'),
|
slug: props.account.slug,
|
||||||
error: t('updateTeamErrorMessage'),
|
name: data.name,
|
||||||
});
|
path: props.path,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
toast.success(t('updateTeamSuccessMessage'), {
|
||||||
|
id: toastId,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
toast.error(t('updateTeamErrorMessage'), {
|
||||||
|
id: toastId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (!isRedirectError(error)) {
|
||||||
|
toast.error(t('updateTeamErrorMessage'), {
|
||||||
|
id: toastId,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
toast.success(t('updateTeamSuccessMessage'), {
|
||||||
|
id: toastId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user