Skip to content

Commit 4720959

Browse files
author
Ruben van Leeuwen
committed
2143: Re-implement back fucntionality
1 parent ce81be0 commit 4720959

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

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

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

44
import { PydanticFormValidationErrorContext } from '@/PydanticForm';
5+
import { useGetConfig, usePydanticForm } from '@/core/hooks';
56
import { PydanticFormSuccessResponse } from '@/types';
7+
import { getHashForArray } from '@/utils';
68

79
import { ReactHookForm } from './ReactHookForm';
8-
import { useGetConfig, usePydanticForm } from './hooks';
910

1011
export interface PydanticFormHandlerProps {
1112
formKey: string;
@@ -26,18 +27,44 @@ export const PydanticFormHandler = ({
2627
const config = useGetConfig();
2728
const [formStep, setStep] = useState<FieldValues>();
2829
const formStepsRef = useRef<FieldValues[]>([]);
30+
const [initialValues, setInitialValues] = useState<FieldValues>();
31+
const formInputHistoryRef = useRef<Map<string, FieldValues>>(
32+
new Map<string, object>(),
33+
);
34+
35+
const storeHistory = useCallback(async (stepData: FieldValues) => {
36+
const hashOfPreviousSteps = await getHashForArray(formStepsRef.current);
37+
formInputHistoryRef.current.set(hashOfPreviousSteps, stepData);
38+
}, []);
39+
2940
const {
3041
validationErrorsDetails,
3142
apiError,
3243
hasNext,
3344
isFullFilled,
3445
isLoading,
3546
pydanticFormSchema,
36-
initialValues,
47+
defaultValues,
3748
} = usePydanticForm(formKey, config, formStepsRef, onSuccess, formStep);
3849

39-
const handleStepSubmit = useCallback((fieldValues: FieldValues) => {
40-
setStep(fieldValues);
50+
const handleStepSubmit = useCallback(
51+
async (fieldValues: FieldValues) => {
52+
await storeHistory(fieldValues);
53+
setStep(fieldValues);
54+
},
55+
[storeHistory],
56+
);
57+
58+
const onPrevious = useCallback(async () => {
59+
const previousSteps = formStepsRef.current.slice(0, -1);
60+
const hashOfPreviousSteps = await getHashForArray(previousSteps);
61+
if (formInputHistoryRef.current.has(hashOfPreviousSteps)) {
62+
setInitialValues(
63+
formInputHistoryRef.current.get(hashOfPreviousSteps) || {},
64+
);
65+
}
66+
formStepsRef.current = previousSteps;
67+
setStep(undefined);
4168
}, []);
4269

4370
const handleCancel = useCallback(() => {
@@ -50,16 +77,17 @@ export const PydanticFormHandler = ({
5077
value={validationErrorsDetails}
5178
>
5279
<ReactHookForm
53-
pydanticFormSchema={pydanticFormSchema}
54-
isLoading={isLoading}
55-
isFullFilled={isFullFilled}
56-
isSending={false}
57-
hasNext={hasNext}
5880
apiError={apiError}
59-
initialValues={initialValues}
60-
handleSubmit={handleStepSubmit}
61-
hasPrevious={false}
81+
defaultValues={defaultValues}
6282
handleCancel={handleCancel}
83+
handleSubmit={handleStepSubmit}
84+
hasNext={hasNext}
85+
hasPrevious={formStepsRef.current.length > 0}
86+
initialValues={initialValues}
87+
isFullFilled={isFullFilled}
88+
isLoading={isLoading}
89+
onPrevious={onPrevious}
90+
pydanticFormSchema={pydanticFormSchema}
6391
title={title}
6492
/>
6593
</PydanticFormValidationErrorContext.Provider>

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,30 @@ import { useGetZodSchema } from './hooks';
2424

2525
export interface ReactHookFormProps {
2626
apiError?: string;
27+
defaultValues: FieldValues;
2728
handleCancel: () => void;
2829
handleSubmit: (fieldValues: FieldValues) => void;
2930
hasNext: boolean;
3031
hasPrevious: boolean;
31-
initialValues: FieldValues;
32+
initialValues?: FieldValues;
3233
isFullFilled: boolean;
3334
isLoading: boolean;
34-
isSending: boolean;
35+
onPrevious: () => void;
3536
pydanticFormSchema?: PydanticFormSchema;
3637
title?: string;
3738
}
3839

3940
export const ReactHookForm = ({
4041
apiError,
42+
defaultValues,
4143
handleCancel,
4244
handleSubmit,
4345
hasNext,
4446
hasPrevious,
4547
initialValues,
4648
isFullFilled,
4749
isLoading,
48-
isSending,
50+
onPrevious,
4951
pydanticFormSchema,
5052
title,
5153
}: ReactHookFormProps) => {
@@ -66,19 +68,16 @@ export const ReactHookForm = ({
6668
const reactHookForm = useForm({
6769
resolver: zodResolver(zodSchema),
6870
mode: 'all',
69-
values: initialValues,
71+
defaultValues,
72+
values: initialValues || defaultValues,
7073
});
7174

7275
if (apiError) {
7376
console.error('API Error:', apiError);
7477
return ErrorComponent;
7578
}
7679

77-
if (isLoading && !isSending) {
78-
return LoadingComponent;
79-
}
80-
81-
if (!pydanticFormSchema || isSending) {
80+
if (isLoading || !pydanticFormSchema) {
8281
return LoadingComponent;
8382
}
8483

@@ -113,6 +112,7 @@ export const ReactHookForm = ({
113112
onCancel={handleCancel}
114113
hasNext={hasNext}
115114
hasPrevious={hasPrevious}
115+
onPrevious={onPrevious}
116116
/>
117117
</form>
118118
</FormProvider>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface UsePydanticFormReturn {
2424
isLoading: boolean;
2525
isSending: boolean;
2626
pydanticFormSchema?: PydanticFormSchema;
27-
initialValues: FieldValues;
27+
defaultValues: FieldValues;
2828
}
2929

3030
export function usePydanticForm(
@@ -75,7 +75,7 @@ export function usePydanticForm(
7575
const { pydanticFormSchema, isLoading: isParsingSchema } =
7676
usePydanticFormParser(rawSchema, formLabels?.labels);
7777

78-
const initialValues = useMemo(() => {
78+
const defaultValues = useMemo(() => {
7979
return getFormValuesFromFieldOrLabels(
8080
pydanticFormSchema?.properties,
8181
{
@@ -139,7 +139,7 @@ export function usePydanticForm(
139139
isFullFilled,
140140
isLoading,
141141
pydanticFormSchema,
142-
initialValues,
142+
defaultValues,
143143
isSending,
144144
};
145145
}

0 commit comments

Comments
 (0)