const filterUpdate: UpdateArgument = (task, view) => {
let updateData: TaskType[];
if (view === "ALL") {
updateData = task;
} else {
updateData = task.filter((item) => item.type === view);
}
return updateData;
};
const useTask = create<StoreType>((set) => ({
task: null,
filteredTask: null,
view: "ALL",
//? SERVER
loadTask: async () => {
try {
const { token } = await JSON.parse(localStorage.getItem("token") as string);
const data = await axios.post("http://localhost:3001/task", token);
if (data.status === 204) return;
set({ task: data.data, filteredTask: data.data });
} catch (error) {
return console.log(error);
}
},
postTask: async (task) => {
try {
const { token } = await JSON.parse(localStorage.getItem("token") as string);
await axios.post("http://localhost:3001/update", { token, task: task });
} catch (error) {
console.log(error);
}
},
//? LOGOUT
logoutTask: () => {
set({ task: null, filteredTask: null });
localStorage.removeItem("token");
},
//? CRUD
viewTask: (view: string) => {
set((state) => {
if (view === "ALL") {
return {
...state,
filteredTask: state.task,
view: view,
};
} else {
return {
...state,
view: view,
filteredTask: state.task!.filter((item) => view === item.type),
};
}
});
},
editTask: (id, form) => {
set((state) => {
(...)
return {
...state,
task: editedTask,
filteredTask: updateTask,
};
});
},
checkTask: (id: string) => {
set((state) => {
(...)
return {
...state,
task: checkedTask,
filteredTask: updateTask,
};
});
},
deleteTask: (id: string) => {
set((state) => {
(...)
return {
...state,
task: deletedTask,
filteredTask: updateTask,
};
});
},
deleteAdminTask: (id: string, name: string) => {
set((state) => {
(...)
return {
...state,
task: deletedTask,
filteredTask: updateTask,
};
});
},
createTask: (form) => {
set((state) => {
(...)
return {
...state,
task: updatedTasks,
};
});
},
}));