@@ -2,13 +2,11 @@ import React, { useEffect, useState } from 'react';
22import { Form } from 'react-final-form' ;
33import useFetchForms from '../hooks/useFetchForms' ;
44import FetchFormItem from './form/FetchFormItem' ;
5- import { validate } from './form /Validations' ;
5+ import { validate } from '../scripts /Validations' ;
66import '../styles/index.scss' ;
7- import useCloudSave from '../hooks/useCloudSave' ;
87
98const FetchForm = ( { slug, showFormError, onSubmit } ) => {
10- const [ fetchForm , loading , error ] = useFetchForms ( slug ) ;
11- const { status, createSubmission } = useCloudSave ( ) ;
9+ const [ fetchForm , loading , error , doCloudSubmit ] = useFetchForms ( slug ) ;
1210 const [ validations , setValidations ] = useState ( { } ) ;
1311 const [ submitError , setSubmitError ] = useState ( false ) ;
1412
@@ -43,17 +41,23 @@ const FetchForm = ({ slug, showFormError, onSubmit }) => {
4341 formattedValues [ keys [ i ] ] = values [ keys [ i ] ] ;
4442 }
4543 }
44+ console . log ( formattedValues ) ;
4645
4746 if ( fetchForm . cloudSave ) {
48- await createSubmission ( fetchForm . id , formattedValues ) ;
49- if ( status . error ) {
50- return setSubmitError ( status . error ) ;
51- }
52- }
47+ try {
48+ const isSaved = await doCloudSubmit ( fetchForm . id , formattedValues ) ;
49+ if ( ! isSaved . success ) {
50+ throw isSaved . message ;
51+ }
5352
54- const hasError = await onSubmit ( formattedValues ) ;
55- if ( hasError ) {
56- setSubmitError ( hasError ) ;
53+ const hasError = await onSubmit ( formattedValues ) ;
54+ if ( hasError ) {
55+ throw hasError ;
56+ }
57+ } catch ( err ) {
58+ console . log ( err ) ;
59+ setSubmitError ( err ) ;
60+ }
5761 }
5862 } ;
5963
@@ -75,7 +79,6 @@ const FetchForm = ({ slug, showFormError, onSubmit }) => {
7579 initialValues = { { } }
7680 validate = { ( values ) => {
7781 const errors = validate ( values , validations ) ;
78- // console.log(errors);
7982 return errors ;
8083 } }
8184 render = { ( { handleSubmit, submitting, invalid } ) => (
@@ -92,7 +95,7 @@ const FetchForm = ({ slug, showFormError, onSubmit }) => {
9295 className = 'fetch-submit-button'
9396 disabled = { submitting || invalid }
9497 >
95- { fetchForm . submitText }
98+ { submitting ? 'Saving...' : fetchForm . submitText }
9699 </ button >
97100 </ form >
98101 ) }
0 commit comments