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 | 3x 3x 3x 3x 5x 5x 1x 3x 3x 3x 22x 13x 13x 12x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 3x | import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { TeacherQuestionImportAPI } from "@/api/teacher/TeacherQuestionImportAPI.js";
/**
* State for a bulk question import.
*
* The reviewed questions live here and are edited in place, then sent back on commit.
* That matters: what gets saved must be what the teacher saw and accepted, not what the
* parser originally proposed.
*
* Developer: Allan Ninal
*/
export const _teacherUploadQuestionFile = createAsyncThunk(
"questionImport/upload",
async ({ file, questionType, gradeLevel = null, difficulty = null }, { rejectWithValue }) => {
try {
const response = await TeacherQuestionImportAPI.prototype.upload(file, questionType, {
gradeLevel,
difficulty,
});
return response.data;
} catch (error) {
return rejectWithValue(error.response?.data ?? { message: error.message });
}
}
);
export const _teacherPasteQuestion = createAsyncThunk(
"questionImport/paste",
async ({ text, questionType }, { rejectWithValue }) => {
try {
const response = await TeacherQuestionImportAPI.prototype.text(text, questionType);
return response.data;
} catch (error) {
return rejectWithValue(error.response?.data ?? { message: error.message });
}
}
);
export const _teacherFetchImportStatus = createAsyncThunk(
"questionImport/status",
async (jobId, { rejectWithValue }) => {
try {
const response = await TeacherQuestionImportAPI.prototype.status(jobId);
return response.data;
} catch (error) {
return rejectWithValue(error.response?.data ?? { message: error.message });
}
}
);
export const _teacherFetchImportQuestions = createAsyncThunk(
"questionImport/questions",
async (jobId, { rejectWithValue }) => {
try {
const response = await TeacherQuestionImportAPI.prototype.questions(jobId);
return response.data;
} catch (error) {
return rejectWithValue(error.response?.data ?? { message: error.message });
}
}
);
export const _teacherCommitImport = createAsyncThunk(
"questionImport/commit",
async ({ jobId, questions, questionSubject, questionTopic, category }, { rejectWithValue }) => {
try {
const response = await TeacherQuestionImportAPI.prototype.commit(jobId, {
questions,
question_subject: questionSubject,
question_topic: questionTopic,
category,
});
return response.data;
} catch (error) {
return rejectWithValue(error.response?.data ?? { message: error.message });
}
}
);
const initialState = {
jobId: null,
state: null, // queued | scanning | parsing | extracting | awaiting_review | committed | failed
filename: null,
progress: 0,
total: 0,
questionsFound: 0,
questionsNeedingReview: 0,
rowErrors: [],
questions: [],
// How many of the reviewed questions already exist in this teacher's bank.
// null means the check could not run — deliberately distinct from 0, which would
// read as "nothing is a duplicate".
duplicates: 0,
// Never true before a successful commit. The review screen reads this to keep
// saying "nothing has been saved yet".
persisted: false,
committed: null,
uploading: false,
loading: false,
committing: false,
error: null,
};
const teacherQuestionImportSlice = createSlice({
name: "teacherQuestionImport",
initialState,
reducers: {
resetImport: () => initialState,
/** Apply a teacher's edit to one reviewed question, in place. */
updateReviewedQuestion: (state, action) => {
const { index, changes } = action.payload;
if (state.questions[index]) {
state.questions[index] = { ...state.questions[index], ...changes };
}
},
/** Drop a question from the batch without touching the rest. */
removeReviewedQuestion: (state, action) => {
state.questions.splice(action.payload, 1);
},
},
extraReducers: (builder) => {
builder
.addCase(_teacherUploadQuestionFile.pending, (state) => {
state.uploading = true;
state.error = null;
})
.addCase(_teacherUploadQuestionFile.fulfilled, (state, action) => {
state.uploading = false;
state.jobId = action.payload.job_id;
state.state = action.payload.state;
state.filename = action.payload.filename ?? state.filename;
// The API returns 200 with this flag when the same file was already
// uploaded, rather than creating a second identical batch.
state.duplicate = Boolean(action.payload.duplicate_of_existing_import);
})
.addCase(_teacherUploadQuestionFile.rejected, (state, action) => {
state.uploading = false;
state.error = action.payload;
})
// A paste produces the SAME job as an upload, so it shares the reducers.
// Duplicating them would let the two drift apart for no reason.
.addCase(_teacherPasteQuestion.pending, (state) => {
state.uploading = true;
state.error = null;
})
.addCase(_teacherPasteQuestion.fulfilled, (state, action) => {
state.uploading = false;
state.jobId = action.payload.job_id;
state.state = action.payload.state;
state.filename = action.payload.filename ?? state.filename;
})
.addCase(_teacherPasteQuestion.rejected, (state, action) => {
state.uploading = false;
state.error = action.payload;
})
.addCase(_teacherFetchImportStatus.fulfilled, (state, action) => {
const data = action.payload;
state.state = data.state;
state.progress = data.progress ?? 0;
state.total = data.total ?? 0;
state.questionsFound = data.questions_found ?? 0;
state.questionsNeedingReview = data.questions_needing_review ?? 0;
state.rowErrors = data.errors ?? [];
})
.addCase(_teacherFetchImportStatus.rejected, (state, action) => {
state.error = action.payload;
})
.addCase(_teacherFetchImportQuestions.pending, (state) => {
state.loading = true;
})
.addCase(_teacherFetchImportQuestions.fulfilled, (state, action) => {
state.loading = false;
state.questions = action.payload.questions ?? [];
state.persisted = Boolean(action.payload.persisted);
// `?? null` NOT `?? 0`: the API sends null when the duplicate lookup
// failed, and collapsing that to 0 would claim there are none.
state.duplicates = action.payload.duplicates ?? null;
})
.addCase(_teacherFetchImportQuestions.rejected, (state, action) => {
state.loading = false;
state.error = action.payload;
})
.addCase(_teacherCommitImport.pending, (state) => {
state.committing = true;
state.error = null;
})
.addCase(_teacherCommitImport.fulfilled, (state, action) => {
state.committing = false;
state.state = "committed";
state.persisted = true;
state.committed = action.payload;
})
.addCase(_teacherCommitImport.rejected, (state, action) => {
state.committing = false;
state.error = action.payload;
});
},
});
export const { resetImport, updateReviewedQuestion, removeReviewedQuestion } = teacherQuestionImportSlice.actions;
// Default export is the SLICE, not the reducer — Store.js does `X.reducer`, matching
// every other slice in this app.
export default teacherQuestionImportSlice;
|