All files / src/store/slices/student StudentSearchClass.js

100% Statements 29/29
100% Branches 0/0
100% Functions 10/10
100% Lines 28/28

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                          7x   5x 14x 14x 9x   5x       5x     4x 4x 2x   2x         5x                 3x   11x 11x 11x     8x 8x 8x     7x 7x 7x         3x 3x     2x 2x     2x 2x            
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { StudentClassAPI } from "@/api/student/StudentClassAPI.js";
import { DEFAULT_ACTION_ERROR, messageFromRejection } from "@/utils/helpers/rejectionMessage";
 
// Re-exported for callers that already import them from here; the logic moved to a
// shared helper once the teacher's class roster needed the same treatment.
export { DEFAULT_ACTION_ERROR as DEFAULT_CLASS_ERROR, messageFromRejection };
 
/**
 * Keep whatever the server said. A transport failure has no response at all, and
 * `error.response.data` on one is exactly what used to throw — the student then saw
 * the TypeError's text. With no body, messageFromRejection supplies the default.
 */
const rejectionBody = (error) => error?.response?.data;
 
export const _searchClassByCode = createAsyncThunk("search/ClassByCode", async (classCode, { rejectWithValue }) => {
    try {
        const response = await StudentClassAPI.prototype.searchClassByCode(classCode);
        return response;
    } catch (error) {
        return rejectWithValue(rejectionBody(error));
    }
});
 
export const _enrollClassByClassCode = createAsyncThunk(
    "enroll/classByClassCode",
    async (classCode, { rejectWithValue }) => {
        try {
            const response = await StudentClassAPI.prototype.enrollClassByClassCode(classCode);
            return response;
        } catch (error) {
            return rejectWithValue(rejectionBody(error));
        }
    }
);
 
const StudentSearchClass = createSlice({
    name: "StudentSearchClass",
    initialState: {
        classSearchData: null,
        hasErrors: null,
        isLoading: false,
    },
    reducers: {},
    extraReducers: (builder) => {
        builder
            .addCase(_searchClassByCode.pending, (state) => {
                state.classSearchData = null;
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_searchClassByCode.fulfilled, (state, action) => {
                state.classSearchData = { ...action.payload.data.Class };
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_searchClassByCode.rejected, (state, action) => {
                state.classSearchData = null;
                state.hasErrors = messageFromRejection(action);
                state.isLoading = false;
            })
 
            //enroll class by class code
            .addCase(_enrollClassByClassCode.pending, (state) => {
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_enrollClassByClassCode.fulfilled, (state) => {
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_enrollClassByClassCode.rejected, (state, action) => {
                state.hasErrors = messageFromRejection(action);
                state.isLoading = false;
            });
    },
});
 
export default StudentSearchClass;