Skip to content

Commit c3afad9

Browse files
authoredJan 16, 2025··
Merge pull request #106 from isd-sgcu/fix/wrong-order-and-badge
fix(admin): save image order and badge
2 parents c6c0236 + bc5442d commit c3afad9

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed
 

‎src/app/admin/pets/add/page.tsx

+35-25
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import { useCreatePet } from "@/hooks/mutation/usePostPet";
1818
import { usePetsAdminQuery } from "@/hooks/queries/usePetsAdminQuery";
1919
import AdminLayout from "@/layouts/AdminLayout";
2020
import dayjs from "dayjs";
21+
import { toast } from "react-hot-toast";
22+
2123
const adminCreate = () => {
2224
const { data, isLoading } = usePetsAdminQuery();
2325

2426
const [name, setName] = useState("กรุณาใส่ชื่อ...");
2527
const [text, setText] = useState("");
2628
const [thumbnail, setThumbnail] = useState<File | null>(null);
27-
const [origin, setOrigin] = useState("fromClub");
28-
// origin : fromClub / fromOutside
29+
const [origin, setOrigin] = useState("club");
2930
const [pictures, setPictures] = useState<File[]>([]);
3031
const [info, setInfo] = useState<info>({
3132
gender: "-",
@@ -54,45 +55,54 @@ const adminCreate = () => {
5455
const postPetMutation = useCreatePet();
5556

5657
const handleSubmit = async () => {
57-
const allImageFile: File[] = await Promise.all(
58-
thumbnail ? [thumbnail, ...pictures] : pictures
59-
);
58+
const toastLoadingId = toast.loading("กำลังสร้างสัตว์เลี้ยง...");
59+
try {
60+
const petImages = await uploadPetImages();
61+
const petData = createPetData(petImages);
62+
postPetMutation.mutate(petData);
63+
toast.dismiss(toastLoadingId);
64+
} catch (error) {
65+
console.error("Failed to create pet:", error);
66+
toast.error("Failed to create pet");
67+
toast.dismiss(toastLoadingId);
68+
}
69+
};
70+
71+
const uploadPetImages = async (): Promise<string[]> => {
72+
const allImageFiles = thumbnail ? [thumbnail, ...pictures] : pictures;
73+
74+
const uploadedImages: string[] = [];
6075

61-
// post image and get id : assume this is correct
62-
const allImage: string[] = (
63-
await Promise.all(
64-
allImageFile.map(async (image) => {
65-
const imageResponse = await postImageMutation.mutateAsync({
66-
file: image,
67-
});
68-
return imageResponse.id;
69-
})
70-
)
71-
)
72-
.filter((id) => id !== undefined)
73-
.map((id) => id ?? ""); // map for bypass ts type checking
74-
75-
const petData: postPetRequest = {
76+
for (const image of allImageFiles) {
77+
const response = await postImageMutation.mutateAsync({ file: image });
78+
if (response.id) {
79+
uploadedImages.push(response.id);
80+
}
81+
}
82+
83+
return uploadedImages;
84+
};
85+
86+
const createPetData = (petImages: string[]): postPetRequest => {
87+
return {
7688
type: info.type,
7789
name: name,
7890
birthdate: dayjs(info.age).toISOString(),
7991
gender: info.gender as "male" | "female",
8092
color: info.color,
81-
pattern: "a", // remove later
93+
pattern: "a",
8294
habit: info.nature,
8395
caption: text,
8496
status: "findHome",
8597
is_sterile: info.sterile,
8698
is_vaccinated: info.vaccine,
8799
is_visible: true,
88-
origin: `${origin === "fromClub" ? "club" : "entrust"}`,
89-
images: allImage,
100+
origin: origin,
101+
images: petImages,
90102
tel: info.tel,
91103
contact: info.contact,
92104
owner: info.owner,
93105
};
94-
95-
postPetMutation.mutate(petData);
96106
};
97107

98108
const onChangeThumbnail = (event: React.ChangeEvent<HTMLInputElement>) => {

0 commit comments

Comments
 (0)
Please sign in to comment.