-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break useTask into 4 different files
THis was done to place each hook on its own file, a file called useTask should only exist if there's a hook called useTask to go with it, otherwise it makes more sense to have everything broken apart.
- Loading branch information
1 parent
75ad87b
commit dc62a82
Showing
12 changed files
with
158 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
frontend/src/app/tasks/__tests__/components/EditTask.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { TaskIntent } from '@/domain/Task' | ||
import { useAlert } from '@/ui/Alert/useAlert' | ||
import { createTask } from '@/infra/task/createTask' | ||
import { useClientFetch } from '@/infra/lib/useClientFetch' | ||
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser/useGetCurrentUser' | ||
|
||
export const useCreateTask = () => { | ||
const apiClient = useClientFetch() | ||
const queryClient = useQueryClient() | ||
const { showError, showSuccess } = useAlert() | ||
const { id: userId } = useGetCurrentUser() | ||
|
||
const { mutate } = useMutation((task: TaskIntent) => createTask(task, apiClient), { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries(['tasks', userId]) | ||
showSuccess('Task added succesfully') | ||
}, | ||
onError: () => { | ||
showError('Failed to add task') | ||
} | ||
}) | ||
|
||
return { addTask: mutate } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { Task } from '@/domain/Task' | ||
import { useAlert } from '@/ui/Alert/useAlert' | ||
import { deleteTask } from '@/infra/task/deleteTask' | ||
import { useClientFetch } from '@/infra/lib/useClientFetch' | ||
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser/useGetCurrentUser' | ||
|
||
export const useDeleteTask = () => { | ||
const apiClient = useClientFetch() | ||
const queryClient = useQueryClient() | ||
const { id: userId } = useGetCurrentUser() | ||
const { showError, showSuccess } = useAlert() | ||
|
||
const { mutate } = useMutation((taskId: number) => deleteTask(taskId, apiClient), { | ||
onSuccess: (_, taskId) => { | ||
queryClient.setQueryData<Array<Task>>(['tasks', userId], (prevData) => | ||
prevData!.filter((prevData) => prevData.id !== taskId) | ||
) | ||
showSuccess('Task succesfully removed') | ||
}, | ||
onError: () => { | ||
showError('Failed to remove task') | ||
} | ||
}) | ||
|
||
return { deleteTask: mutate } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { Task } from '@/domain/Task' | ||
import { useAlert } from '@/ui/Alert/useAlert' | ||
import { useClientFetch } from '@/infra/lib/useClientFetch' | ||
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser/useGetCurrentUser' | ||
import { editTask } from '@/infra/task/editTask' | ||
|
||
export const useEditTask = ({ handleSuccess }: { handleSuccess: () => void }) => { | ||
const apiClient = useClientFetch() | ||
const { showError, showSuccess } = useAlert() | ||
const { id: userId } = useGetCurrentUser() | ||
const queryClient = useQueryClient() | ||
|
||
const { mutate } = useMutation((task: Task) => editTask(task, apiClient), { | ||
onSuccess: (data) => { | ||
queryClient.setQueryData<Array<Task>>(['tasks', userId], (prevData) => | ||
prevData!.map((task) => { | ||
if (task.id === data.id) { | ||
return data | ||
} | ||
|
||
return task | ||
}) | ||
) | ||
showSuccess('Task succesfully edited') | ||
handleSuccess() | ||
}, | ||
onError: () => { | ||
showError('Failed to edit task') | ||
} | ||
}) | ||
|
||
return { editTask: mutate } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { getTasks } from '@/infra/task/getTasks' | ||
import { useClientFetch } from '@/infra/lib/useClientFetch' | ||
import { format } from 'date-fns' | ||
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser/useGetCurrentUser' | ||
|
||
export const useGetTasks = () => { | ||
const apiClient = useClientFetch() | ||
const { id: userId } = useGetCurrentUser() | ||
const today = format(new Date(), 'yyyy-MM-dd') | ||
|
||
const { data } = useQuery({ | ||
queryKey: ['tasks', userId], | ||
queryFn: () => getTasks(apiClient, { userId, start: today, end: today }), | ||
initialData: [] | ||
}) | ||
|
||
return { tasks: data } | ||
} |
Oops, something went wrong.