Skip to content

Commit

Permalink
debounce user input
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 10, 2024
1 parent bb71499 commit 501d6d7
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/components/EditSection/EditSection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, memo } from 'react';
import { useEffect, memo, useRef } from 'react';
import { debounce } from 'lodash';
import classNames from 'classnames';
import { saveRecordLocally } from '@common/helpers/record.helper';
import { PROFILE_BFIDS } from '@common/constants/bibframe.constants';
Expand All @@ -8,10 +9,12 @@ import { Fields } from '@components/Fields';
import { Prompt } from '@components/Prompt';
import { useContainerEvents } from '@common/hooks/useContainerEvents';
import { useServicesContext } from '@common/hooks/useServicesContext';
import { renderDrawComponent } from './renderDrawComponent';
import './EditSection.scss';
import { useRecordGeneration } from '@common/hooks/useRecordGeneration';
import { useInputsState, useProfileState, useStatusState, useUIState } from '@src/store';
import { renderDrawComponent } from './renderDrawComponent';
import './EditSection.scss';

const USER_INPUT_DELAY = 750;

export const EditSection = memo(() => {
const { selectedEntriesService } = useServicesContext() as Required<ServicesParams>;
Expand Down Expand Up @@ -45,10 +48,16 @@ export const EditSection = memo(() => {
return () => clearInterval(autoSaveRecord);
}, [isEdited, userValues]);

const debouncedAddUserValues = useRef(
debounce((value: UserValues) => {
addUserValues?.(value);
}, USER_INPUT_DELAY),
).current;

const onChange = (uuid: string, contents: Array<UserValueContents>) => {
if (!isEdited) setIsEdited(true);

addUserValues?.({
debouncedAddUserValues({
[uuid]: {
uuid,
contents,
Expand Down

0 comments on commit 501d6d7

Please sign in to comment.