From 650bb7bd1d47ba106c3d2a05e64322e9f0bfc2f8 Mon Sep 17 00:00:00 2001 From: bedrockdude10 Date: Thu, 28 Mar 2024 11:32:10 -0400 Subject: [PATCH] feat: added new task and edit task mutations, also added bones of task details screen --- client/screens/AddTaskDetails.tsx | 120 ++++++++++++++++++++++++++++++ client/services/task.ts | 50 ++++++++++++- 2 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 client/screens/AddTaskDetails.tsx diff --git a/client/screens/AddTaskDetails.tsx b/client/screens/AddTaskDetails.tsx new file mode 100644 index 0000000..a4feae9 --- /dev/null +++ b/client/screens/AddTaskDetails.tsx @@ -0,0 +1,120 @@ +import React, { useState } from 'react'; +import { + ActivityIndicator, + Pressable, + Text, + TextInput, + View +} from 'react-native'; + +import { useNavigation } from '@react-navigation/native'; + +import { PopupModal } from '../components/PopupModal'; +import { AppStackNavigation } from '../navigation/types'; +import { useTaskByAssigned } from '../services/task'; +import { Task } from '../types/task'; + +// const currentUserIDs = [useCareWalletContext().user.userID]; +const currentUserIDs = ['0']; + +export default function AddNewTask() { + const navigation = useNavigation(); // Initialize navigation + + const [newTaskState, setNewTaskState] = useState({ + task_id: 0, + task_title: '', + group_id: 0, + created_by: '', + created_date: '', + start_date: '', + end_date: '', + notes: '', + task_status: '', + task_type: '' + }); + + const [addingTask, setAddingTask] = useState(false); + console.log(addingTask); + const { addTaskMutation: addNewTaskMutation } = + useTaskByAssigned(currentUserIDs); + + const handleAddTask = async () => { + setAddingTask(true); + await addNewTaskMutation(newTaskState); + setAddingTask(false); + navigation.navigate('TaskList'); // Navigate back to TaskList screen + }; + + // State to manage the visibility of the modal + const [modalVisible, setModalVisible] = useState(false); + + return ( + + + + + Adding Task... + + + Task Details + + + + + Task ID: + + + setNewTaskState({ ...newTaskState, task_id: parseInt(val) || 0 }) + } + keyboardType="numeric" + /> + + + {/* Add input fields for other task fields (start_date, end_date, notes, task_status, task_type) similarly */} + + Add Task + + + + ); +} diff --git a/client/services/task.ts b/client/services/task.ts index 68744b3..0acfd65 100644 --- a/client/services/task.ts +++ b/client/services/task.ts @@ -1,4 +1,4 @@ -import { useQuery } from '@tanstack/react-query'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import axios from 'axios'; import { TaskLabel } from '../types/label'; @@ -45,6 +45,16 @@ const getTaskLabels = async (taskID: string): Promise => { return data; }; +const addNewTask = async (newTask: Task): Promise => { + const response = await axios.post(`${api_url}/tasks`, newTask); + return response.data; +}; + +const editTask = async (taskID: string, updatedTask: Task): Promise => { + const response = await axios.put(`${api_url}/tasks/${taskID}`, updatedTask); + return response.data; +}; + export const useFilteredTasks = (queryParams: TaskQueryParams) => { const { data: tasks, isLoading: tasksIsLoading } = useQuery({ queryKey: ['filteredTaskList', queryParams], @@ -87,3 +97,41 @@ export const useTaskById = (taskId: string) => { taskLabelsIsLoading }; }; + +export const addNewTaskMutation = () => { + const queryClient = useQueryClient(); + + const { mutate: addTaskMutation } = useMutation({ + mutationFn: (newTask: Task) => addNewTask(newTask), + onSuccess: () => { + queryClient.invalidateQueries('filteredTaskList'); + }, + onError: (err) => { + console.error('ERROR: Failed to Add Task. Code:', err); + } + }); + + return addTaskMutation; +}; + +export const editTaskMutation = () => { + const queryClient = useQueryClient(); + + const { mutate: editTaskMutation } = useMutation({ + mutationFn: ({ + taskId, + updatedTask + }: { + taskId: string; + updatedTask: Task; + }) => editTask(taskId, updatedTask), + onSuccess: () => { + queryClient.invalidateQueries('filteredTaskList'); + }, + onError: (err) => { + console.error('ERROR: Failed to Edit Task. Code:', err); + } + }); + + return editTaskMutation; +};