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 | 6x 4x 4x 3x 1x 6x 1x 1x 1x 6x 2x 2x 2x 6x 1x 1x 1x 6x 1x 1x 1x 11x 11x 6x 5x 4x 6x 2x 3x 6x 6x 6x 15x 3x 15x 15x 15x 15x 15x 3x 15x 3x 15x 15x 3x 3x 3x 2x 2x 5x 5x 2x 4x 4x 9x 9x 4x 4x 2x 2x 5x 9x 5x 3x 3x 6x | import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { TeacherClassAPI } from "@/api/teacher/TeacherClassAPI.js";
export const _fetchAllClasses = createAsyncThunk("fetch/allClasses", async (query, { rejectWithValue }) => {
try {
const response = await TeacherClassAPI.prototype.fetchAllClasses(query);
return response;
} catch (error) {
return rejectWithValue(error.response.data);
}
});
export const _createNewClass = createAsyncThunk("create/newClass", async (payload, { rejectWithValue }) => {
try {
const response = await TeacherClassAPI.prototype.createNewClass(payload);
return response;
} catch (error) {
return rejectWithValue(error.response.data);
}
});
export const _addClassPhoto = createAsyncThunk(
"teacher/addClassPhoto",
async ({ classUUID, formData }, { rejectWithValue }) => {
try {
const response = await TeacherClassAPI.prototype.addClassPhoto(classUUID, formData);
return { classUUID, response };
} catch (error) {
return rejectWithValue(error.response?.data);
}
}
);
export const _updateClassPhoto = createAsyncThunk(
"teacher/updateClassPhoto",
async ({ classUUID, formData }, { rejectWithValue }) => {
try {
const response = await TeacherClassAPI.prototype.updateClassPhoto(classUUID, formData);
return { classUUID, response };
} catch (error) {
return rejectWithValue(error.response?.data);
}
}
);
export const _deleteClassPhoto = createAsyncThunk(
"teacher/deleteClassPhoto",
async (classUUID, { rejectWithValue }) => {
try {
const response = await TeacherClassAPI.prototype.deleteClassPhoto(classUUID);
return { classUUID, response };
} catch (error) {
return rejectWithValue(error.response?.data);
}
}
);
// Shared by the three class-photo thunks' fulfilled cases: finds the class
// card matching the mutated classUUID and syncs its class_photo in place.
function patchClassPhoto(state, { classUUID, response }) {
const updatedClass = response?.data?.updated_class;
if (!updatedClass || !state.classData) return;
const index = state.classData.findIndex((entry) => entry._id === classUUID);
if (index !== -1) {
state.classData[index].class_photo = updatedClass.class_photo ?? null;
}
}
const TeacherClasses = createSlice({
name: "TeacherClasses",
initialState: {
classData: null,
_pagination: {
totalCount: null,
pageSize: null,
currentPage: null,
overAllEntries: null,
},
// Backend-enforced 10-class cap (see `server/utilities/class_limits.py`).
// Populated from `/v1/teacher/class/all/fetch` on every list refresh so
// the Create button + Classes page can render "X/N used" and disable
// Create at the cap. Null until the first fetch resolves; older
// backends omit these fields → indicator stays hidden.
classLimit: null,
activeClassCount: null,
hasErrors: null,
isLoading: false,
},
reducers: {
_clearErrorMessage: (state) => {
state.hasErrors = null;
},
},
extraReducers: (builder) => {
builder
//fetch all classes
.addCase(_fetchAllClasses.pending, (state) => {
state.classData = null;
state.hasErrors = null;
state.isLoading = true;
})
.addCase(_fetchAllClasses.fulfilled, (state, action) => {
state.classData = [
...action.payload.data.data.sort((a, b) => {
return b.created_at.localeCompare(a.created_at);
}),
];
//pagination
state._pagination.totalCount = action.payload.data.count;
state._pagination.pageSize = action.payload.data.no_of_pages;
state._pagination.currentPage = action.payload.data.page;
state._pagination.overAllEntries = action.payload.data.total;
// Class-cap UX fields — backend ships these on `class/all/fetch`
// (PR adding `class_limit`+`active_class_count`); older backends
// omit them, so leave the existing slice values alone in that
// case (null → indicator hidden by the FE).
if (typeof action.payload.data.class_limit === "number") {
state.classLimit = action.payload.data.class_limit;
}
if (typeof action.payload.data.active_class_count === "number") {
state.activeClassCount = action.payload.data.active_class_count;
}
state.hasErrors = null;
state.isLoading = false;
})
.addCase(_fetchAllClasses.rejected, (state, action) => {
state.classData = null;
state.hasErrors = action.error.message;
state.isLoading = false;
})
//create new class
.addCase(_createNewClass.pending, (state) => {
state.hasErrors = null;
state.isLoading = true;
})
.addCase(_createNewClass.fulfilled, (state, action) => {
state.classData.unshift({ ...action.payload.data.new_class });
// Increment the active class count locally so the X/N
// indicator reflects the new total without waiting for the
// next list refetch.
if (typeof state.activeClassCount === "number") {
state.activeClassCount += 1;
}
state.hasErrors = null;
state.isLoading = false;
})
.addCase(_createNewClass.rejected, (state, action) => {
// Backend returns string OR structured detail dict (e.g. the
// class-limit-reached error carries `{message, reason,
// class_limit, active_class_count}` so the FE can render a
// remediation hint and sync its in-memory count). Normalize
// to a string for the SnackBar.
const detail = action.payload?.detail;
if (detail && typeof detail === "object") {
state.hasErrors = detail.message || "Class creation failed.";
if (
detail.reason === "class_limit_reached" &&
typeof detail.class_limit === "number" &&
typeof detail.active_class_count === "number"
) {
state.classLimit = detail.class_limit;
state.activeClassCount = detail.active_class_count;
}
} else {
state.hasErrors = detail || "Class creation failed.";
}
state.isLoading = false;
})
// class cover photo add / update / delete — patch the matching
// card in-place with the server's presigned class_photo URL
// instead of forcing a full list refetch.
.addCase(_addClassPhoto.fulfilled, (state, action) => {
patchClassPhoto(state, action.payload);
})
.addCase(_updateClassPhoto.fulfilled, (state, action) => {
patchClassPhoto(state, action.payload);
})
.addCase(_deleteClassPhoto.fulfilled, (state, action) => {
patchClassPhoto(state, action.payload);
});
},
});
export const { _clearErrorMessage } = TeacherClasses.actions;
export default TeacherClasses;
|