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

100% Statements 87/87
75% Branches 12/16
100% Functions 26/26
100% Lines 86/86

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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212      17x     3x 3x 1x   2x         17x     2x 2x 1x   1x         17x     44x 44x 39x   5x         17x     6x 6x 3x   3x         17x     2x 2x 1x   1x         17x                                           2x     2x 3x       3x       13x     5x 5x     5x 5x 5x 5x 5x 5x 5x     5x 5x 5x 5x 5x   5x 5x     4x 4x         3x 3x     3x 3x 3x 3x 3x 3x 3x     3x 3x 3x 3x 3x   3x 3x     2x 2x         46x 46x       41x 41x     6x 6x         7x 7x             5x 5x     4x 4x         3x 3x           2x 2x     6x 6x           17x    
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { TeacherQuestionBankAPI } from "@/api/teacher/TeacherQuestionBankAPI.js";
 
export const _teacherFetchPersonalQuestions = createAsyncThunk(
    "auth/teacherFetchPersonalQuestions",
    async (query, { rejectWithValue }) => {
        try {
            const response = await TeacherQuestionBankAPI.prototype.fetchAllQuestions(query);
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data ?? { message: error.message });
        }
    }
);
 
export const _teacherFetchStaffQuestions = createAsyncThunk(
    "auth/teacherFetchStaffQuestions",
    async (query, { rejectWithValue }) => {
        try {
            const response = await TeacherQuestionBankAPI.prototype.fetchStaffQuestions(query);
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data ?? { message: error.message });
        }
    }
);
 
export const _teacherCreatePersonalQuestion = createAsyncThunk(
    "auth/teacherCreatePersonalQuestion",
    async (newQuestion, { rejectWithValue }) => {
        try {
            const response = await TeacherQuestionBankAPI.prototype.createNewQuestion(newQuestion);
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data ?? { message: error.message });
        }
    }
);
 
export const _teacherUpdatePersonalQuestion = createAsyncThunk(
    "auth/teacherUpdatePersonalQuestion",
    async (payload, { rejectWithValue }) => {
        try {
            const response = await TeacherQuestionBankAPI.prototype.updateQuestion(payload._id, payload);
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data ?? { message: error.message });
        }
    }
);
 
export const _teacherDeletePersonalQuestion = createAsyncThunk(
    "auth/teacherDeletePersonalQuestion",
    async (questionID, { rejectWithValue }) => {
        try {
            const response = await TeacherQuestionBankAPI.prototype.deleteQuestion(questionID);
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data ?? { message: error.message, detail: error.message });
        }
    }
);
 
const TeacherQuestionBank = createSlice({
    name: "TeacherQuestionBank",
    initialState: {
        questionData: [],
        _assignmentTypes: {},
        _questionTypes: {},
        _categories: {},
        _difficulties: {},
        _gradeLevels: {},
        _subjects: {},
        _pagination: {
            totalCount: null,
            pageSize: null,
            currentPage: null,
            overAllEntries: null,
            totalPages: null,
        },
        hasErrors: null,
        isLoading: false,
    },
    reducers: {
        _addNewQuestion: (state, action) => {
            state.questionData = [action.payload, ...state.questionData];
        },
        _updateQuestion: (state, action) => {
            state.questionData = state.questionData.map((question) =>
                question.id === action.payload.id ? action.payload : question
            );
        },
        _deleteQuestion: (state, action) => {
            state.questionData = state.questionData.filter((question) => question.id !== action.payload);
        },
    },
    extraReducers: (builder) => {
        builder
            // fetch all questions
            .addCase(_teacherFetchPersonalQuestions.pending, (state) => {
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_teacherFetchPersonalQuestions.fulfilled, (state, action) => {
                state.questionData = [...action.payload.data.data.questions];
                state._assignmentTypes = { ...action.payload.data.data.assignmentTypes };
                state._questionTypes = { ...action.payload.data.data.questionTypes };
                state._categories = { ...action.payload.data.data.categories };
                state._difficulties = { ...action.payload.data.data.difficulties };
                state._gradeLevels = { ...action.payload.data.data.gradeLevels };
                state._subjects = { ...action.payload.data.data.subjects };
 
                //pagination
                state._pagination.totalCount = action.payload.data.pagination.totalCount;
                state._pagination.pageSize = action.payload.data.pagination.pageSize;
                state._pagination.currentPage = action.payload.data.pagination.page;
                state._pagination.overAllEntries = action.payload.data.pagination.totalQuestions;
                state._pagination.totalPages = action.payload.data.pagination.totalPages;
 
                state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_teacherFetchPersonalQuestions.rejected, (state, action) => {
                state.isLoading = false;
                state.hasErrors = action.error.message;
            })
 
            // fetch all staff questions
            .addCase(_teacherFetchStaffQuestions.pending, (state) => {
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_teacherFetchStaffQuestions.fulfilled, (state, action) => {
                state.questionData = [...action.payload.data.data.questions];
                state._assignmentTypes = { ...action.payload.data.data.assignmentTypes };
                state._questionTypes = { ...action.payload.data.data.questionTypes };
                state._categories = { ...action.payload.data.data.categories };
                state._difficulties = { ...action.payload.data.data.difficulties };
                state._gradeLevels = { ...action.payload.data.data.gradeLevels };
                state._subjects = { ...action.payload.data.data.subjects };
 
                //pagination
                state._pagination.totalCount = action.payload.data.pagination.totalCount;
                state._pagination.pageSize = action.payload.data.pagination.pageSize;
                state._pagination.currentPage = action.payload.data.pagination.page;
                state._pagination.overAllEntries = action.payload.data.pagination.totalQuestions;
                state._pagination.totalPages = action.payload.data.pagination.totalPages;
 
                state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_teacherFetchStaffQuestions.rejected, (state, action) => {
                state.isLoading = false;
                state.hasErrors = action.error.message;
            })
 
            // create new question
            .addCase(_teacherCreatePersonalQuestion.pending, (state) => {
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_teacherCreatePersonalQuestion.fulfilled, (state, action) => {
                // state.questionData = [...state.questionData, action.payload.data.new_question];
                state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_teacherCreatePersonalQuestion.rejected, (state, action) => {
                state.isLoading = false;
                state.hasErrors = action.error.message;
            })
 
            // update question
            .addCase(_teacherUpdatePersonalQuestion.pending, (state) => {
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_teacherUpdatePersonalQuestion.fulfilled, (state) => {
                // The list is refetched after an update rather than patched in
                // place. A leftover unused `const updatedQuestion = action
                // .payload.data.updated_question` dereferenced `.data` and so
                // would THROW on a response without it; removed.
                state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_teacherUpdatePersonalQuestion.rejected, (state, action) => {
                state.isLoading = false;
                state.hasErrors = action.error.message;
            })
 
            // delete question
            .addCase(_teacherDeletePersonalQuestion.pending, (state) => {
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_teacherDeletePersonalQuestion.fulfilled, (state, action) => {
                // state.questionData = state.questionData.filter(
                //     (question) => question._id !== action.payload.data.data._id
                // );
                state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_teacherDeletePersonalQuestion.rejected, (state, action) => {
                state.isLoading = false;
                state.hasErrors =
                    action.payload?.detail || action.payload?.message || action.error?.message || "Delete failed";
            });
    },
});
 
export const { _addNewQuestion, _updateQuestion, _deleteQuestion } = TeacherQuestionBank.actions;
export default TeacherQuestionBank;