@@ -4,7 +4,7 @@ import Container from "@/components/Container";
44import SmallPetCardList from "@/components/SmallPetCardList" ;
55import { useCreateImage } from "@/hooks/mutation/usePostImage" ;
66import { Icon } from "@iconify/react" ;
7- import { useEffect , useState } from "react" ;
7+ import { useMemo , useState } from "react" ;
88import { Link } from "react-router-dom" ;
99import AddSmallPicture from "../../../../components/Admin/Pets/Add/AddSmallPicture" ;
1010import AddThumbnail from "../../../../components/Admin/Pets/Add/AddThumbnail" ;
@@ -40,27 +40,20 @@ const adminCreate = () => {
4040 contact : "-" ,
4141 } ) ;
4242
43- const [ enableSubmit , setEnableSubmit ] = useState ( false ) ;
44- useEffect ( ( ) => {
45- if (
43+ const enableSubmit = useMemo ( ( ) => {
44+ return ! (
4645 info . gender === "-" ||
4746 info . type === "-" ||
4847 info . color === "-" ||
4948 info . age === "-" ||
5049 name === "กรุณาใส่ชื่อ..."
51- ) {
52- setEnableSubmit ( false ) ;
53- } else {
54- setEnableSubmit ( true ) ;
55- }
50+ ) ;
5651 } , [ info . gender , info . type , info . color , info . age , name ] ) ;
5752
5853 const postImageMutation = useCreateImage ( ) ;
5954 const postPetMutation = useCreatePet ( ) ;
6055
6156 const handleSubmit = async ( ) => {
62- if ( info . gender === "-" ) return ; // already detect "-" by disable post button
63-
6457 const allImageFile : File [ ] = await Promise . all (
6558 thumbnail ? [ thumbnail , ...pictures ] : pictures
6659 ) ;
@@ -102,6 +95,23 @@ const adminCreate = () => {
10295 postPetMutation . mutate ( petData ) ;
10396 } ;
10497
98+ const onChangeThumbnail = ( event : React . ChangeEvent < HTMLInputElement > ) => {
99+ if ( event . target . files ) {
100+ setThumbnail ( event . target . files [ 0 ] ) ;
101+ }
102+ } ;
103+
104+ const onChangePictures = ( event : React . ChangeEvent < HTMLInputElement > ) => {
105+ if ( event . target . files ) {
106+ setPictures ( [ ...pictures , ...Array . from ( event . target . files ) ] ) ;
107+ }
108+ } ;
109+
110+ const onDeletePicture = ( index : number ) => {
111+ console . log ( index ) ;
112+ setPictures ( pictures . filter ( ( _ , i ) => i !== index ) ) ;
113+ } ;
114+
105115 return (
106116 < >
107117 { /* Icon return & small EditName */ }
@@ -118,9 +128,9 @@ const adminCreate = () => {
118128 { /* Thumpnail */ }
119129 < div className = "w-full max-w-80 self-center md:w-80" >
120130 < AddThumbnail
121- valueThumbnail = { thumbnail }
122- setThumbnail = { setThumbnail }
123- valueOrigin = { origin }
131+ thumbnail = { thumbnail ? URL . createObjectURL ( thumbnail ) : null }
132+ onChangeThumbnail = { onChangeThumbnail }
133+ origin = { origin }
124134 setOrigin = { setOrigin }
125135 />
126136 </ div >
@@ -136,7 +146,11 @@ const adminCreate = () => {
136146
137147 { /* Small Picture */ }
138148 < Container >
139- < AddSmallPicture value = { pictures } setValue = { setPictures } />
149+ < AddSmallPicture
150+ images = { pictures . map ( ( picture ) => URL . createObjectURL ( picture ) ) }
151+ onChange = { onChangePictures }
152+ onDelete = { onDeletePicture }
153+ />
140154 </ Container >
141155
142156 < div className = "mx-auto my-8 flex max-w-[1536px] gap-20 xl:justify-between xl:pr-24" >
0 commit comments