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

85.71% Statements 54/63
87.87% Branches 29/33
88.23% Functions 15/17
85.71% Lines 54/63

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            9x                         9x     6x 6x 4x   1x         9x                       9x 9x 9x 9x 9x 9x 9x 9x     9x                                     1x     6x     2x 2x 2x 2x 2x 2x 2x 2x 2x       2x   1x 1x     9x 9x     2x 2x     5x     6x 6x 6x 5x       1x       1x 1x     3x 3x 3x 3x 1x   3x 1x   3x 1x       2x 2x         9x    
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { TeacherFeaturesAPI } from "@/api/teacher/TeacherFeaturesAPI";
import { normalizeFeatureList } from "@/utils/features/featureAccess";
import { extractTeacherMadeAssignmentQuotaFromPayload } from "@/utils/features/teacherMadeAssignmentQuota";
 
// Async thunk to fetch teacher's district features
export const fetchTeacherDistrictFeatures = createAsyncThunk(
    "teacher/features/fetchDistrictFeatures",
    async (_, { rejectWithValue, dispatch }) => {
        try {
            const response = await TeacherFeaturesAPI.getEffectiveFeatures();
            await dispatch(fetchTeacherMadeAssignmentQuota());
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data || error);
        }
    }
);
 
export const fetchTeacherMadeAssignmentQuota = createAsyncThunk(
    "teacher/features/fetchTeacherMadeAssignmentQuota",
    async (_, { rejectWithValue }) => {
        try {
            const response = await TeacherFeaturesAPI.getTeacherMadeAssignmentsQuota();
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data || error);
        }
    }
);
 
export const fetchTeacherOrganization = createAsyncThunk(
    "teacher/features/fetchOrganization",
    async (_, { rejectWithValue }) => {
        try {
            const response = await TeacherFeaturesAPI.getOrganization();
            return response;
        } catch (error) {
            return rejectWithValue(error.response?.data || error);
        }
    }
);
 
const applyEffectiveFeaturesPayload = (state, payload) => {
    state.districtId = payload.district_id ?? null;
    state.currentPlan = payload.current_plan ?? null;
    state.planName = payload.plan_name ?? null;
    state.districtFeatures = normalizeFeatureList(payload.features || payload.results || []);
    state.maintenanceMode = Boolean(payload.maintenance_mode);
    state.lastFetched = new Date().toISOString();
    state.hasErrors = null;
};
 
const TeacherFeatureFlags = createSlice({
    name: "TeacherFeatureFlags",
    initialState: {
        districtId: null,
        currentPlan: null,
        planName: null,
        districtFeatures: [],
        teacherMadeAssignments: null,
        maintenanceMode: false,
        organization: null,
        organizationLoading: false,
        organizationError: null,
        isLoading: false,
        quotaLoading: false,
        hasErrors: null,
        lastFetched: null,
    },
    reducers: {
        clearErrors: (state) => {
            state.hasErrors = null;
        },
        setTeacherMadeAssignmentQuota: (state, action) => {
            state.teacherMadeAssignments = action.payload ?? null;
        },
        resetFeatures: (state) => {
            state.districtId = null;
            state.currentPlan = null;
            state.planName = null;
            state.districtFeatures = [];
            state.teacherMadeAssignments = null;
            state.maintenanceMode = false;
            state.organization = null;
            state.organizationError = null;
            state.lastFetched = null;
        },
    },
    extraReducers: (builder) => {
        builder
            .addCase(fetchTeacherDistrictFeatures.pending, (state) => {
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(fetchTeacherDistrictFeatures.fulfilled, (state, action) => {
                state.isLoading = false;
                applyEffectiveFeaturesPayload(state, action.payload || {});
            })
            .addCase(fetchTeacherDistrictFeatures.rejected, (state, action) => {
                state.isLoading = false;
                state.hasErrors = action.payload?.detail || action.error.message;
            })
            .addCase(fetchTeacherMadeAssignmentQuota.pending, (state) => {
                state.quotaLoading = true;
            })
            .addCase(fetchTeacherMadeAssignmentQuota.fulfilled, (state, action) => {
                state.quotaLoading = false;
                const quota = extractTeacherMadeAssignmentQuotaFromPayload(action.payload);
                if (quota) {
                    state.teacherMadeAssignments = quota;
                }
            })
            .addCase(fetchTeacherMadeAssignmentQuota.rejected, (state) => {
                state.quotaLoading = false;
            })
            // Organization (school + district + plan)
            .addCase(fetchTeacherOrganization.pending, (state) => {
                state.organizationLoading = true;
                state.organizationError = null;
            })
            .addCase(fetchTeacherOrganization.fulfilled, (state, action) => {
                state.organizationLoading = false;
                state.organization = action.payload || null;
                state.organizationError = null;
                if (action.payload?.district_id) {
                    state.districtId = action.payload.district_id;
                }
                if (action.payload?.current_plan) {
                    state.currentPlan = action.payload.current_plan;
                }
                if (action.payload?.plan_name) {
                    state.planName = action.payload.plan_name;
                }
            })
            .addCase(fetchTeacherOrganization.rejected, (state, action) => {
                state.organizationLoading = false;
                state.organizationError = action.payload?.detail || action.error.message;
            });
    },
});
 
export const { clearErrors, resetFeatures, setTeacherMadeAssignmentQuota } = TeacherFeatureFlags.actions;
export default TeacherFeatureFlags;