From 682d2dd98b6d6a304dc48d9f95714c256ff7c5cd Mon Sep 17 00:00:00 2001 From: miyu-di <141654479+miyu-di@users.noreply.github.com> Date: Fri, 14 Jun 2024 00:56:14 +0300 Subject: [PATCH] tasks redux --- src/redux/store.js | 2 ++ src/redux/tasks/operations.js | 40 ++++++++++++++++++++++ src/redux/tasks/selectors.js | 5 +++ src/redux/tasks/slice.js | 64 +++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 src/redux/tasks/operations.js create mode 100644 src/redux/tasks/selectors.js create mode 100644 src/redux/tasks/slice.js diff --git a/src/redux/store.js b/src/redux/store.js index dcdae5b..5fabdcc 100644 --- a/src/redux/store.js +++ b/src/redux/store.js @@ -12,6 +12,7 @@ import { import { boardsReducer } from "./boards/slice"; import { authReducer } from "./auth/slice"; import { columnReducer } from "./columns/slice"; +import { tasksReducer } from "./tasks/slice"; import storage from "redux-persist/lib/storage"; const authPersistConfig = { @@ -27,6 +28,7 @@ export const store = configureStore({ boards: boardsReducer, auth: persistedAuthReducer, column: columnReducer, + tasks: tasksReducer, }, middleware: (getDefaultMiddleware) => getDefaultMiddleware({ diff --git a/src/redux/tasks/operations.js b/src/redux/tasks/operations.js new file mode 100644 index 0000000..ef47e5f --- /dev/null +++ b/src/redux/tasks/operations.js @@ -0,0 +1,40 @@ +import axios from "axios"; +import { createAsyncThunk } from "@reduxjs/toolkit"; + +axios.defaults.baseURL = "https://project06back.onrender.com"; + +export const fetchTasks = createAsyncThunk("/tasks/getAll", async ({deskId, columnId}, thunkAPI) => { + try { + const response = await axios.get(`/home/${deskId}/columns/${columnId}/tasks`); + return response.data; + } catch (e) { + return thunkAPI.rejectWithValue(e.message); + } +}); + +export const addTask = createAsyncThunk("/tasks/addTask", async ({deskId, columnId}, thunkAPI) => { + try { + const response = await axios.post(`/home/${deskId}/columns/${columnId}/tasks`); + return response.data; + } catch (e) { + return thunkAPI.rejectWithValue(e.message); + } +}); + +export const updateTask = createAsyncThunk("/tasks/updateTask", async ({deskId, columnId, taskId}, thunkAPI)=> { + try { + const response = await axios.put(`/home/${deskId}/columns/${columnId}/tasks/${taskId}`); + return response.data; + } catch (e) { + return thunkAPI.rejectWithValue(e.message); + } +}) + +export const deleteTask = createAsyncThunk("/tasks/deleteTask", async ({deskId, columnId, taskId}, thunkAPI) => { + try { + const response = await axios.delete(`/home/${deskId}/columns/${columnId}/tasks/${taskId}`); + return response.data; + } catch (e) { + return thunkAPI.rejectWithValue(e.message); + } +}) \ No newline at end of file diff --git a/src/redux/tasks/selectors.js b/src/redux/tasks/selectors.js new file mode 100644 index 0000000..7c19a14 --- /dev/null +++ b/src/redux/tasks/selectors.js @@ -0,0 +1,5 @@ +export const selectTasks = (state) => state.tasks.items; + +export const selectLoading = (state) => state.tasks.loading; + +export const selectError = (state) => state.tasks.error; \ No newline at end of file diff --git a/src/redux/tasks/slice.js b/src/redux/tasks/slice.js new file mode 100644 index 0000000..0fe32ca --- /dev/null +++ b/src/redux/tasks/slice.js @@ -0,0 +1,64 @@ +import { createSlice } from "@reduxjs/toolkit"; +import { addTask, fetchTasks, updateTask, deleteTask } from "./operations"; + +const taskSlice = createSlice({ + name: "tasks", + initialState: { items: [], loading: false, error: null }, + + extraReducers: (builder) => + builder + .addCase(fetchTasks.pending, (state) => { + state.loading = true; + }) + .addCase(fetchTasks.fulfilled, (state, action) => { + state.loading = false; + state.error = null; + state.items = action.payload; + }) + .addCase(fetchTasks.rejected, (state, action) => { + state.loading = false; + state.error = action.payload; + }) + .addCase(addTask.pending, (state) => { + state.loading = true; + }) + .addCase(addTask.fulfilled, (state, action) => { + state.loading = false; + state.error = null; + state.items = action.payload; + }) + .addCase(addTask.rejected, (state, action) => { + state.loading = false; + state.error = action.payload; + }) + .addCase(updateTask.pending, (state) => { + state.loading = true; + }) + .addCase(updateTask.fulfilled, (state, action) => { + state.loading = false; + state.error = null; + state.items = state.items.map(task => + task.id === action.payload.id ? action.payload : task + ); + }) + .addCase(updateTask.rejected, (state, action) => { + state.loading = false; + state.error = action.payload; + }) + .addCase(deleteTask.pending, (state) => { + state.loading = true; + }) + .addCase(updateTask.fulfilled, (state, action) => { + state.loading = false; + state.error = null; + state.items = state.items.filter( + (task) => task.id !== action.payload.id + ); + }) + .addCase(updateTask.rejected, (state, action) => { + state.loading = false; + state.error = action.payload; + }) +}) + +export const tasksReducer = taskSlice.reducer; \ No newline at end of file