@@ -27,7 +27,7 @@ import {
2727 getFormValuesFromFieldOrLabels ,
2828} from '@/core/helper' ;
2929import {
30- usePydanticForm ,
30+ useApiProvider ,
3131 usePydanticFormParser ,
3232 useRefParser ,
3333} from '@/core/hooks' ;
@@ -78,7 +78,7 @@ function PydanticFormContextProvider({
7878 const {
7979 customDataProvider,
8080 labelProvider,
81- formProvider ,
81+ apiProvider ,
8282 fieldDetailProvider,
8383 onFieldChangeHandler,
8484 customDataProviderCacheKey,
@@ -93,13 +93,21 @@ function PydanticFormContextProvider({
9393 cancelButton,
9494 } = config ;
9595
96+ // TODO: Fix this again
9697 // option to enable the debug mode on the fly in the browser
9798 // by setting localStorage.setItem("pydanticFormsDebugMode", "true")
9899 // reload is required
99100 const debugMode = false ;
100101
101102 // eslint-disable-next-line @typescript-eslint/no-explicit-any
102- const [ formInputData , setFormInputData ] = useState < any > ( [ ] ) ;
103+ const [ formInputData , setFormInputData ] = useState < object [ ] > ( [ ] ) ;
104+
105+ const addFormInputData = ( formInput : object ) => {
106+ setFormInputData ( ( currentInputs ) => {
107+ return [ ...currentInputs , formInput ] ;
108+ } ) ;
109+ } ;
110+
103111 const [ errorDetails , setErrorDetails ] =
104112 useState < PydanticFormValidationErrorDetails > ( ) ;
105113 const [ isFullFilled , setIsFullFilled ] = useState ( false ) ;
@@ -111,18 +119,18 @@ function PydanticFormContextProvider({
111119 const { data : formLabels , isLoading : isLoadingFormLabels } =
112120 useLabelProvider ( labelProvider , formKey , formIdKey ) ;
113121
114- const { data : customData , isLoading : isCustomDataLoading } =
122+ const { data : customData , isLoading : isLoadingCustomData } =
115123 useCustomDataProvider (
116124 customDataProviderCacheKey ?? 100 ,
117125 customDataProvider ,
118126 ) ;
119127
120128 // fetch the form definition using SWR hook
121129 const {
122- data : apiErrorResp ,
130+ data : apiResponse ,
123131 isLoading : isLoadingSchema ,
124132 error,
125- } = usePydanticForm ( formKey , formInputData , formProvider , metaData ) ;
133+ } = useApiProvider ( formKey , formInputData , apiProvider , metaData ) ;
126134
127135 // we cache the form scheme so when there is an error, we still have the form
128136 // the form is not in the error response
@@ -170,14 +178,13 @@ function PydanticFormContextProvider({
170178
171179 rhfRef . current = rhf ;
172180
173- /* TODO: implement this
181+ /* TODO: Reimplement
174182 // prevent user from navigating away when there are unsaved changes
175183 const hasUnsavedData =
176184 !saveToLeavePageInCurrentState &&
177185 !isFullFilled &&
178186 rhf.formState.isDirty;
179187
180- /* TODO: implement this
181188 useLeavePageConfirm(
182189 hasUnsavedData,
183190 'Er zijn aanpassingen in het formulier. \nWeet je zeker dat je de pagina wilt verlaten?',
@@ -226,36 +233,43 @@ function PydanticFormContextProvider({
226233 } ) ;
227234
228235 if ( skipSuccessNotice ) {
229- onSuccess ( values , summaryData ) ;
236+ onSuccess ( values , summaryData , apiResponse || { } ) ;
230237 } else {
231238 setTimeout ( ( ) => {
232- onSuccess ?.( values , summaryData ) ;
233- } , 1500 ) ;
239+ onSuccess ?.( values , summaryData , apiResponse || { } ) ;
240+ } , 1500 ) ; // Delay to allow notice to show first
234241 }
235- } , [ isFullFilled , skipSuccessNotice , onSuccess , rhf , formData ] ) ;
242+ } , [
243+ isFullFilled ,
244+ skipSuccessNotice ,
245+ onSuccess ,
246+ rhf ,
247+ formData ,
248+ apiResponse ,
249+ ] ) ;
236250
237251 // a useeffect for whenever the error response updates
238252 // sometimes we need to update the form,
239253 // some we need to update the errors
240254 useEffect ( ( ) => {
241- if ( apiErrorResp ?. success ) {
255+ if ( apiResponse ?. success ) {
242256 setIsFullFilled ( true ) ;
243257 return ;
244258 }
245259
246260 // when we receive a form from the JSON, we fully reset the scheme
247- if ( apiErrorResp ?. form ) {
248- setRawSchema ( apiErrorResp . form ) ;
261+ if ( apiResponse ?. form ) {
262+ setRawSchema ( apiResponse . form ) ;
249263 setErrorDetails ( undefined ) ;
250264 }
251265
252266 // when we receive errors, we append to the scheme
253- if ( apiErrorResp ?. validation_errors ) {
254- setErrorDetails ( getErrorDetailsFromResponse ( apiErrorResp ) ) ;
267+ if ( apiResponse ?. validation_errors ) {
268+ setErrorDetails ( getErrorDetailsFromResponse ( apiResponse ) ) ;
255269 }
256270
257271 setIsSending ( false ) ;
258- } , [ apiErrorResp , onSuccess , rhf , skipSuccessNotice ] ) ;
272+ } , [ apiResponse , onSuccess , rhf , skipSuccessNotice ] ) ;
259273
260274 const resetFormData = useCallback ( ( ) => {
261275 if ( ! formData ) {
@@ -292,7 +306,7 @@ function PydanticFormContextProvider({
292306
293307 const submitFormFn = useCallback ( ( ) => {
294308 setIsSending ( true ) ;
295- setFormInputData ( [ rhf ?. getValues ( ) ] ) ;
309+ addFormInputData ( rhf ?. getValues ( ) ) ;
296310
297311 window . scrollTo ( 0 , 0 ) ;
298312 } , [ rhf ] ) ;
@@ -320,7 +334,7 @@ function PydanticFormContextProvider({
320334 [ resetFormData , rhf ] ,
321335 ) ;
322336
323- // with this we have the possiblity to have listeners for specific fields
337+ // with this we have the possibility to have listeners for specific fields
324338 // this could be used to trigger validations of related fields, casting changes to elsewhere, etc.
325339 useEffect ( ( ) => {
326340 let sub : Subscription ;
@@ -349,7 +363,7 @@ function PydanticFormContextProvider({
349363 isLoadingFormLabels ||
350364 isLoadingSchema ||
351365 isLoadingSchema ||
352- ( customDataProvider ? isCustomDataLoading : false ) ;
366+ ( customDataProvider ? isLoadingCustomData : false ) ;
353367
354368 const PydanticFormContextState = {
355369 // to prevent an issue where the sending state hangs
0 commit comments