Skip to content

Commit

Permalink
refactor: sync in apodization to use the new hook
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Sep 20, 2024
1 parent 0d1d9b7 commit 1d487d6
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/component/panels/filtersPanel/Filters/useSharedApodization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import * as Yup from 'yup';

import { defaultApodizationOptions } from '../../../../data/constants/DefaultApodizationOptions';
import { useDispatch } from '../../../context/DispatchContext';
import { useFilterSyncOptions } from '../../../context/FilterSyncOptionsContext';

import { useSyncedFilterOptions } from './useSyncedFilterOptions';

const simpleValidationSchema = Yup.object().shape({
lineBroadening: Yup.number().required(),
Expand Down Expand Up @@ -40,8 +41,6 @@ export const useSharedApodization = (
} = options;

const dispatch = useDispatch();
const { sharedFilterOptions, updateFilterOptions } = useFilterSyncOptions();
const isSynOptionsDirty = useRef(true);
const previousPreviewRef = useRef<boolean>(true);

let formData = initialValues;
Expand All @@ -62,6 +61,13 @@ export const useSharedApodization = (
mode: 'onChange',
});

function syncWatch(sharedFilterOptions) {
reset({ ...getValues(), ...sharedFilterOptions });
}

const { syncFilterOptions, clearSyncFilterOptions } =
useSyncedFilterOptions(syncWatch);

const onChange = useCallback(
(values: ApodizationOptions) => {
const { livePreview, ...options } = values;
Expand Down Expand Up @@ -92,7 +98,7 @@ export const useSharedApodization = (
type: 'APPLY_APODIZATION_FILTER',
payload: { options },
});
updateFilterOptions(null);
clearSyncFilterOptions();

break;
}
Expand All @@ -102,31 +108,22 @@ export const useSharedApodization = (

previousPreviewRef.current = livePreview;
},
[dispatch, onChange, updateFilterOptions],
[clearSyncFilterOptions, dispatch, onChange],
);

const handleCancelFilter = useCallback(() => {
dispatch({
type: 'RESET_SELECTED_TOOL',
});
updateFilterOptions(null);
}, [dispatch, updateFilterOptions]);
clearSyncFilterOptions();
}, [clearSyncFilterOptions, dispatch]);

const submitHandler = useCallback(() => {
void handleSubmit((values) => {
isSynOptionsDirty.current = false;
updateFilterOptions(values);
syncFilterOptions(values);
handleApplyFilter(values, 'onChange');
})();
}, [handleSubmit, updateFilterOptions, handleApplyFilter]);

useEffect(() => {
if (sharedFilterOptions && isSynOptionsDirty.current) {
reset({ ...getValues(), ...sharedFilterOptions });
} else {
isSynOptionsDirty.current = true;
}
}, [getValues, sharedFilterOptions, reset]);
}, [handleSubmit, syncFilterOptions, handleApplyFilter]);

useEffect(() => {
if (applyFilterOnload) {
Expand Down

0 comments on commit 1d487d6

Please sign in to comment.