Skip to content

Commit

Permalink
Move deleteTask to factory pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
negreirosleo committed Jan 26, 2024
1 parent 4ebf320 commit 5443e6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions frontend/src/app/tasks/hooks/useDeleteTask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { makeDeleteTask } from '@/infra/task/deleteTask'
import { useClientFetch } from '@/infra/lib/useClientFetch'
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser/useGetCurrentUser'

Expand All @@ -10,8 +10,9 @@ export const useDeleteTask = () => {
const queryClient = useQueryClient()
const { id: userId } = useGetCurrentUser()
const { showError, showSuccess } = useAlert()
const deleteTask = makeDeleteTask(apiClient)

const { mutate } = useMutation((taskId: number) => deleteTask(taskId, apiClient), {
const { mutate } = useMutation((taskId: number) => deleteTask(taskId), {
onSuccess: (_, taskId) => {
queryClient.setQueryData<Array<Task>>(['tasks', userId], (prevData) =>
prevData!.filter((prevData) => prevData.id !== taskId)
Expand Down
23 changes: 13 additions & 10 deletions frontend/src/infra/task/deleteTask.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { ApiClient } from '../lib/apiClient'
import { Task } from '@/domain/Task'

export const makeDeleteTask =
(apiClient: ApiClient) =>
async (taskId: Task['id']): Promise<string> => {
try {
const response = await apiClient(`/v1/timelog/tasks/${taskId}`, {
method: 'DELETE'
})

export const deleteTask = async (taskId: number, apiClient: ApiClient): Promise<string> => {
return apiClient(`/v1/timelog/tasks/${taskId}`, {
method: 'DELETE'
})
.then((response) => {
if (!response.ok) {
throw Error(response.statusText)
}

return response.statusText
})
.catch((e) => {
throw new Error(e)
})
}
} catch (e) {
throw e
}
}

0 comments on commit 5443e6d

Please sign in to comment.