@@ -33,6 +33,7 @@ const AddNewProduct = () => {
3333 const [ merchants , setMerchants ] = useState < Merchant [ ] > ( [ ] ) ;
3434 const addProduct = async ( ) => {
3535 if (
36+ ! product . merchantId ||
3637 product . title === "" ||
3738 product . manufacturer === "" ||
3839 product . description == "" ||
@@ -59,6 +60,7 @@ const AddNewProduct = () => {
5960 . then ( ( data ) => {
6061 toast . success ( "Product added successfully" ) ;
6162 setProduct ( {
63+ merchantId : "" ,
6264 title : "" ,
6365 price : 0 ,
6466 manufacturer : "" ,
@@ -74,6 +76,20 @@ const AddNewProduct = () => {
7476 } ) ;
7577 } ;
7678
79+ const fetchMerchants = async ( ) => {
80+ try {
81+ const res = await apiClient . get ( "/api/merchants" ) ;
82+ const data : Merchant [ ] = await res . json ( ) ;
83+ setMerchants ( data || [ ] ) ;
84+ setProduct ( ( prev ) => ( {
85+ ...prev ,
86+ merchantId : prev . merchantId || data ?. [ 0 ] ?. id || "" ,
87+ } ) ) ;
88+ } catch ( e ) {
89+ toast . error ( "Failed to load merchants" ) ;
90+ }
91+ } ;
92+
7793 const uploadFile = async ( file : any ) => {
7894 const formData = new FormData ( ) ;
7995 formData . append ( "uploadedFile" , file ) ;
@@ -102,6 +118,7 @@ const AddNewProduct = () => {
102118 . then ( ( data ) => {
103119 setCategories ( data ) ;
104120 setProduct ( {
121+ merchantId : product . merchantId || "" ,
105122 title : "" ,
106123 price : 0 ,
107124 manufacturer : "" ,
@@ -116,6 +133,7 @@ const AddNewProduct = () => {
116133
117134 useEffect ( ( ) => {
118135 fetchCategories ( ) ;
136+ fetchMerchants ( ) ;
119137 } , [ ] ) ;
120138
121139 return (
@@ -128,14 +146,24 @@ const AddNewProduct = () => {
128146 < div className = "label" >
129147 < span className = "label-text" > Merchant Info:</ span >
130148 </ div >
131- < input
132- type = "text"
133- className = "input input-bordered w-full max-w-xs"
134- value = { product ?. title }
149+ < select
150+ className = "select select-bordered"
151+ value = { product ?. merchantId }
135152 onChange = { ( e ) =>
136- setProduct ( { ...product , title : e . target . value } )
153+ setProduct ( { ...product , merchantId : e . target . value } )
137154 }
138- />
155+ >
156+ { merchants . map ( ( merchant ) => (
157+ < option key = { merchant . id } value = { merchant . id } >
158+ { merchant . name }
159+ </ option >
160+ ) ) }
161+ </ select >
162+ { merchants . length === 0 && (
163+ < span className = "text-xs text-red-500 mt-1" >
164+ Please create a merchant first.
165+ </ span >
166+ ) }
139167 </ label >
140168 </ div >
141169
0 commit comments