Skip to content

Commit

Permalink
Create editTask api call
Browse files Browse the repository at this point in the history
This creates the api function and the hook to use in the components
  • Loading branch information
negreirosleo committed Dec 13, 2023
1 parent ba1774c commit 71bdcf4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
29 changes: 29 additions & 0 deletions frontend/src/app/tasks/hooks/useTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { deleteTask } from '@/infra/task/deleteTask'
import { useClientFetch } from '@/infra/lib/useClientFetch'
import { format } from 'date-fns'
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser/useGetCurrentUser'
import { editTask } from '@/infra/task/editTask'

export const useGetTasks = () => {
const apiClient = useClientFetch()
Expand Down Expand Up @@ -61,3 +62,31 @@ export const useDeleteTask = () => {

return { deleteTask: mutate }
}

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 }
}
28 changes: 28 additions & 0 deletions frontend/src/infra/task/editTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Task } from '@/domain/Task'
import { ApiClient } from '../lib/apiClient'

export const editTask = async (task: Task, apiClient: ApiClient): Promise<Task> => {
return apiClient(`/v1/timelog/tasks/${task.id}`, {
method: 'PUT',
body: JSON.stringify({
user_id: task.userId,
project_id: task.projectId,
story: task.story,
description: task.description,
task_type: task.taskType,
start_time: task.startTime,
end_time: task.endTime,
date: task.date
})
})
.then((response) => {
if (!response.ok) {
throw Error(response.statusText)
}
return response
})
.then((response) => response.json() as Promise<Task>)
.catch((e) => {
throw new Error(e)
})
}

0 comments on commit 71bdcf4

Please sign in to comment.