From 64136a5a5ffbd43ca389064cbf3cbcdd506beede Mon Sep 17 00:00:00 2001 From: teegoood Date: Mon, 22 Jul 2024 21:46:27 +0700 Subject: [PATCH] add toast loading --- .../Admin/Pets/Add/AddSmallPicture.tsx | 44 ++-- .../Admin/Pets/Add/AddThumbnail.tsx | 36 ++- src/components/Pets/Details/Details.tsx | 243 +++++++++--------- src/hooks/mutation/useDeleteImage.ts | 4 - src/hooks/mutation/usePostImage.ts | 4 - src/hooks/mutation/useUpdatePet.ts | 5 - 6 files changed, 155 insertions(+), 181 deletions(-) diff --git a/src/components/Admin/Pets/Add/AddSmallPicture.tsx b/src/components/Admin/Pets/Add/AddSmallPicture.tsx index 5c7d58ab..96276b6c 100644 --- a/src/components/Admin/Pets/Add/AddSmallPicture.tsx +++ b/src/components/Admin/Pets/Add/AddSmallPicture.tsx @@ -1,47 +1,33 @@ import { Icon } from "@iconify/react"; interface AddSmallPictureProps { - value: File[]; - setValue: React.Dispatch>; + images: string[]; + onChange: (event: React.ChangeEvent) => void; + onDelete: (index: number) => void; } -const AddSmallPicture = (props: AddSmallPictureProps) => { - const { value, setValue } = props; - const handleOnChange = (event: React.ChangeEvent) => { - const selectedFiles = event.target.files; - if (selectedFiles && selectedFiles.length !== 0) { - const newFiles: File[] = Array.from(selectedFiles); - setValue((prev) => { - return [...prev, ...Array.from(newFiles)]; - }); - } - }; - - const handleDeleteImage = (index: number) => { - setValue((prev) => { - const newFiles = [...prev]; - newFiles.splice(index, 1); - return newFiles; - }); - }; - +const AddSmallPicture = ({ + images, + onChange, + onDelete, +}: AddSmallPictureProps) => { return (
- {Array.from(value).map((picture, index) => { - if (!picture) return; + {images.map((image, index) => { + if (!image) return; return (
{picture.name}
- +
@@ -197,14 +205,17 @@ const Details = (props: DetailsProps) => {
- {!props.isAdmin ? ( - + {!isAdmin ? ( + img.url)} + origin={origin} + /> ) : ( )}
@@ -213,22 +224,18 @@ const Details = (props: DetailsProps) => {
- +
- +
- {props.isAdmin && ( - + {isAdmin && ( + )} @@ -238,7 +245,7 @@ const Details = (props: DetailsProps) => { value={petInfo} setValue={setPetInfo} onSubmit={handleSubmit} - isAdmin={props.isAdmin} + isAdmin={isAdmin} isFav={isFav} handleFavPressed={handleFavPressed} id={id} diff --git a/src/hooks/mutation/useDeleteImage.ts b/src/hooks/mutation/useDeleteImage.ts index fc83c825..b640cad1 100644 --- a/src/hooks/mutation/useDeleteImage.ts +++ b/src/hooks/mutation/useDeleteImage.ts @@ -1,6 +1,5 @@ import { deleteImage } from "@/api/images"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import toast from "react-hot-toast"; const useDeleteImage = () => { const queryClient = useQueryClient(); @@ -10,9 +9,6 @@ const useDeleteImage = () => { onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["pet"] }); }, - onError: () => { - toast.error("มีบางอย่างผิดพลาด"); - }, }); }; diff --git a/src/hooks/mutation/usePostImage.ts b/src/hooks/mutation/usePostImage.ts index 354e4b85..6a862fc4 100644 --- a/src/hooks/mutation/usePostImage.ts +++ b/src/hooks/mutation/usePostImage.ts @@ -1,6 +1,5 @@ import { postImage, postImageRequest } from "@/api/images"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import toast from "react-hot-toast"; const useCreateImage = () => { const queryClient = useQueryClient(); @@ -10,9 +9,6 @@ const useCreateImage = () => { onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["pet"] }); }, - onError() { - toast.error("มีบางอย่างผิดพลาด"); - }, }); }; diff --git a/src/hooks/mutation/useUpdatePet.ts b/src/hooks/mutation/useUpdatePet.ts index 771ac957..d1dfbcb2 100644 --- a/src/hooks/mutation/useUpdatePet.ts +++ b/src/hooks/mutation/useUpdatePet.ts @@ -1,6 +1,5 @@ import { PutPetRequest, updatePet } from "@/api/pets"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import toast from "react-hot-toast"; interface UpdateMutationParameter { body: PutPetRequest; @@ -15,12 +14,8 @@ function useUpdatePet() { return updatePet(data.body, data.id); }, onSuccess: () => { - toast.success("แก้ไขข้อมูลสัตว์เลี้ยงสำเร็จ"); queryClient.invalidateQueries({ queryKey: ["pets"] }); }, - onError: () => { - toast.error("มีบางอย่างผิดพลาด"); - }, }); }