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

95.45% Statements 42/44
100% Branches 0/0
93.33% Functions 14/15
95.45% Lines 42/44

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      3x 4x 4x 2x   2x       3x 5x 5x 3x   2x       3x 5x 5x 3x   2x       3x 5x 5x 3x   2x       3x                 2x     1x 1x 1x     1x   3x     1x 1x     1x 1x 1x         1x 1x     1x 1x     1x 1x         1x 1x     1x 1x                    
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { TeacherClassAPI } from "@/api/teacher/TeacherClassAPI.js";
 
export const _fetchClassRoster = createAsyncThunk("fetch/classRoster", async (classID, { rejectWithValue }) => {
    try {
        const response = await TeacherClassAPI.prototype.fetchClassRoster(classID);
        return response;
    } catch (error) {
        return rejectWithValue(error.response.data);
    }
});
 
export const _acceptStudent = createAsyncThunk("accept/Student", async (payload, { rejectWithValue }) => {
    try {
        const response = await TeacherClassAPI.prototype.acceptStudent(payload.classID, payload.studentID);
        return response;
    } catch (error) {
        return rejectWithValue(error.response.data);
    }
});
 
export const _removeStudent = createAsyncThunk("remove/Student", async (payload, { rejectWithValue }) => {
    try {
        const response = await TeacherClassAPI.prototype.removeStudent(payload.classID, payload.studentID);
        return response;
    } catch (error) {
        return rejectWithValue(error.response.data);
    }
});
 
export const _approveLeave = createAsyncThunk("approve/Leave", async (payload, { rejectWithValue }) => {
    try {
        const response = await TeacherClassAPI.prototype.approveLeave(payload.classID, payload.studentID);
        return response;
    } catch (error) {
        return rejectWithValue(error.response.data);
    }
});
 
const TeacherClassRoster = createSlice({
    name: "TeacherClassRoster",
    initialState: {
        classRosterData: null,
        hasErrors: null,
        isLoading: false,
    },
    reducers: {},
    extraReducers: (builder) => {
        builder
            //fetch class roster
            .addCase(_fetchClassRoster.pending, (state) => {
                state.classRosterData = null;
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_fetchClassRoster.fulfilled, (state, action) => {
                state.classRosterData = [
                    ...action.payload.data.class_roster.sort((a, b) => {
                        return a.last_name.localeCompare(b.last_name);
                    }),
                ];
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_fetchClassRoster.rejected, (state, action) => {
                state.classRosterData = null;
                state.hasErrors = action.error.message;
                state.isLoading = false;
            })
 
            //accept student
            .addCase(_acceptStudent.pending, (state) => {
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_acceptStudent.fulfilled, (state) => {
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_acceptStudent.rejected, (state, action) => {
                state.hasErrors = action.error.message;
                state.isLoading = false;
            })
 
            //remove student
            .addCase(_removeStudent.pending, (state) => {
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_removeStudent.fulfilled, (state) => {
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_removeStudent.rejected, (state, action) => {
                state.hasErrors = action.error.message;
                state.isLoading = false;
            });
    },
});
 
export default TeacherClassRoster;