From 44c9659b1d8f1a1ff44a382aaefd1a8afd9cb27c Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 10 Jun 2024 12:24:17 -0700 Subject: [PATCH] Refactored actions --- Client/src/Features/Auth/authSlice.js | 44 +++++++++++---------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/Client/src/Features/Auth/authSlice.js b/Client/src/Features/Auth/authSlice.js index bef6e8630..2182acef5 100644 --- a/Client/src/Features/Auth/authSlice.js +++ b/Client/src/Features/Auth/authSlice.js @@ -31,6 +31,20 @@ export const login = createAsyncThunk("auth/login", async (form, thunkApi) => { } }); +const handleAuthFulfilled = (state, action) => { + state.isLoading = false; + state.success = action.payload.success; + state.msg = action.payload.msg; + const decodedToken = jwtDecode(action.payload.data); + const user = { ...decodedToken }; + state.user = user; +}; +const handleAuthRejected = (state, action) => { + state.isLoading = false; + state.success = action.payload.success; + state.msg = action.payload.msg; +}; + const authSlice = createSlice({ name: "auth", initialState, @@ -41,36 +55,14 @@ const authSlice = createSlice({ .addCase(register.pending, (state, action) => { state.isLoading = true; }) - .addCase(register.fulfilled, (state, action) => { - state.isLoading = false; - state.success = action.payload.success; - state.msg = action.payload.msg; - const decodedToken = jwtDecode(action.payload.data); - const user = { ...decodedToken }; - state.user = user; - }) - .addCase(register.rejected, (state, action) => { - state.isLoading = false; - state.success = action.payload.success; - state.msg = action.payload.msg; - }) + .addCase(register.fulfilled, handleAuthFulfilled) + .addCase(register.rejected, handleAuthRejected) // Login thunk .addCase(login.pending, (state, action) => { state.isLoading = true; }) - .addCase(login.fulfilled, (state, action) => { - state.isLoading = false; - state.success = action.payload.success; - state.msg = action.payload.msg; - const decodedToken = jwtDecode(action.payload.data); - const user = { ...decodedToken }; - state.user = user; - }) - .addCase(login.rejected, (state, action) => { - state.isLoading = false; - state.success = action.payload.success; - state.msg = action.payload.msg; - }); + .addCase(login.fulfilled, handleAuthFulfilled) + .addCase(login.rejected, handleAuthRejected); }, });