Skip to content

Commit 8ab2da0

Browse files
committed
fix the filter-input state notify/retention
Signed-off-by: Scott J Dickerson <[email protected]>
1 parent 08b81de commit 8ab2da0

File tree

3 files changed

+70
-46
lines changed

3 files changed

+70
-46
lines changed

client/src/app/pages/source-platforms/discover-import-wizard/discover-import-wizard.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const DiscoverImportWizardInner: React.FC<IDiscoverImportWizard> = ({
125125
description={t("platformDiscoverWizard.description")}
126126
/>
127127
}
128+
isVisitRequired
128129
>
129130
<WizardStep
130131
id="filter-input"
@@ -134,7 +135,11 @@ const DiscoverImportWizardInner: React.FC<IDiscoverImportWizard> = ({
134135
isNextDisabled: !filters.isValid,
135136
}}
136137
>
137-
<FilterInput platform={platform} onFiltersChanged={setFilters} />
138+
<FilterInput
139+
platform={platform}
140+
onFiltersChanged={setFilters}
141+
initialFilters={filters}
142+
/>
138143
</WizardStep>
139144

140145
<WizardStep

client/src/app/pages/source-platforms/discover-import-wizard/filter-input.tsx

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import { useTranslation } from "react-i18next";
3-
import { useForm } from "react-hook-form";
3+
import { useForm, useWatch, UseFormReturn } from "react-hook-form";
44
import * as yup from "yup";
55
import { yupResolver } from "@hookform/resolvers/yup";
66

@@ -13,6 +13,7 @@ import { HookFormPFGroupController } from "@app/components/HookFormPFFields";
1313
import { jsonSchemaToYupSchema } from "@app/components/schema-defined-fields/utils";
1414

1515
interface FiltersFormValues {
16+
filterRequired: boolean;
1617
schema?: TargetedSchema;
1718
document?: JsonDocument;
1819
}
@@ -24,10 +25,40 @@ export interface FilterState {
2425
isValid: boolean;
2526
}
2627

28+
const useFilterStateChangeHandler = (
29+
form: UseFormReturn<FiltersFormValues>,
30+
onFiltersChanged: (filterState: FilterState) => void
31+
) => {
32+
const {
33+
control,
34+
formState: { isValid },
35+
} = form;
36+
37+
const watchedValues = useWatch({
38+
control,
39+
name: ["schema", "document", "filterRequired"],
40+
});
41+
42+
const filterState = React.useMemo((): FilterState => {
43+
const [schema, document, filterRequired] = watchedValues;
44+
return {
45+
filterRequired,
46+
schema,
47+
document,
48+
isValid,
49+
};
50+
}, [watchedValues, isValid]);
51+
52+
React.useEffect(() => {
53+
onFiltersChanged(filterState);
54+
}, [onFiltersChanged, filterState]);
55+
};
56+
2757
export const FilterInput: React.FC<{
2858
platform: SourcePlatform;
2959
onFiltersChanged: (filterState: FilterState) => void;
30-
}> = ({ platform, onFiltersChanged }) => {
60+
initialFilters?: FilterState;
61+
}> = ({ platform, onFiltersChanged, initialFilters }) => {
3162
const { t } = useTranslation();
3263

3364
const validationSchema = yup.object().shape({
@@ -41,54 +72,35 @@ export const FilterInput: React.FC<{
4172
}),
4273
});
4374

44-
const {
45-
control,
46-
reset,
47-
getValues,
48-
watch,
49-
formState: { isValid },
50-
} = useForm<FiltersFormValues>({
75+
const form = useForm<FiltersFormValues>({
5176
defaultValues: {
52-
schema: undefined,
53-
document: {},
77+
filterRequired: initialFilters?.filterRequired ?? true,
78+
schema: initialFilters?.schema ?? undefined,
79+
document: initialFilters?.document ?? {},
5480
},
5581
resolver: yupResolver(validationSchema),
5682
mode: "all",
5783
});
84+
const { setValue, control } = form;
5885

59-
// Fetch the discovery filters schema for the platform and put it in the form
86+
// Fetch the discovery filter schema for the platform
6087
const { filtersSchema } = useFetchPlatformDiscoveryFilterSchema(
6188
platform?.kind
6289
);
90+
91+
// Update form values that react to schema changes
6392
React.useEffect(() => {
93+
// TODO: If the schema is undefined, it could be a 404 and we should not require a filter
6494
if (filtersSchema) {
65-
reset({
66-
...getValues(),
67-
schema: filtersSchema,
68-
document: {},
69-
});
95+
setValue("schema", filtersSchema);
96+
setValue("filterRequired", true);
7097
} else {
71-
reset({
72-
...getValues(),
73-
schema: undefined,
74-
document: undefined,
75-
});
98+
setValue("schema", undefined);
99+
setValue("filterRequired", false);
76100
}
77-
}, [filtersSchema, reset, getValues]);
101+
}, [filtersSchema, setValue]);
78102

79-
// Relay form state changes to parent component
80-
const watchedValues = watch();
81-
React.useEffect(() => {
82-
// TODO: Track the filter loading state -- 404 = no filter needed, !!data = filter needed
83-
const filterRequired = !!filtersSchema;
84-
85-
onFiltersChanged({
86-
filterRequired,
87-
schema: watchedValues.schema,
88-
document: watchedValues.document,
89-
isValid: isValid,
90-
});
91-
}, [onFiltersChanged, watchedValues, isValid, filtersSchema]);
103+
useFilterStateChangeHandler(form, onFiltersChanged);
92104

93105
return (
94106
<div>

client/src/app/pages/source-platforms/discover-import-wizard/useWizardReducer.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,23 @@ export const useWizardReducer = () => {
5151
validateWizardState
5252
);
5353

54+
// Create stable callbacks using useCallback
55+
const setFilters = React.useCallback((filters: FilterState) => {
56+
dispatch({ type: "SET_FILTERS", payload: filters });
57+
}, []);
58+
59+
const setResults = React.useCallback((results: ResultsData | null) => {
60+
dispatch({ type: "SET_RESULTS", payload: results });
61+
}, []);
62+
63+
const reset = React.useCallback(() => {
64+
dispatch({ type: "RESET" });
65+
}, []);
66+
5467
return {
5568
state,
56-
setFilters(filters: FilterState) {
57-
dispatch({ type: "SET_FILTERS", payload: filters });
58-
},
59-
setResults(results: ResultsData | null) {
60-
dispatch({ type: "SET_RESULTS", payload: results });
61-
},
62-
reset() {
63-
dispatch({ type: "RESET" });
64-
},
69+
setFilters,
70+
setResults,
71+
reset,
6572
};
6673
};

0 commit comments

Comments
 (0)