All files / src/store/slices/teacher TeacherSpecificClass.js

100% Statements 41/41
100% Branches 0/0
100% Functions 15/15
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105        11x     29x 29x 2x   27x         11x 7x 7x 4x   3x       11x 4x 4x 2x   2x       11x                 3x     2x       3x     1x 1x 1x     1x 1x 1x     1x 1x 1x         6x 6x     3x 3x     4x 4x         1x 1x     1x   1x           4x 4x         11x    
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { messageFromRejection } from "@/utils/helpers/rejectionMessage";
import { TeacherClassAPI } from "@/api/teacher/TeacherClassAPI.js";
 
export const _fetchSpecificClassByCode = createAsyncThunk(
    "fetch/specificClassByCode",
    async (classCode, { rejectWithValue }) => {
        try {
            const response = await TeacherClassAPI.prototype.fetchSpecificClassByCode(classCode);
            return response;
        } catch (error) {
            return rejectWithValue(error.response.data);
        }
    }
);
 
export const _updateClass = createAsyncThunk("update/Class", async (payload, { rejectWithValue }) => {
    try {
        const response = await TeacherClassAPI.prototype.updateClass(payload.classID, payload.classDetails);
        return response;
    } catch (error) {
        return rejectWithValue(error.response.data);
    }
});
 
export const _deleteClass = createAsyncThunk("delete/Class", async (classID, { rejectWithValue }) => {
    try {
        const response = await TeacherClassAPI.prototype.deleteClass(classID);
        return response;
    } catch (error) {
        return rejectWithValue(error.response.data);
    }
});
 
const TeacherSpecificClass = createSlice({
    name: "TeacherSpecificClass",
    initialState: {
        specificClassData: null,
        hasErrors: null,
        isLoading: false,
    },
    reducers: {
        setSpecificClassData: (state, action) => {
            state.specificClassData = { ...action.payload };
        },
        _clearErrorMessage: (state) => {
            state.hasErrors = null;
        },
    },
    extraReducers: (builder) => {
        builder
            // fetch specific class by class code
            .addCase(_fetchSpecificClassByCode.pending, (state) => {
                state.specificClassData = null;
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_fetchSpecificClassByCode.fulfilled, (state, action) => {
                state.specificClassData = { ...action.payload.data.Class };
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_fetchSpecificClassByCode.rejected, (state, action) => {
                state.specificClassData = null;
                state.hasErrors = action.error.message;
                state.isLoading = false;
            })
 
            // update class
            .addCase(_updateClass.pending, (state) => {
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_updateClass.fulfilled, (state, action) => {
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_updateClass.rejected, (state, action) => {
                state.hasErrors = action.payload.detail;
                state.isLoading = false;
            })
 
            //delete class
            .addCase(_deleteClass.pending, (state) => {
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_deleteClass.fulfilled, (state) => {
                state.hasErrors = null;
                // Keep isLoading true so Main.jsx doesn't re-render tabs (and trigger Gradebook fetch) before navigation completes
                state.isLoading = true;
            })
            .addCase(_deleteClass.rejected, (state, action) => {
                // Modified by Allan Ninal — `action.error.message` is the literal
                // string "Rejected" whenever a thunk uses rejectWithValue, so any
                // consumer rendering this showed the user that word.
                state.hasErrors = messageFromRejection(action);
                state.isLoading = false;
            });
    },
});
 
export const { setSpecificClassData, _clearErrorMessage } = TeacherSpecificClass.actions;
export default TeacherSpecificClass;