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

92.59% Statements 50/54
90% Branches 18/20
95.65% Functions 22/23
91.83% Lines 45/49

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      12x     4x 4x 2x   2x         12x                                                               1x     1x     38x 38x   23x   15x     38x     5x     1x     1x     7x       2x 1x 2x       2x 1x 2x     1x     1x     1x                   3x               2x 2x               1x                   9x     2x 2x 2x                 2x     2x 2x 2x 2x 2x 2x     1x 1x 1x                                         12x    
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { StudentAssignmentAPI } from "@/api/student/StudentAssignmentAPI.js";
 
export const _studentFetchAssignmentQuestions = createAsyncThunk(
    "student/fetchAssignmentQuestions",
    async (assignmentId, { rejectWithValue }) => {
        try {
            const response = await StudentAssignmentAPI.prototype.fetchSpecificAssignmentQuestions(assignmentId);
            return response;
        } catch (error) {
            return rejectWithValue(error.response.data);
        }
    }
);
 
const StudentQuestionPage = createSlice({
    name: "StudentQuestionPage",
    initialState: {
        assignmentQuestions: [],
        myAnswers: {
            isShow: true,
            data: [],
            version: 0,
        },
        remainingTime: null,
        settings: {
            layout: "2",
            showTotalAnswers: true,
            showTimer: true,
            questionTextSize: 20,
            choiceTextSize: 15,
            voice: {
                speed: 1,
                volume: 1,
            },
        },
        hasErrors: null,
        isLoading: false,
        // Set once by LockdownGuard's onThresholdExceeded when the backend reports the
        // browser-lockdown violation threshold exceeded (see
        // docs/implementation/browser_lockdown_violation_tracking.md). AnswerTracker.jsx
        // watches this the same way it watches the close-time countdown, to auto-submit
        // and lock the student out.
        violationLockout: { triggered: false, violationCount: 0, maxViolations: 3 },
    },
    reducers: {
        _updateQuestionsData: (state, action) => {
            state.assignmentQuestions = [...action.payload];
        },
        _updateMyAnswersData: (state, action) => {
            state.myAnswers.data = [...action.payload];
        },
        _addMyAnswer: (state, action) => {
            const index = state.myAnswers.data.findIndex((item) => item.questionId === action.payload.questionId);
            if (index !== -1) {
                // Direct mutation works with Immer
                state.myAnswers.data[index].answer = action.payload.answer;
            } else {
                state.myAnswers.data.push(action.payload);
            }
            // Increment version to trigger re-render
            state.myAnswers.version += 1;
        },
        _updateQuestionFlag: (state, action) => {
            state.myAnswers.data[action.payload].isFlagged = !state.myAnswers.data[action.payload].isFlagged;
        },
        _updateShowMyAnswers: (state) => {
            state.myAnswers.isShow = !state.myAnswers.isShow;
        },
        _updateLayout: (state, action) => {
            state.settings.layout = action.payload;
        },
        _updateShowTimer: (state) => {
            state.settings.showTimer = !state.settings.showTimer;
        },
        _increaseTextSize: (state, action) => {
            // 30 is the maximum size
            if (state.settings.questionTextSize !== 30)
                state.settings.questionTextSize = state.settings.questionTextSize + 2;
            if (state.settings.choiceTextSize !== 25) state.settings.choiceTextSize = state.settings.choiceTextSize + 2;
        },
        _decreaseTextSize: (state, action) => {
            // 20 is the minimum size
            if (state.settings.questionTextSize !== 20)
                state.settings.questionTextSize = state.settings.questionTextSize - 2;
            if (state.settings.choiceTextSize !== 15) state.settings.choiceTextSize = state.settings.choiceTextSize - 2;
        },
        _updateVoiceSpeed: (state, action) => {
            state.settings.voice.speed = action.payload;
        },
        _updateVoiceVolume: (state, action) => {
            state.settings.voice.volume = action.payload;
        },
        _setRemainingTime: (state, action) => {
            state.remainingTime = action.payload;
        },
        _restoreQuestionPageSession: (state, action) => {
            state.assignmentQuestions = action.payload.assignmentQuestions;
            state.myAnswers.data = action.payload.myAnswersData;
            if (action.payload.settings) {
                state.settings = { ...state.settings, ...action.payload.settings };
            }
        },
        _triggerViolationLockout: (state, action) => {
            state.violationLockout = {
                triggered: true,
                violationCount: action.payload?.violationCount ?? state.violationLockout.violationCount,
                maxViolations: action.payload?.maxViolations ?? state.violationLockout.maxViolations,
            };
        },
        _generateAnswersModel: (state, action) => {
            // for teacher student-preview
            if (action.payload) {
                state.myAnswers.data = action.payload.map((question) => ({
                    questionId: question._id,
                    questionType: question.questionType.toLowerCase(),
                    isFlagged: false,
                    answer: "",
                }));
                // for student preview
            } else {
                state.myAnswers.data = state.assignmentQuestions.map((question) => ({
                    questionId: question._id,
                    questionType: question.questionType,
                    isFlagged: false,
                    studentAnswer: "",
                }));
            }
        },
    },
    extraReducers: (builder) => {
        builder
            //fetch assignment questions
            .addCase(_studentFetchAssignmentQuestions.pending, (state) => {
                state.assignmentQuestions = null;
                state.hasErrors = null;
                state.isLoading = true;
                // A fresh attempt is starting to load — whatever violationLockout was
                // left over from a PREVIOUS assignment (or a previous attempt of this
                // same one) must not carry into it. Redux state persists across
                // client-side navigation between assignments (unlike a real page
                // refresh, which recreates the store from its default state), so
                // without this reset, a student who ever tripped the lockout once in
                // this browser tab would have every assignment they open afterward
                // auto-submitted on mount, with zero actual violations against it.
                state.violationLockout = { triggered: false, violationCount: 0, maxViolations: 3 };
            })
            .addCase(_studentFetchAssignmentQuestions.fulfilled, (state, action) => {
                state.assignmentDetails = { ...action.payload.data.assignment.details };
                state.assignmentQuestions = [...action.payload.data.assignment.questions];
                state.myAnswers.data = [...(action.payload.data.assignment.student_answers ?? [])];
                state.remainingTime = action.payload.data.assignment.remaining_time;
                state.hasErrors = null;
                state.isLoading = false;
            })
            .addCase(_studentFetchAssignmentQuestions.rejected, (state, action) => {
                state.assignmentQuestions = null;
                state.hasErrors = action.error.message;
                state.isLoading = false;
            });
    },
});
 
export const {
    _updateQuestionsData,
    _updateMyAnswersData,
    _addMyAnswer,
    _updateQuestionFlag,
    _updateShowMyAnswers,
    _updateLayout,
    _updateShowTimer,
    _increaseTextSize,
    _decreaseTextSize,
    _updateVoiceSpeed,
    _updateVoiceVolume,
    _generateAnswersModel,
    _setRemainingTime,
    _restoreQuestionPageSession,
    _triggerViolationLockout,
} = StudentQuestionPage.actions;
export default StudentQuestionPage;