1- import { createAsyncThunk , createSlice } from ' @reduxjs/toolkit' ;
2- import { fetchCount } from ' ./authAPI' ;
1+ import { createAsyncThunk , createSlice } from " @reduxjs/toolkit" ;
2+ import { checkUser , createUser } from " ./authAPI" ;
33
44const initialState = {
5- value : 0 ,
6- status : 'idle' ,
5+ loggedInUser : null ,
6+ status : "idle" ,
7+ erorr : null ,
78} ;
89
9- export const incrementAsync = createAsyncThunk (
10- 'counter/fetchCount' ,
11- async ( amount ) => {
12- const response = await fetchCount ( amount ) ;
10+ export const createUserAsync = createAsyncThunk (
11+ "user/createUser" ,
12+ async ( userData ) => {
13+ const response = await createUser ( userData ) ;
14+ // The value we return becomes the `fulfilled` action payload
15+ return response . data ;
16+ }
17+ ) ;
18+ export const checkUserAsync = createAsyncThunk (
19+ "user/checkUser" ,
20+ async ( loginInfo ) => {
21+ const response = await checkUser ( loginInfo ) ;
1322 // The value we return becomes the `fulfilled` action payload
1423 return response . data ;
1524 }
1625) ;
1726
18- export const counterSlice = createSlice ( {
19- name : 'counter' ,
27+ export const authSlice = createSlice ( {
28+ name : "user" ,
2029 initialState,
2130 reducers : {
2231 increment : ( state ) => {
@@ -25,18 +34,30 @@ export const counterSlice = createSlice({
2534 } ,
2635 extraReducers : ( builder ) => {
2736 builder
28- . addCase ( incrementAsync . pending , ( state ) => {
29- state . status = 'loading' ;
37+ . addCase ( createUserAsync . pending , ( state ) => {
38+ state . status = "loading" ;
39+ } )
40+ . addCase ( createUserAsync . fulfilled , ( state , action ) => {
41+ state . status = "idle" ;
42+ state . loggedInUser = action . payload ;
43+ } )
44+ . addCase ( checkUserAsync . pending , ( state ) => {
45+ state . status = "loading" ;
46+ } )
47+ . addCase ( checkUserAsync . fulfilled , ( state , action ) => {
48+ state . status = "idle" ;
49+ state . loggedInUser = action . payload ;
3050 } )
31- . addCase ( incrementAsync . fulfilled , ( state , action ) => {
32- state . status = ' idle' ;
33- state . value + = action . payload ;
51+ . addCase ( checkUserAsync . rejected , ( state , action ) => {
52+ state . status = " idle" ;
53+ state . erorr = action . error ;
3454 } ) ;
3555 } ,
3656} ) ;
3757
38- export const { increment } = counterSlice . actions ;
58+ export const { increment } = authSlice . actions ;
3959
40- export const selectCount = ( state ) => state . counter . value ;
60+ export const selectLoggedInUser = ( state ) => state . auth . loggedInUser ;
61+ export const selectError = ( state ) => state . auth . erorr ;
4162
42- export default counterSlice . reducer ;
63+ export default authSlice . reducer ;
0 commit comments