@@ -18,14 +18,15 @@ import { useCreatePet } from "@/hooks/mutation/usePostPet";
18
18
import { usePetsAdminQuery } from "@/hooks/queries/usePetsAdminQuery" ;
19
19
import AdminLayout from "@/layouts/AdminLayout" ;
20
20
import dayjs from "dayjs" ;
21
+ import { toast } from "react-hot-toast" ;
22
+
21
23
const adminCreate = ( ) => {
22
24
const { data, isLoading } = usePetsAdminQuery ( ) ;
23
25
24
26
const [ name , setName ] = useState ( "กรุณาใส่ชื่อ..." ) ;
25
27
const [ text , setText ] = useState ( "" ) ;
26
28
const [ thumbnail , setThumbnail ] = useState < File | null > ( null ) ;
27
- const [ origin , setOrigin ] = useState ( "fromClub" ) ;
28
- // origin : fromClub / fromOutside
29
+ const [ origin , setOrigin ] = useState ( "club" ) ;
29
30
const [ pictures , setPictures ] = useState < File [ ] > ( [ ] ) ;
30
31
const [ info , setInfo ] = useState < info > ( {
31
32
gender : "-" ,
@@ -54,45 +55,54 @@ const adminCreate = () => {
54
55
const postPetMutation = useCreatePet ( ) ;
55
56
56
57
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 [ ] = [ ] ;
60
75
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 {
76
88
type : info . type ,
77
89
name : name ,
78
90
birthdate : dayjs ( info . age ) . toISOString ( ) ,
79
91
gender : info . gender as "male" | "female" ,
80
92
color : info . color ,
81
- pattern : "a" , // remove later
93
+ pattern : "a" ,
82
94
habit : info . nature ,
83
95
caption : text ,
84
96
status : "findHome" ,
85
97
is_sterile : info . sterile ,
86
98
is_vaccinated : info . vaccine ,
87
99
is_visible : true ,
88
- origin : ` ${ origin === "fromClub" ? "club" : "entrust" } ` ,
89
- images : allImage ,
100
+ origin : origin ,
101
+ images : petImages ,
90
102
tel : info . tel ,
91
103
contact : info . contact ,
92
104
owner : info . owner ,
93
105
} ;
94
-
95
- postPetMutation . mutate ( petData ) ;
96
106
} ;
97
107
98
108
const onChangeThumbnail = ( event : React . ChangeEvent < HTMLInputElement > ) => {
0 commit comments