Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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