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

80% Statements 44/55
100% Branches 0/0
76.47% Functions 13/17
80% Lines 44/55

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        4x 4x 4x 2x   2x 2x       4x 4x 4x 2x   2x 2x             4x                       4x     4x 4x 2x   2x 2x         4x                       2x     1x 1x 1x     1x 1x 1x     1x 1x 1x         1x 1x     1x       1x 1x     1x 1x         1x 1x     1x       1x 1x     1x 1x                                                
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { StudentDataAPI } from "@/api/student/StudentDataAPI.js";
import { removeAllValues } from "@/utils/helpers/removeValuesFromLocalStorage";
 
export const _studentGetData = createAsyncThunk("auth/studentGetData", async (_, { rejectWithValue }) => {
    try {
        const request = await StudentDataAPI.prototype.studentGetData();
        return request;
    } catch (error) {
        removeAllValues();
        return rejectWithValue(error.response.data);
    }
});
 
export const _addProfilePicture = createAsyncThunk("add/profilePicture", async (imageFile, { rejectWithValue }) => {
    try {
        const response = await StudentDataAPI.prototype.addProfilePicture(imageFile);
        return response;
    } catch (error) {
        console.log(error);
        return rejectWithValue(error);
    }
});
 
// Added by Allan Ninal — 2026-09-24 (EI-T81). Mirrors the teacher slice's
// _deleteProfilePicture; the student side never had one, so the UI had nothing
// to dispatch even though the API endpoint existed.
export const _deleteProfilePicture = createAsyncThunk(
    "delete/studentProfilePicture",
    async (_, { rejectWithValue }) => {
        try {
            const response = await StudentDataAPI.prototype.deleteProfilePicture();
            return response;
        } catch (error) {
            return rejectWithValue(error);
        }
    }
);
 
export const _updateProfilePicture = createAsyncThunk(
    "update/profilePicture",
    async (imageFile, { rejectWithValue }) => {
        try {
            const response = await StudentDataAPI.prototype.updateProfilePicture(imageFile);
            return response;
        } catch (error) {
            console.log(error);
            return rejectWithValue(error);
        }
    }
);
 
const StudentData = createSlice({
    name: "StudentData",
    initialState: {
        studentData: null,
        hasErrors: null,
        isLoading: false,
 
        uploadPhotoHasErrors: null,
        uploadPhotoIsLoading: false,
    },
    reducers: {},
    extraReducers: (builder) => {
        builder
            //student get account data
            .addCase(_studentGetData.pending, (state) => {
                state.studentData = null;
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_studentGetData.fulfilled, (state, action) => {
                state.studentData = { ...action.payload.data.data };
                state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_studentGetData.rejected, (state, action) => {
                state.studentData = null;
                state.isLoading = false;
                state.hasErrors = action.error.message;
            })
 
            //student add profile picture
            .addCase(_addProfilePicture.pending, (state) => {
                state.uploadPhotoIsLoading = true;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_addProfilePicture.fulfilled, (state, action) => {
                state.studentData = {
                    ...state.studentData,
                    profile_picture: action.payload.data.data.profile_picture,
                };
                state.uploadPhotoIsLoading = false;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_addProfilePicture.rejected, (state, action) => {
                state.uploadPhotoIsLoading = false;
                state.uploadPhotoHasErrors = action.error.message;
            })
 
            //student update profile picture
            .addCase(_updateProfilePicture.pending, (state) => {
                state.uploadPhotoIsLoading = true;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_updateProfilePicture.fulfilled, (state, action) => {
                state.studentData = {
                    ...state.studentData,
                    profile_picture: action.payload.data.data.profile_picture,
                };
                state.uploadPhotoIsLoading = false;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_updateProfilePicture.rejected, (state, action) => {
                state.uploadPhotoIsLoading = false;
                state.uploadPhotoHasErrors = action.error.message;
            })
 
            // student delete profile picture — added 2026-09-24 (EI-T81)
            .addCase(_deleteProfilePicture.pending, (state) => {
                state.uploadPhotoIsLoading = true;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_deleteProfilePicture.fulfilled, (state, action) => {
                state.studentData = {
                    ...state.studentData,
                    profile_picture: action.payload.data.data.profile_picture,
                };
                state.uploadPhotoIsLoading = false;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_deleteProfilePicture.rejected, (state, action) => {
                state.uploadPhotoIsLoading = false;
                state.uploadPhotoHasErrors = action.error.message;
            });
    },
});
 
export default StudentData;