fix(striker-ui): add buildObjectStateSetterCallback

main
Tsu-ba-me 2 years ago
parent 43f4d89271
commit 03a251f44d
  1. 17
      striker-ui/components/StrikerConfig/AddPeerDialog.tsx
  2. 9
      striker-ui/lib/buildObjectStateSetterCallback.ts

@ -5,6 +5,7 @@ import INPUT_TYPES from '../../lib/consts/INPUT_TYPES';
import api from '../../lib/api'; import api from '../../lib/api';
import buildMapToMessageSetter from '../../lib/buildMapToMessageSetter'; import buildMapToMessageSetter from '../../lib/buildMapToMessageSetter';
import buildNumberTestBatch from '../../lib/test_input/buildNumberTestBatch'; import buildNumberTestBatch from '../../lib/test_input/buildNumberTestBatch';
import buildObjectStateSetterCallback from '../../lib/buildObjectStateSetterCallback';
import CheckboxWithLabel from '../CheckboxWithLabel'; import CheckboxWithLabel from '../CheckboxWithLabel';
import ConfirmDialog from '../ConfirmDialog'; import ConfirmDialog from '../ConfirmDialog';
import FlexBox from '../FlexBox'; import FlexBox from '../FlexBox';
@ -58,26 +59,18 @@ const AddPeerDialog = forwardRef<
const [isSubmittingAddPeer, setIsSubmittingAddPeer] = const [isSubmittingAddPeer, setIsSubmittingAddPeer] =
useProtectedState<boolean>(false, protect); useProtectedState<boolean>(false, protect);
const buildFormValiditySetterCallback = useCallback(
(key: string, value: boolean) =>
({ [key]: toReplace, ...restPrevious }) => ({
...restPrevious,
[key]: value,
}),
[],
);
const buildInputFirstRenderFunction = useCallback( const buildInputFirstRenderFunction = useCallback(
(key: string) => (key: string) =>
({ isRequired }: { isRequired: boolean }) => { ({ isRequired }: { isRequired: boolean }) => {
setFormValidity(buildFormValiditySetterCallback(key, !isRequired)); setFormValidity(buildObjectStateSetterCallback(key, !isRequired));
}, },
[buildFormValiditySetterCallback], [],
); );
const buildFinishInputTestBatchFunction = useCallback( const buildFinishInputTestBatchFunction = useCallback(
(key: string) => (result: boolean) => { (key: string) => (result: boolean) => {
setFormValidity(buildFormValiditySetterCallback(key, result)); setFormValidity(buildObjectStateSetterCallback(key, result));
}, },
[buildFormValiditySetterCallback], [],
); );
const setAPIMessage = useCallback((message?: Message) => { const setAPIMessage = useCallback((message?: Message) => {
messageGroupRef.current.setMessage?.call(null, 'api', message); messageGroupRef.current.setMessage?.call(null, 'api', message);

@ -0,0 +1,9 @@
const buildObjectStateSetterCallback =
<S extends Record<string, unknown>>(key: keyof S, value: S[keyof S]) =>
({ [key]: toReplace, ...restPrevious }: S): S =>
({
...restPrevious,
[key]: value,
} as S);
export default buildObjectStateSetterCallback;
Loading…
Cancel
Save