diff --git a/frontend/src/app/tasks/__tests__/components/CreateTask.test.tsx b/frontend/src/app/tasks/__tests__/components/CreateTask.test.tsx index 1bbe2de8d..2b53f089d 100644 --- a/frontend/src/app/tasks/__tests__/components/CreateTask.test.tsx +++ b/frontend/src/app/tasks/__tests__/components/CreateTask.test.tsx @@ -279,7 +279,7 @@ describe('TaskForm', () => { await user.click(screen.getByRole('button', { name: 'Save' })) - expect(addTask).toHaveBeenCalledWith({ + expect(addTask.mock.lastCall[0].task).toEqual({ date: '2023-01-01', description: 'description!', endTime: '13:00', @@ -315,7 +315,7 @@ describe('TaskForm', () => { await user.keyboard('{Control>}s') - expect(addTask).toHaveBeenCalledWith({ + expect(addTask.mock.lastCall[0].task).toEqual({ date: '2023-01-01', description: 'description!', endTime: '13:00', @@ -351,7 +351,7 @@ describe('TaskForm', () => { await user.keyboard('{Enter}') - expect(addTask).toHaveBeenCalledWith({ + expect(addTask.mock.lastCall[0].task).toEqual({ date: '2023-01-01', description: 'description!', endTime: '13:00', diff --git a/frontend/src/app/tasks/components/CreateTask.tsx b/frontend/src/app/tasks/components/CreateTask.tsx index 2399e8884..59f255e7d 100644 --- a/frontend/src/app/tasks/components/CreateTask.tsx +++ b/frontend/src/app/tasks/components/CreateTask.tsx @@ -21,8 +21,16 @@ type CreateTaskProps = { } export const CreateTask = ({ projects, taskTypes, templates }: CreateTaskProps) => { - const { task, handleChange, resetForm, handleSubmit, formRef, selectTemplate, template } = - useCreateTaskForm() + const { + task, + handleChange, + resetForm, + handleSubmit, + formRef, + selectTemplate, + template, + isLoading + } = useCreateTaskForm() const { loggedTime, toggleTimer, isTimerRunning } = useTaskFormTimer({ handleChange, startTime: task.startTime, @@ -160,8 +168,8 @@ export const CreateTask = ({ projects, taskTypes, templates }: CreateTaskProps) - diff --git a/frontend/src/app/tasks/hooks/useCreateTask.ts b/frontend/src/app/tasks/hooks/useCreateTask.ts index 4ec264837..6f667e457 100644 --- a/frontend/src/app/tasks/hooks/useCreateTask.ts +++ b/frontend/src/app/tasks/hooks/useCreateTask.ts @@ -15,20 +15,24 @@ export const useCreateTask = () => { const { id: userId } = useGetCurrentUser() const { tasks } = useGetTasks() - const { mutate } = useMutation((task: TaskIntent) => createTask(task, tasks), { - onSuccess: () => { - queryClient.invalidateQueries(['tasks', userId]) - showSuccess('Task added succesfully') - }, - onError: (e) => { - if (e instanceof BaseError) { - showError(e.message) - return - } + const { mutate, isLoading } = useMutation( + ({ task }: { task: TaskIntent; handleSuccess: () => void }) => createTask(task, tasks), + { + onSuccess: (_, { handleSuccess }) => { + handleSuccess() + queryClient.invalidateQueries(['tasks', userId]) + showSuccess('Task added succesfully') + }, + onError: (e) => { + if (e instanceof BaseError) { + showError(e.message) + return + } - throw e + throw e + } } - }) + ) - return { addTask: mutate } + return { addTask: mutate, isLoading } } diff --git a/frontend/src/app/tasks/providers/CreateTaskFormProvider.tsx b/frontend/src/app/tasks/providers/CreateTaskFormProvider.tsx index b464d486d..98cdc9d7f 100644 --- a/frontend/src/app/tasks/providers/CreateTaskFormProvider.tsx +++ b/frontend/src/app/tasks/providers/CreateTaskFormProvider.tsx @@ -19,13 +19,14 @@ type CreateTaskFormContext = { selectTemplate: (template: Template | null) => void template: Template | null cloneTask: (task: Task) => void + isLoading: boolean } export const CreateTaskFormContext = createContext({} as CreateTaskFormContext) export const CreateTaskFormProvider = ({ children }: PropsWithChildren) => { const { id: userId } = useGetCurrentUser() - const { addTask } = useCreateTask() + const { addTask, isLoading } = useCreateTask() const [template, setTemplate] = useState