Skip to content

Commit faf543b

Browse files
DutchBenRuben van Leeuwen
andauthored
Fix double onSuccess calls (#196)
* Adds formId to use instead of cacheKey * Aligns packagelock with node 22.20 * Adds changeset --------- Co-authored-by: Ruben van Leeuwen <ruben.vanleeuwen@surf.nl>
1 parent e507161 commit faf543b

File tree

9 files changed

+55
-60
lines changed

9 files changed

+55
-60
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pydantic-forms': patch
3+
---
4+
5+
Fixes triggering onSuccess twice

frontend/apps/example/src/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export default function Home() {
2525
requestBody,
2626
}) => {
2727
const url = 'http://localhost:8000/form';
28-
2928
return fetch(url, {
3029
method: 'POST',
3130
body: JSON.stringify(requestBody),
@@ -136,6 +135,7 @@ export default function Home() {
136135

137136
<PydanticForm
138137
formKey="theForm"
138+
formId="example123"
139139
title="Example form"
140140
onCancel={() => {
141141
alert('Form cancelled');

frontend/package-lock.json

Lines changed: 19 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/packages/pydantic-forms/src/PydanticForm.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const PydanticFormValidationErrorContext =
3030
export const PydanticForm = ({
3131
config,
3232
formKey,
33+
formId,
3334
onCancel,
3435
onSuccess,
3536
title,
@@ -59,6 +60,7 @@ export const PydanticForm = ({
5960
onSuccess={onSuccess}
6061
title={title}
6162
formKey={formKey}
63+
formId={formId}
6264
/>
6365
</PydanticFormFieldDataStorageProvider>
6466
</PydanticFormConfigContext.Provider>

frontend/packages/pydantic-forms/src/core/PydanticFormHandler.tsx

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
import React, { useCallback, useEffect, useRef, useState } from 'react';
22
import type { FieldValues } from 'react-hook-form';
33

4-
import _ from 'lodash';
5-
64
import { PydanticFormValidationErrorContext } from '@/PydanticForm';
75
import { useGetConfig, usePydanticForm } from '@/core/hooks';
8-
import { PydanticFormHandlerProps, PydanticFormSuccessResponse } from '@/types';
6+
import { PydanticFormHandlerProps } from '@/types';
97
import { getHashForArray } from '@/utils';
108

119
import { ReactHookForm } from './ReactHookForm';
1210

1311
export const PydanticFormHandler = ({
1412
formKey,
13+
formId,
1514
onCancel,
1615
onSuccess,
1716
title,
1817
}: PydanticFormHandlerProps) => {
1918
const config = useGetConfig();
19+
const getComposedFormKey = useCallback(() => {
20+
return `${formKey}${formId}`;
21+
}, [formKey, formId]);
22+
2023
const [formStep, setStep] = useState<FieldValues>();
2124
const formStepsRef = useRef<FieldValues[]>([]);
2225
const [initialValues, setInitialValues] = useState<FieldValues>();
23-
const [currentFormKey, setCurrentFormKey] = useState<string>(formKey);
24-
const [cacheKey, setCacheKey] = useState<string>(_.uniqueId(formKey));
26+
const [currentFormId, setCurrentFormId] = useState<string>(
27+
getComposedFormKey(),
28+
);
2529
const formInputHistoryRef = useRef<Map<string, FieldValues>>(
2630
new Map<string, object>(),
2731
);
2832

2933
useEffect(() => {
30-
if (formKey && formKey !== currentFormKey) {
34+
if (formKey && getComposedFormKey() !== currentFormId) {
3135
formStepsRef.current = [];
3236
formInputHistoryRef.current = new Map<string, object>();
33-
setCurrentFormKey(formKey);
37+
setCurrentFormId(getComposedFormKey);
3438
setStep(undefined);
3539
setInitialValues(undefined);
3640
}
37-
}, [formKey, currentFormKey]);
41+
}, [formKey, formId, currentFormId, getComposedFormKey]);
3842

3943
const storeHistory = useCallback(async (stepData: FieldValues) => {
4044
const hashOfSteps = await getHashForArray(formStepsRef.current);
@@ -50,16 +54,6 @@ export const PydanticFormHandler = ({
5054
}
5155
}, []);
5256

53-
const handleSuccess = (
54-
fieldValues: FieldValues[],
55-
response: PydanticFormSuccessResponse,
56-
) => {
57-
if (onSuccess) {
58-
onSuccess(fieldValues, response);
59-
}
60-
setCacheKey(_.uniqueId(currentFormKey));
61-
};
62-
6357
const {
6458
validationErrorsDetails,
6559
apiError,
@@ -70,10 +64,10 @@ export const PydanticFormHandler = ({
7064
defaultValues,
7165
} = usePydanticForm(
7266
formKey,
67+
formId,
7368
config,
7469
formStepsRef,
75-
cacheKey,
76-
handleSuccess,
70+
onSuccess,
7771
formStep,
7872
);
7973

@@ -94,11 +88,10 @@ export const PydanticFormHandler = ({
9488
}, [restoreHistory]);
9589

9690
const handleCancel = useCallback(() => {
97-
setCacheKey(_.uniqueId(currentFormKey));
9891
if (onCancel) {
9992
onCancel();
10093
}
101-
}, [currentFormKey, onCancel]);
94+
}, [onCancel]);
10295

10396
return (
10497
<PydanticFormValidationErrorContext.Provider

frontend/packages/pydantic-forms/src/core/hooks/useApiProvider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import {
2424

2525
export function useApiProvider(
2626
formKey: string,
27-
formInputData: FieldValues[], // TODO: This doesn't seem right
27+
formId: string,
28+
formInputData: FieldValues[],
2829
apiProvider: PydanticFormApiProvider,
29-
cacheKey: string,
3030
) {
3131
return useSWR<PydanticFormApiResponse>(
32-
[formKey, formInputData, cacheKey],
32+
[formKey, formInputData, formId],
3333
([formKey, formInputData]) => {
3434
const requestBody = formInputData;
3535

frontend/packages/pydantic-forms/src/core/hooks/useLabelProvider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ import {
2222
export function useLabelProvider(
2323
labelProvider?: PydanticFormLabelProvider,
2424
formKey?: string,
25-
cacheKey?: string,
25+
formId?: string,
2626
) {
2727
return useSWR<PydanticFormLabelProviderResponse | undefined>(
2828
// cache key
29-
[labelProvider, formKey, cacheKey],
29+
[labelProvider, formKey, formId],
3030

3131
// return val
3232
async () => {
3333
if (labelProvider) {
3434
return labelProvider({
3535
formKey: formKey || '',
36-
id: cacheKey,
36+
id: formId || '',
3737
});
3838
}
3939
},

0 commit comments

Comments
 (0)