Skip to content

Commit

Permalink
fix: debounce saving task description/draft (#1187)
Browse files Browse the repository at this point in the history
Co-authored-by: Johnny Almonte <johnny243@users.noreply.github.com>
  • Loading branch information
johnny243 and johnny243 committed Jun 30, 2022
1 parent 6960bec commit 47a0551
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import useResizeObserver from '@react-hook/resize-observer'
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import type { AppDispatch, RootState } from './store'
Expand Down Expand Up @@ -32,3 +32,13 @@ export const useResize = (ref: React.RefObject<HTMLElement>, effect: (target: HT
}
})
}

export const useDebouncedCallback = (callback: () => void, waitMs: number = 500) => {
const timeout = useRef<any>()

clearTimeout(timeout.current)

timeout.current = setTimeout(() => {
callback()
}, waitMs)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeEvent, createRef, KeyboardEvent, useState } from 'react'
import styled from 'styled-components'
import { v4 as uuidv4 } from 'uuid'

import { useAppDispatch, useAppSelector } from '../../app/hooks'
import { useAppDispatch, useAppSelector, useDebouncedCallback } from '../../app/hooks'
import { GroupModel, taskAdded, tasksGroupDraft } from './tasks-slice'

import { TextInput } from '../../common/components'
Expand Down Expand Up @@ -37,9 +37,7 @@ const CreateTask: React.FC<CreateTaskProps> = ({ group }) => {
const [taskDraft, setTaskDraft] = useState<string>(group.draft ?? '')

function onTextChange(event: ChangeEvent<HTMLInputElement>) {
const draft = event.target.value
dispatch(tasksGroupDraft({ groupName, draft }))
setTaskDraft(draft)
setTaskDraft(event.target.value)
}

function handleKeyPress(event: KeyboardEvent<HTMLInputElement>) {
Expand All @@ -54,6 +52,12 @@ const CreateTask: React.FC<CreateTaskProps> = ({ group }) => {
}
}

useDebouncedCallback(() => {
if (group.draft !== undefined && taskDraft !== group.draft) {
dispatch(tasksGroupDraft({ groupName, draft: taskDraft }))
}
})

if (!canEdit) {
return <></>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './TaskItem.scss'
import { ChangeEvent, createRef, KeyboardEvent, useState } from 'react'
import styled from 'styled-components'

import { useAppDispatch, useAppSelector, useDidMount, useResize } from '../../app/hooks'
import { useAppDispatch, useAppSelector, useDebouncedCallback, useResize } from '../../app/hooks'
import { taskDeleted, TaskModel, taskModified, taskToggled } from './tasks-slice'

import { CheckBoxInput, TextAreaInput } from '../../common/components'
Expand Down Expand Up @@ -116,18 +116,11 @@ const TaskItem: React.FC<TaskItemProps> = ({ task, groupName, innerRef, ...props
}
}

/**
* Save the task after the user has stopped typing.
*/
useDidMount(() => {
const timeoutId = setTimeout(() => {
if (description !== task.description) {
dispatch(taskModified({ task: { id: task.id, description }, groupName }))
}
}, 500)

return () => clearTimeout(timeoutId)
}, [description, groupName])
useDebouncedCallback(() => {
if (description !== task.description) {
dispatch(taskModified({ task: { id: task.id, description }, groupName }))
}
})

useResize(textAreaRef, resizeTextArea)

Expand Down

0 comments on commit 47a0551

Please sign in to comment.