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

100% Statements 73/73
100% Branches 12/12
100% Functions 18/18
100% Lines 73/73

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                3x     4x 4x 2x 2x   2x         3x     8x 8x 3x 8x 1x   2x   3x   5x 5x 5x 2x   5x         3x     8x 8x 3x 8x 2x   1x   3x   5x 5x 5x 2x   5x         3x     6x 6x 5x   1x         3x                 1x     7x 7x 7x     3x 3x     4x 4x 4x         6x 6x 6x     2x 2x     4x 4x 4x         5x 5x 5x     3x 3x     2x 2x 2x         6x 6x 6x     7x     5x       7x 7x     3x 3x 3x            
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { TeacherAssignmentAPI } from "@/api/teacher/TeacherAssignmentAPI.js";
import { fetchTeacherMadeAssignmentQuota, setTeacherMadeAssignmentQuota } from "@/store/slices/teacher/TeacherFeatureFlags";
import {
    extractTeacherMadeAssignmentQuotaFromPayload,
    parseTeacherMadeAssignmentError,
} from "@/utils/features/teacherMadeAssignmentQuota";
 
export const _teacherFetchAllAssignments = createAsyncThunk(
    "teacher/fetchAllAssignments",
    async (classCode, { rejectWithValue, dispatch }) => {
        try {
            const response = await TeacherAssignmentAPI.prototype.fetchAllAssignments(classCode);
            await dispatch(fetchTeacherMadeAssignmentQuota());
            return response;
        } catch (error) {
            return rejectWithValue(error.response.data);
        }
    }
);
 
export const _teacherCreateNewAssignment = createAsyncThunk(
    "teacher/createNewAssignment",
    async (newAssignment, { rejectWithValue, dispatch }) => {
        try {
            const response = await TeacherAssignmentAPI.prototype.createAssignment(newAssignment);
            const quotaFromResponse = extractTeacherMadeAssignmentQuotaFromPayload(response?.data);
            if (quotaFromResponse) {
                dispatch(setTeacherMadeAssignmentQuota(quotaFromResponse));
            } else {
                await dispatch(fetchTeacherMadeAssignmentQuota());
            }
            return response;
        } catch (error) {
            const errorData = error.response?.data || error;
            const parsed = parseTeacherMadeAssignmentError(errorData);
            if (parsed.quota) {
                dispatch(setTeacherMadeAssignmentQuota(parsed.quota));
            }
            return rejectWithValue(errorData);
        }
    }
);
 
export const _teacherCreateStaffAssignment = createAsyncThunk(
    "teacher/createStaffAssignment",
    async (newAssignment, { rejectWithValue, dispatch }) => {
        try {
            const response = await TeacherAssignmentAPI.prototype.createStaffAssignment(newAssignment);
            const quotaFromResponse = extractTeacherMadeAssignmentQuotaFromPayload(response?.data);
            if (quotaFromResponse) {
                dispatch(setTeacherMadeAssignmentQuota(quotaFromResponse));
            } else {
                await dispatch(fetchTeacherMadeAssignmentQuota());
            }
            return response;
        } catch (error) {
            const errorData = error.response?.data || error;
            const parsed = parseTeacherMadeAssignmentError(errorData);
            if (parsed.quota) {
                dispatch(setTeacherMadeAssignmentQuota(parsed.quota));
            }
            return rejectWithValue(errorData);
        }
    }
);
 
export const _teacherUpdateAssignment = createAsyncThunk(
    "teacher/updateAssignment",
    async (payload, { rejectWithValue }) => {
        try {
            const response = await TeacherAssignmentAPI.prototype.teacherUpdateAssignment(payload._id, payload);
            return response;
        } catch (error) {
            return rejectWithValue(error.response.data);
        }
    }
);
 
const TeacherAssignments = createSlice({
    name: "TeacherAssignments",
    initialState: {
        assignmentData: [],
        hasErrors: null,
        isLoading: false,
    },
    reducers: {},
    extraReducers: (builder) => {
        builder
            // create new assignment
            .addCase(_teacherCreateNewAssignment.pending, (state) => {
                state.assignmentData = null;
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_teacherCreateNewAssignment.fulfilled, (state, action) => {
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_teacherCreateNewAssignment.rejected, (state, action) => {
                state.assignmentData = null;
                state.hasErrors = action.error.message;
                state.isLoading = false;
            })
 
            // create new staff assignment
            .addCase(_teacherCreateStaffAssignment.pending, (state) => {
                state.assignmentData = null;
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_teacherCreateStaffAssignment.fulfilled, (state, action) => {
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_teacherCreateStaffAssignment.rejected, (state, action) => {
                state.assignmentData = null;
                state.hasErrors = action.error.message;
                state.isLoading = false;
            })
 
            // update assignment
            .addCase(_teacherUpdateAssignment.pending, (state) => {
                state.assignmentData = null;
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_teacherUpdateAssignment.fulfilled, (state, action) => {
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_teacherUpdateAssignment.rejected, (state, action) => {
                state.assignmentData = null;
                state.hasErrors = action.error.message;
                state.isLoading = false;
            })
 
            // fetch all assignments
            .addCase(_teacherFetchAllAssignments.pending, (state) => {
                state.assignmentData = null;
                state.hasErrors = null;
                state.isLoading = true;
            })
            .addCase(_teacherFetchAllAssignments.fulfilled, (state, action) => {
                state.assignmentData = JSON.parse(
                    JSON.stringify(
                        action.payload.data.assignments.sort((a, b) => {
                            return b.created_at.localeCompare(a.created_at);
                        })
                    )
                );
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_teacherFetchAllAssignments.rejected, (state, action) => {
                state.assignmentData = null;
                state.hasErrors = action.error.message;
                state.isLoading = false;
            });
    },
});
 
export default TeacherAssignments;