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

90.62% Statements 58/64
100% Branches 0/0
86.36% Functions 19/22
90.62% Lines 58/64

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          6x 4x 4x 2x   2x 2x       6x 4x 4x 2x   2x       6x     4x 4x 2x   2x         6x 4x 4x 2x   2x       6x     6x 6x 3x                       3x         6x                       2x       3x     1x 1x 1x     1x 1x 1x     1x 1x 1x         1x 1x     1x       1x 1x     1x 1x         1x 1x     1x       1x 1x                         1x       1x 1x                   3x     2x   2x       3x         6x    
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { messageFromRejection } from "@/utils/helpers/rejectionMessage";
import { TeacherDataAPI } from "@/api/teacher/TeacherDataAPI.js";
import { removeAllValues } from "@/utils/helpers/removeValuesFromLocalStorage";
 
export const _teacherGetData = createAsyncThunk("auth/teacherGetData", async (_, { rejectWithValue }) => {
    try {
        const request = await TeacherDataAPI.prototype.teacherGetData();
        return request;
    } catch (error) {
        removeAllValues();
        return rejectWithValue(error);
    }
});
 
export const _addProfilePicture = createAsyncThunk("add/profilePicture", async (imageFile, { rejectWithValue }) => {
    try {
        const response = await TeacherDataAPI.prototype.addProfilePicture(imageFile);
        return response;
    } catch (error) {
        return rejectWithValue(error);
    }
});
 
export const _updateProfilePicture = createAsyncThunk(
    "update/profilePicture",
    async (imageFile, { rejectWithValue }) => {
        try {
            const response = await TeacherDataAPI.prototype.updateProfilePicture(imageFile);
            return response;
        } catch (error) {
            return rejectWithValue(error);
        }
    }
);
 
export const _deleteProfilePicture = createAsyncThunk("delete/profilePicture", async (_, { rejectWithValue }) => {
    try {
        const response = await TeacherDataAPI.prototype.deleteProfilePicture();
        return response;
    } catch (error) {
        return rejectWithValue(error);
    }
});
 
export const _updateFullname = createAsyncThunk(
    "update/fullname",
    async ({ teacherUUID, payload }, { rejectWithValue }) => {
        try {
            const response = await TeacherDataAPI.prototype.updateFullname(teacherUUID, payload);
            return response;
        } catch (error) {
            // Modified by Allan Ninal — this used to reject with the whole axios error,
            // and the reducer read `payload.response.data.detail` straight through it.
            // On a network error there is no `.response`, so the REDUCER threw: the
            // dispatch rejected with a TypeError, `hasErrors` was never set, and the
            // teacher got no message at all. (Rejecting with an Error also puts a
            // non-serialisable value in the action.)
            //
            // Only this thunk changes: the profile-picture thunks above are read by
            // reducers that use `action.error.message`, which rejectWithValue would
            // turn into the literal string "Rejected".
            return rejectWithValue(error?.response?.data);
        }
    }
);
 
const TeacherData = createSlice({
    name: "TeacherData",
    initialState: {
        teacherData: null,
        hasErrors: null,
        isLoading: false,
 
        uploadPhotoHasErrors: null,
        uploadPhotoIsLoading: false,
    },
    reducers: {
        _clearErrorMessage: (state, action) => {
            state.hasErrors = null;
        },
    },
    extraReducers: (builder) => {
        builder
            //teacher get account data
            .addCase(_teacherGetData.pending, (state) => {
                state.teacherData = null;
                state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_teacherGetData.fulfilled, (state, action) => {
                state.teacherData = { ...action.payload.data.data };
                state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_teacherGetData.rejected, (state, action) => {
                state.teacherData = null;
                state.isLoading = false;
                state.hasErrors = action.error.message;
            })
 
            //teacher add profile picture
            .addCase(_addProfilePicture.pending, (state) => {
                state.uploadPhotoIsLoading = true;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_addProfilePicture.fulfilled, (state, action) => {
                state.teacherData = {
                    ...state.teacherData,
                    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;
            })
 
            //teacher update profile picture
            .addCase(_updateProfilePicture.pending, (state) => {
                state.uploadPhotoIsLoading = true;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_updateProfilePicture.fulfilled, (state, action) => {
                state.teacherData = {
                    ...state.teacherData,
                    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;
            })
 
            //teacher delete profile picture
            .addCase(_deleteProfilePicture.pending, (state) => {
                state.uploadPhotoIsLoading = true;
                state.uploadPhotoHasErrors = null;
            })
            .addCase(_deleteProfilePicture.fulfilled, (state, action) => {
                state.teacherData = {
                    ...state.teacherData,
                    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;
            })
 
            //teacher update fullname
            .addCase(_updateFullname.pending, (state) => {
                // state.isLoading = true;
                state.hasErrors = null;
            })
            .addCase(_updateFullname.fulfilled, (state, action) => {
                state.teacherData = { ...state.teacherData, ...action.payload.data.updated_user_account };
                // state.isLoading = false;
                state.hasErrors = null;
            })
            .addCase(_updateFullname.rejected, (state, action) => {
                // state.isLoading = false;
                state.hasErrors = messageFromRejection(action);
            });
    },
});
 
export const { _clearErrorMessage } = TeacherData.actions;
export default TeacherData;