Skip to content

Commit dd1c4ab

Browse files
author
Ruben van Leeuwen
committed
1891: Refactor. Remove some unused code
1 parent b84354c commit dd1c4ab

File tree

7 files changed

+94
-207
lines changed

7 files changed

+94
-207
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,26 @@ import React from 'react';
1010

1111
import RenderForm from '@/components/render/RenderForm';
1212
import PydanticFormContextProvider from '@/core/PydanticFormContextProvider';
13+
import type { PydanticFormContextProviderProps } from '@/core/PydanticFormContextProvider';
1314
import { TranslationsProvider } from '@/messages/translationsProvider';
14-
import type {
15-
PydanticFormInitialContextProps,
16-
PydanticFormMetaData,
17-
} from '@/types';
18-
19-
export interface PydanticFormProps
20-
extends Omit<PydanticFormInitialContextProps, 'formKey' | 'children'> {
21-
id: string;
22-
metaData?: PydanticFormMetaData;
23-
}
2415

2516
export const PydanticForm = ({
26-
id,
27-
metaData,
28-
...contextProps
29-
}: PydanticFormProps) => (
17+
config,
18+
formKey,
19+
onCancel,
20+
onSuccess,
21+
title,
22+
}: Omit<PydanticFormContextProviderProps, 'children'>) => (
3023
<TranslationsProvider
31-
customTranslations={contextProps.config.customTranslations}
32-
locale={contextProps.config.locale}
24+
customTranslations={config.customTranslations}
25+
locale={config.locale}
3326
>
3427
<PydanticFormContextProvider
35-
{...contextProps}
36-
formKey={id}
37-
metaData={metaData}
28+
config={config}
29+
onCancel={onCancel}
30+
onSuccess={onSuccess}
31+
title={title}
32+
formKey={formKey}
3833
>
3934
{RenderForm}
4035
</PydanticFormContextProvider>

frontend/packages/pydantic-forms/src/components/form/Footer.tsx

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,11 @@ import { useTranslations } from 'next-intl';
1010
import { usePydanticFormContext } from '@/core';
1111

1212
const Footer = () => {
13-
const {
14-
resetForm,
15-
reactHookForm,
16-
onCancel,
17-
onPrevious,
18-
cancelButton,
19-
resetButtonAlternative,
20-
sendLabel,
21-
allowUntouchedSubmit,
22-
hasNext,
23-
formInputData,
24-
} = usePydanticFormContext();
13+
const { resetForm, onCancel, onPrevious, hasNext, formInputData } =
14+
usePydanticFormContext();
2515

2616
const t = useTranslations('footer');
27-
const submitButtonLabel = sendLabel ?? hasNext ? t('send') : t('submit');
17+
const submitButtonLabel = hasNext ? t('send') : t('submit');
2818
const PreviousButton = () => (
2919
<button
3020
type="button"
@@ -37,31 +27,27 @@ const Footer = () => {
3727

3828
const ResetButton = () => {
3929
return (
40-
resetButtonAlternative ?? (
41-
<button
42-
type="button"
43-
onClick={(e) => {
44-
resetForm(e);
45-
}}
46-
style={{ padding: '12px' }}
47-
>
48-
{t('reset')}
49-
</button>
50-
)
30+
<button
31+
type="button"
32+
onClick={(e) => {
33+
resetForm(e);
34+
}}
35+
style={{ padding: '12px' }}
36+
>
37+
{t('reset')}
38+
</button>
5139
);
5240
};
5341

5442
const CancelButton = () => {
5543
return (
56-
cancelButton ?? (
57-
<button
58-
type="button"
59-
onClick={onCancel}
60-
style={{ padding: '12px' }}
61-
>
62-
{t('cancel')}
63-
</button>
64-
)
44+
<button
45+
type="button"
46+
onClick={onCancel}
47+
style={{ padding: '12px' }}
48+
>
49+
{t('cancel')}
50+
</button>
6551
);
6652
};
6753

@@ -74,11 +60,6 @@ const Footer = () => {
7460
return (
7561
<div style={{ height: '200px', marginTop: '24px' }}>
7662
<div style={{ display: 'flex', gap: '16px' }}>
77-
{reactHookForm.formState.isValid &&
78-
!allowUntouchedSubmit &&
79-
!reactHookForm.formState.isDirty && (
80-
<div>Het formulier is nog niet aangepast</div>
81-
)}
8263
{formInputData && formInputData.length > 0 && (
8364
<PreviousButton />
8465
)}
@@ -89,19 +70,6 @@ const Footer = () => {
8970

9071
<SubmitButton />
9172
</div>
92-
<div
93-
style={{
94-
margin: '8px 0',
95-
color: 'red',
96-
fontWeight: '600',
97-
fontSize: '24px',
98-
}}
99-
>
100-
{!reactHookForm.formState.isValid &&
101-
reactHookForm.formState.isDirty && (
102-
<div>{t('notFilledYet')}</div>
103-
)}
104-
</div>
10573
</div>
10674
);
10775
};

frontend/packages/pydantic-forms/src/components/render/RenderForm.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ const RenderForm = (contextProps: PydanticFormContextProps) => {
2222
config,
2323
isLoading,
2424
isFullFilled,
25-
successNotice,
2625
isSending,
27-
skipSuccessNotice,
28-
loadingComponent,
2926
} = contextProps;
3027

3128
const { formRenderer, footerRenderer, headerRenderer } = config || {};
@@ -38,7 +35,7 @@ const RenderForm = (contextProps: PydanticFormContextProps) => {
3835

3936
const t = useTranslations('renderForm');
4037

41-
const LoadingComponent = loadingComponent ?? <div>{t('loading')}</div>;
38+
const LoadingComponent = <div>{t('loading')}</div>;
4239

4340
if (isLoading && !isSending) {
4441
return LoadingComponent;
@@ -49,11 +46,7 @@ const RenderForm = (contextProps: PydanticFormContextProps) => {
4946
}
5047

5148
if (isFullFilled) {
52-
if (skipSuccessNotice) {
53-
return <></>;
54-
}
55-
56-
return <div>{successNotice ?? t('successfullySent')}</div>;
49+
return <div>{t('successfullySent')}</div>;
5750
}
5851

5952
const FormRenderer = formRenderer ?? Form;

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

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,39 @@ import {
3535
Locale,
3636
PydanticFormContextProps,
3737
PydanticFormFieldType,
38-
PydanticFormInitialContextProps,
3938
PydanticFormSchemaRawJson,
4039
PydanticFormValidationErrorDetails,
40+
PydanticFormsContextConfig,
4141
} from '@/types';
4242
import { getHashForArray } from '@/utils';
4343

4444
export const PydanticFormContext =
4545
createContext<PydanticFormContextProps | null>(null);
4646

47+
export interface PydanticFormContextProviderProps {
48+
children: (props: PydanticFormContextProps) => React.ReactNode;
49+
config: PydanticFormsContextConfig;
50+
formKey: string;
51+
onCancel?: () => void;
52+
onSuccess?: (fieldValues: FieldValues, response: object) => void;
53+
title?: string;
54+
}
55+
4756
function PydanticFormContextProvider({
48-
formKey,
49-
formIdKey,
50-
metaData,
51-
title,
52-
sendLabel,
53-
loadingComponent,
54-
successNotice,
55-
onSuccess,
56-
onCancel,
5757
children,
5858
config,
59-
}: PydanticFormInitialContextProps) {
59+
formKey,
60+
onCancel,
61+
onSuccess,
62+
title,
63+
}: PydanticFormContextProviderProps) {
6064
const {
6165
apiProvider,
62-
labelProvider,
66+
componentMatcherExtender,
6367
customDataProvider,
6468
customDataProviderCacheKey,
65-
resetButtonAlternative,
66-
allowUntouchedSubmit,
67-
skipSuccessNotice,
68-
componentMatcherExtender,
69+
labelProvider,
6970
locale,
70-
cancelButton,
7171
} = config;
7272

7373
const [formInputHistory, setFormInputHistory] = useState(
@@ -96,24 +96,27 @@ function PydanticFormContextProvider({
9696

9797
const [errorDetails, setErrorDetails] =
9898
useState<PydanticFormValidationErrorDetails>();
99+
99100
const [isFullFilled, setIsFullFilled] = useState(false);
100101
const [isSending, setIsSending] = useState(false);
101102

102103
// fetch the labels of the form, can also contain default values
103104
const { data: formLabels, isLoading: isLoadingFormLabels } =
104-
useLabelProvider(labelProvider, formKey, formIdKey);
105+
useLabelProvider(labelProvider, formKey);
105106

107+
// fetch custom data
106108
const { data: customData, isLoading: isLoadingCustomData } =
107109
useCustomDataProvider(
108110
customDataProviderCacheKey ?? 100,
109111
customDataProvider,
110112
);
111113

114+
// fetch API response with form definition
112115
const {
113116
data: apiResponse,
114117
isLoading: isLoadingSchema,
115118
error,
116-
} = useApiProvider(formKey, formInputData, apiProvider, metaData);
119+
} = useApiProvider(formKey, formInputData, apiProvider);
117120

118121
const emptyRawSchema: PydanticFormSchemaRawJson = useMemo(
119122
() => ({
@@ -295,39 +298,29 @@ function PydanticFormContextProvider({
295298
const PydanticFormContextState = {
296299
// to prevent an issue where the sending state hangs
297300
// we check both the SWR hook state as our manual state
298-
isSending: isSending && isLoadingSchema,
299-
isLoading,
300-
reactHookForm,
301-
pydanticFormSchema,
302-
loadingComponent,
303-
onPrevious: () => goToPreviousStep(reactHookForm?.getValues()),
304-
onCancel,
305-
title,
306-
sendLabel,
307-
isFullFilled,
301+
clearForm,
302+
config,
308303
customDataProvider,
309304
errorDetails,
310-
resetErrorDetails,
311-
successNotice,
312-
submitForm,
313-
resetForm,
314-
cancelButton,
315-
skipSuccessNotice,
316-
allowUntouchedSubmit,
317-
resetButtonAlternative,
318-
config,
319-
formKey,
320-
formIdKey,
321-
clearForm,
305+
fieldDataStorage,
322306
formInputData,
307+
formKey,
323308
hasNext,
324309
initialData,
325-
fieldDataStorage,
310+
isFullFilled,
311+
isLoading,
312+
isSending: isSending && isLoadingSchema,
313+
onCancel,
314+
onPrevious: () => goToPreviousStep(reactHookForm?.getValues()),
315+
pydanticFormSchema,
316+
reactHookForm,
317+
resetErrorDetails,
318+
resetForm,
319+
submitForm,
320+
title,
326321
};
327322

328-
// a useeffect for whenever the error response updates
329-
// sometimes we need to update the form,
330-
// some we need to update the errors
323+
// useEffect to handle API responses
331324
useEffect(() => {
332325
if (!apiResponse) {
333326
return;
@@ -359,41 +352,33 @@ function PydanticFormContextProvider({
359352
setIsSending(false);
360353
// eslint-disable-next-line react-hooks/exhaustive-deps
361354
}, [apiResponse]); // Avoid completing the dependencies array here to avoid unwanted resetFormData calls
362-
// a useeffect for filling data whenever formdefinition or labels update
363355

364-
// When a formKey changes we reset the form input data
356+
// Useeffect to the form input data if the formKey changes
365357
useEffect(() => {
366358
if (formKey !== formRef.current) {
367-
// When the formKey changes we need to reset the form input data
368359
setFormInputData([]);
369360
setFormInputHistory(new Map<string, object>());
370361
awaitReset({});
371362
formRef.current = formKey;
372363
}
373364
}, [awaitReset, formKey]);
374365

375-
// handle successfull submits
366+
// UseEffect to handle successfull submits
376367
useEffect(() => {
377368
if (!isFullFilled) {
378369
return;
379370
}
380371

381372
if (onSuccess) {
382373
const values = reactHookForm.getValues();
383-
if (skipSuccessNotice) {
384-
onSuccess(values, apiResponse || {});
385-
} else {
386-
setTimeout(() => {
387-
onSuccess?.(values, apiResponse || {});
388-
}, 1500); // Delay to allow notice to show first
389-
}
374+
onSuccess(values, apiResponse || {});
390375
}
391376

392377
setFormInputHistory(new Map<string, object>());
393378
// eslint-disable-next-line react-hooks/exhaustive-deps
394379
}, [apiResponse, isFullFilled]); // Avoid completing the dependencies array here to avoid unwanted resetFormData calls
395380

396-
// this handles errors throws by the useApiProvider call
381+
// UseEffect to handles errors throws by the useApiProvider call
397382
// for instance unexpected 500 errors
398383
useEffect(() => {
399384
if (!error) {
@@ -407,6 +392,7 @@ function PydanticFormContextProvider({
407392
});
408393
}, [error]);
409394

395+
// UseEffect to handle locale change
410396
useEffect(() => {
411397
const getLocale = () => {
412398
switch (locale) {

frontend/packages/pydantic-forms/src/core/helper.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export const getMockPydanticFormField = (
2929
properties: {},
3030
required: false,
3131
options: [],
32-
columns: 6,
3332
schema: {
3433
type: PydanticFormFieldType.STRING,
3534
format: PydanticFormFieldFormat.DEFAULT,

0 commit comments

Comments
 (0)