diff --git a/client/src/hooks/useModalComplete.ts b/client/src/hooks/useModalComplete.ts index 820bfe3..2315aa1 100644 --- a/client/src/hooks/useModalComplete.ts +++ b/client/src/hooks/useModalComplete.ts @@ -5,7 +5,7 @@ import { toast } from 'react-toastify'; import { InputTodo } from '@todo/todo.type'; import { TodoList } from '@todo/todoList'; -import { elapsedTimeAtom, todoList } from '@util/GlobalState'; +import { asyncActiveTodo, elapsedTimeAtom, todoList } from '@util/GlobalState'; import { getCheckedInputData } from '@util/modal.util'; import 'react-toastify/dist/ReactToastify.css'; @@ -27,30 +27,26 @@ const COMPLETE_MESSAGE = { const useModalComplete = (type: string): any[] => { const [todoListAtom, setTodoListAtom] = useAtom(todoList); const elapsedTime = useAtomValue(elapsedTimeAtom); + const activeTodo = useAtomValue(asyncActiveTodo); const setComplete = async ( inputData: any[], setHasModal: Dispatch>, editingTodoId: string, ): Promise => { - todoListAtom - .updateElapsedTime(elapsedTime) - .then((newTodoList) => { - return newTodoList; - }) - .then(async (newTodoList) => { - return await MODAL_COMPLETE_ACTIONS[type as keyof typeof MODAL_COMPLETE_ACTIONS]( - newTodoList, - getCheckedInputData(type, inputData), - editingTodoId, - ); - }) - .then((data) => { - setTodoListAtom(data); + const newTodoList = activeTodo !== undefined ? await todoListAtom.updateElapsedTime(elapsedTime) : todoListAtom; + + MODAL_COMPLETE_ACTIONS[type as keyof typeof MODAL_COMPLETE_ACTIONS]( + newTodoList, + getCheckedInputData(type, inputData), + editingTodoId, + ) + .then((updatedTodoList) => { + setTodoListAtom(updatedTodoList); setHasModal(false); toast.success(COMPLETE_MESSAGE[type as keyof typeof COMPLETE_MESSAGE]); }) - .catch((err: any) => { + .catch((err) => { toast.error(err.message); }); }; diff --git a/client/src/util/modal.util.ts b/client/src/util/modal.util.ts index 9aaf336..efa9eb5 100644 --- a/client/src/util/modal.util.ts +++ b/client/src/util/modal.util.ts @@ -40,10 +40,10 @@ interface ModalValues { const MODAL_CREATE = 'create'; interface CheckedInputData { - newData: any; prev: string[]; next: string[]; } + export const getCheckedInputData = (type: string, inputData: ModalValues[]): CheckedInputData => { let newData = {}; const prevTodoIdList: string[] = []; @@ -78,5 +78,5 @@ export const getCheckedInputData = (type: string, inputData: ModalValues[]): Che newData = { ...newData, [id]: value }; }); - return { newData, prev: prevTodoIdList, next: nextTodoIdList }; + return { ...newData, prev: prevTodoIdList, next: nextTodoIdList }; };