Refactored actions
This commit is contained in:
1 parent
aea01f2d00
commit
44c9659b1d
1 file changed
+18
-26
@@ -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({
|
const authSlice = createSlice({
|
||||||
name: "auth",
|
name: "auth",
|
||||||
initialState,
|
initialState,
|
||||||
@@ -41,36 +55,14 @@ const authSlice = createSlice({
|
|||||||
.addCase(register.pending, (state, action) => {
|
.addCase(register.pending, (state, action) => {
|
||||||
state.isLoading = true;
|
state.isLoading = true;
|
||||||
})
|
})
|
||||||
.addCase(register.fulfilled, (state, action) => {
|
.addCase(register.fulfilled, handleAuthFulfilled)
|
||||||
state.isLoading = false;
|
.addCase(register.rejected, handleAuthRejected)
|
||||||
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;
|
|
||||||
})
|
|
||||||
// Login thunk
|
// Login thunk
|
||||||
.addCase(login.pending, (state, action) => {
|
.addCase(login.pending, (state, action) => {
|
||||||
state.isLoading = true;
|
state.isLoading = true;
|
||||||
})
|
})
|
||||||
.addCase(login.fulfilled, (state, action) => {
|
.addCase(login.fulfilled, handleAuthFulfilled)
|
||||||
state.isLoading = false;
|
.addCase(login.rejected, handleAuthRejected);
|
||||||
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;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user