@@ -2,6 +2,7 @@ import { FormButtons } from '@components/admin/FormButtons.js';
22import Area from '@components/common/Area.js' ;
33import { Form } from '@components/common/form/Form.js' ;
44import React from 'react' ;
5+ import { SubmitHandler , useForm } from 'react-hook-form' ;
56import { toast } from 'react-toastify' ;
67
78export default function ProductNewForm ( {
@@ -11,22 +12,46 @@ export default function ProductNewForm({
1112 action : string ;
1213 gridUrl : string ;
1314} ) {
15+ const form = useForm ( {
16+ shouldUnregister : true
17+ } ) ;
18+ const submit : SubmitHandler < any > = async ( data ) => {
19+ try {
20+ const images = ( data . images || [ ] ) . map (
21+ ( image : { uuid : string ; url : string } ) => image . url
22+ ) ;
23+ data . images = images ;
24+ const response = await fetch ( action , {
25+ method : 'POST' ,
26+ headers : {
27+ 'Content-Type' : 'application/json'
28+ } ,
29+ body : JSON . stringify ( { ...data , action : undefined , method : undefined } )
30+ } ) ;
31+ const result = await response . json ( ) ;
32+ if ( result . error ) {
33+ toast . error ( result . error . message ) ;
34+ } else {
35+ toast . success ( 'Product created successfully' ) ;
36+ const editUrl = result . data . links . find (
37+ ( link ) => link . rel === 'edit'
38+ ) . href ;
39+ setTimeout ( ( ) => {
40+ window . location . href = editUrl ;
41+ } , 1500 ) ;
42+ }
43+ } catch ( error ) {
44+ toast . error ( error . message ) ;
45+ }
46+ } ;
1447 return (
1548 < Form
1649 id = "productNewForm"
1750 method = "POST"
1851 action = { action }
52+ form = { form }
53+ onSubmit = { submit }
1954 submitBtn = { false }
20- onSuccess = { ( response ) => {
21- toast . success ( 'Product created successfully!' ) ;
22- const editUrl = response . data . links . find (
23- ( link ) => link . rel === 'edit'
24- ) . href ;
25- window . location . href = editUrl ;
26- setTimeout ( ( ) => {
27- window . location . href = gridUrl ;
28- } , 1500 ) ;
29- } }
3055 >
3156 < div className = "grid grid-cols-3 gap-x-5 grid-flow-row " >
3257 < div className = "col-span-2 grid grid-cols-1 gap-5 auto-rows-max" >
0 commit comments