generated from TetiZ/vite-react-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |