Add proper toast notifications to edit/create beer post. fix delete

This commit is contained in:
Aaron William Po
2023-12-01 14:34:26 -05:00
parent 293200fbe2
commit 49d5b782a9
3 changed files with 13 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ import sendUploadBeerImagesRequest from '@/requests/BeerImage/sendUploadBeerImag
import toast from 'react-hot-toast';
import createErrorToast from '@/util/createErrorToast';
import Button from './ui/forms/Button';
import FormError from './ui/forms/FormError';
import FormInfo from './ui/forms/FormInfo';
@@ -51,14 +52,14 @@ const CreateBeerPostForm: FunctionComponent<BeerFormProps> = ({
}
try {
const loadingToast = toast.loading('Creating beer post...');
const beerPost = await sendCreateBeerPostRequest(data);
await sendUploadBeerImagesRequest({ beerPost, images: data.images });
await router.push(`/beers/${beerPost.id}`);
toast.dismiss(loadingToast);
toast.success('Created beer post.');
} catch (e) {
const errorMessage = e instanceof Error ? e.message : 'Something went wrong.';
toast.error(errorMessage);
createErrorToast(e);
}
};

View File

@@ -9,6 +9,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
import deleteBeerPostRequest from '@/requests/BeerPost/deleteBeerPostRequest';
import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPostValidationSchema';
import sendEditBeerPostRequest from '@/requests/BeerPost/sendEditBeerPostRequest';
import createErrorToast from '@/util/createErrorToast';
import Button from './ui/forms/Button';
import FormError from './ui/forms/FormError';
import FormInfo from './ui/forms/FormInfo';
@@ -33,23 +34,26 @@ const EditBeerPostForm: FC<EditBeerPostFormProps> = ({ previousValues }) => {
const { isSubmitting, errors } = formState;
const onSubmit: SubmitHandler<EditBeerPostSchema> = async (data) => {
try {
const loadingToast = toast.loading('Editing beer post...');
await sendEditBeerPostRequest(data);
await router.push(`/beers/${data.id}`);
toast.success('Edited beer post.');
toast.dismiss(loadingToast);
} catch (e) {
const errorMessage = e instanceof Error ? e.message : 'Something went wrong.';
toast.error(errorMessage);
createErrorToast(e);
await router.push(`/beers/${data.id}`);
}
};
const onDelete = async () => {
try {
const loadingToast = toast.loading('Deleting beer post...');
await deleteBeerPostRequest(previousValues.id);
toast.dismiss(loadingToast);
await router.push('/beers');
toast.success('Deleted beer post.');
} catch (e) {
const errorMessage = e instanceof Error ? e.message : 'Something went wrong.';
toast.error(errorMessage);
createErrorToast(e);
await router.push(`/beers`);
}
};

View File

@@ -57,7 +57,7 @@ const editBeerPost = async (
const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
const { id } = req.query;
const deleted = deleteBeerPostById({ beerPostId: id });
const deleted = await deleteBeerPostById({ beerPostId: id });
if (!deleted) {
throw new ServerError('Beer post not found', 404);
}