Skip to content

Commit 7483c6e

Browse files
DutchBenRuben van Leeuwen
andauthored
2226 caching issues (#229)
* 2226: Store apiResponse with useState in usePydanticForm to avoid useEffect run * 2226: Align npm version in packagelock * 2226: Typescript fixes * 2226: Adds changeset --------- Co-authored-by: Ruben van Leeuwen <ruben.vanleeuwen@surf.nl>
1 parent 8386290 commit 7483c6e

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
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 caching issues when running same form twice

frontend/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ import {
2020
PydanticFormApiProvider,
2121
PydanticFormApiResponse,
2222
PydanticFormApiResponseType,
23+
PydanticFormDefinitionResponse,
24+
PydanticFormSuccessResponse,
25+
PydanticFormValidationResponse,
2326
} from '@/types';
2427

2528
export function useApiProvider(
2629
formKey: string,
2730
formId: string,
2831
formInputData: FieldValues[],
2932
apiProvider: PydanticFormApiProvider,
33+
setApiResponse: React.Dispatch<
34+
React.SetStateAction<PydanticFormApiResponse | undefined>
35+
>,
3036
) {
31-
return useSWR<PydanticFormApiResponse>(
37+
return useSWR(
3238
[formKey, formInputData, formId],
3339
([formKey, formInputData]) => {
3440
const requestBody = formInputData;
@@ -42,27 +48,33 @@ export function useApiProvider(
4248
throw new Error('No API Response');
4349
}
4450
if (request.form) {
45-
return {
51+
const formDefinitionResponse = {
4652
type: PydanticFormApiResponseType.FORM_DEFINITION,
4753
form: request.form,
4854
meta: request.meta,
49-
} as PydanticFormApiResponse;
55+
} as PydanticFormDefinitionResponse;
56+
setApiResponse(formDefinitionResponse);
57+
return;
5058
}
5159
if (request.validation_errors) {
52-
return {
60+
const formValidationResponse = {
5361
type: PydanticFormApiResponseType.VALIDATION_ERRORS,
5462
validation_errors: request.validation_errors,
55-
} as PydanticFormApiResponse;
63+
} as PydanticFormValidationResponse;
64+
setApiResponse(formValidationResponse);
65+
return;
5666
}
5767
if (
5868
request.status &&
5969
request.status >= 200 &&
6070
request.status < 300
6171
) {
62-
return {
72+
const formSuccessResponse = {
6373
type: PydanticFormApiResponseType.SUCCESS,
6474
data: request.data,
65-
} as PydanticFormApiResponse;
75+
} as PydanticFormSuccessResponse;
76+
setApiResponse(formSuccessResponse);
77+
return;
6678
}
6779
throw new Error('Unknown API Response code');
6880
})

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getValidationErrorDetailsFromResponse,
77
} from '@/core/helper';
88
import type {
9+
PydanticFormApiResponse,
910
PydanticFormConfig,
1011
PydanticFormSchema,
1112
PydanticFormSchemaRawJson,
@@ -73,6 +74,9 @@ export function usePydanticForm(
7374
);
7475
const [isFullFilled, setIsFullFilled] = useState<boolean>(false);
7576
const [isSending, setIsSending] = useState<boolean>(false);
77+
const [apiResponse, setApiResponse] = useState<
78+
PydanticFormApiResponse | undefined
79+
>(undefined);
7680
const [rawSchema, setRawSchema] =
7781
useState<PydanticFormSchemaRawJson>(emptyRawSchema);
7882
const [hasNext, setHasNext] = useState<boolean>(false);
@@ -92,11 +96,13 @@ export function usePydanticForm(
9296
}, [formStep, formSteps]);
9397

9498
// fetch API response with form definition
95-
const {
96-
data: apiResponse,
97-
isLoading: isLoadingSchema,
98-
error: apiError,
99-
} = useApiProvider(formKey, formId, formInputData, apiProvider);
99+
const { isLoading: isLoadingSchema, error: apiError } = useApiProvider(
100+
formKey,
101+
formId,
102+
formInputData,
103+
apiProvider,
104+
setApiResponse,
105+
);
100106

101107
// extract the JSON schema to a more usable custom schema
102108
const { pydanticFormSchema, isLoading: isParsingSchema } =

0 commit comments

Comments
 (0)