@@ -57,31 +57,35 @@ export default function CreateJob() {
5757 useConfirmExit ( isDirty , ! isSubmitted && ! isSubmitting ) ;
5858
5959 const submitJob = async ( job : JobDetails ) => {
60- fetch ( `${ import . meta. env . VITE_BACKEND_URL } /api/jobs` , {
61- method : "POST" ,
62- credentials : "include" ,
63- headers : { "Content-Type" : "application/json" } ,
64- body : JSON . stringify ( job ) ,
65- } )
66- . then ( async ( res ) => {
67- const data = await res . json ( ) ;
68- if ( ! res . ok ) throw new Error ( data . message || "Something went wrong, please try again." ) ;
69- return data ;
70- } )
71- . then ( ( data ) => {
72- setMessage ( { type : "success" , text : data . message } ) ;
73- dispatch ( addJob ( data . job ) ) ;
74- navigate ( "/jobs" ) ;
75- } )
76- . catch ( ( err ) => {
77- setMessage ( { type : "error" , text : err . message } ) ;
78- } )
79- . finally ( ( ) => {
80- setTimeout ( ( ) => setMessage ( null ) , 8000 ) ;
60+ try {
61+ const res = await fetch ( `${ import . meta. env . VITE_BACKEND_URL } /api/jobs` , {
62+ method : "POST" ,
63+ credentials : "include" ,
64+ headers : { "Content-Type" : "application/json" } ,
65+ body : JSON . stringify ( job ) ,
8166 } ) ;
67+
68+ const data = await res . json ( ) ;
69+
70+ if ( ! res . ok ) {
71+ throw new Error ( data . message || "Something went wrong." ) ;
72+ }
73+
74+ setMessage ( { type : "success" , text : data . message } ) ;
75+ dispatch ( addJob ( data . job ) ) ;
76+ navigate ( "/jobs" ) ;
77+ } catch ( err ) {
78+ if ( err instanceof Error ) {
79+ setMessage ( { type : "error" , text : err . message } ) ;
80+ } else {
81+ setMessage ( { type : "error" , text : "Something went wrong, Please try again later." } ) ;
82+ }
83+ } finally {
84+ setTimeout ( ( ) => setMessage ( null ) , 8000 ) ;
85+ }
8286 } ;
8387
84- const onSubmit = ( job : JobDetails ) => {
88+ const onSubmit = async ( job : JobDetails ) => {
8589 const allowBody = [ "POST" , "PUT" , "PATCH" ] . includes ( job . method ?. toUpperCase ( ) ) ;
8690 if ( allowBody && job . body . trim ( ) ) {
8791 try {
@@ -100,7 +104,7 @@ export default function CreateJob() {
100104 }
101105 }
102106
103- submitJob ( job ) ;
107+ await submitJob ( job ) ;
104108 } ;
105109
106110 return (
0 commit comments