From 506bc4bc6c6743a72a325f30c4bfe801bedf6c09 Mon Sep 17 00:00:00 2001 From: hamed musallam Date: Tue, 9 Jul 2024 13:26:18 +0200 Subject: [PATCH] fix: apply apodization on the fly when opening its options panel --- .../header/ApodizationOptionsPanel.tsx | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/component/header/ApodizationOptionsPanel.tsx b/src/component/header/ApodizationOptionsPanel.tsx index 40698acc8..bdaf552cf 100644 --- a/src/component/header/ApodizationOptionsPanel.tsx +++ b/src/component/header/ApodizationOptionsPanel.tsx @@ -5,7 +5,7 @@ import { Filter, ApodizationOptions as BaseApodizationOptions, } from 'nmr-processing'; -import { useRef, memo } from 'react'; +import { useRef, memo, useCallback, useEffect } from 'react'; import { useForm } from 'react-hook-form'; import * as Yup from 'yup'; @@ -43,6 +43,20 @@ function ApodizationOptionsInnerPanel( const dispatch = useDispatch(); const previousPreviewRef = useRef(initialValues.livePreview); + const onChange = useCallback( + (values) => { + const { livePreview, ...options } = values; + + if (livePreview || previousPreviewRef.current !== livePreview) { + dispatch({ + type: 'CALCULATE_APODIZATION_FILTER', + payload: { livePreview, options }, + }); + } + }, + [dispatch], + ); + function handleApplyFilter( values, triggerSource: 'apply' | 'onChange' = 'apply', @@ -50,12 +64,7 @@ function ApodizationOptionsInnerPanel( const { livePreview, ...options } = values; switch (triggerSource) { case 'onChange': { - if (livePreview || previousPreviewRef.current !== livePreview) { - dispatch({ - type: 'CALCULATE_APODIZATION_FILTER', - payload: { livePreview, options }, - }); - } + onChange(values); break; } @@ -102,6 +111,10 @@ function ApodizationOptionsInnerPanel( const { onChange: onLivePreviewChange, ...otherLivePreviewOptions } = register('livePreview'); + useEffect(() => { + void handleSubmit((values) => onChange(values))(); + }, [handleSubmit, onChange]); + return (