add login page with login action
Showing
import { createAction, createSlice, PayloadAction } from '@reduxjs/toolkit'; | import { createAction, createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { AppThunk } from '../../app/store'; | import { AppThunk, RootState } from '../store'; | ||
interface UserState { | interface UserState { | ||
name?: string; | name?: string; | ||
... | @@ -26,29 +26,29 @@ const initialState: UserState = { | ... | @@ -26,29 +26,29 @@ const initialState: UserState = { |
isLoggedOut: true | isLoggedOut: true | ||
} | } | ||
const loginRequest = createAction('user/login'); | // const loginRequest = createAction('user/login'); | ||
const loginSuccess = createAction('user/login/success', function prepare(info: UserInfo) { | // const loginSuccess = createAction('user/login/success', function prepare(info: UserInfo) { | ||
return { | // return { | ||
payload: info | // payload: info | ||
} | // } | ||
}); | // }); | ||
const loginFailure = createAction('user/login/failure', function prepare(error: UserError) { | // const loginFailure = createAction('user/login/failure', function prepare(error: UserError) { | ||
return { | // return { | ||
payload: error | // payload: error | ||
} | // } | ||
}); | // }); | ||
const logout = createAction('user/logout'); | // const logout = createAction('user/logout'); | ||
export const userSlice = createSlice({ | export const userSlice = createSlice({ | ||
name: 'user', | name: 'user', | ||
initialState, | initialState, | ||
reducers: { | reducers: { | ||
[loginRequest]: (state, action) => { | loginRequest: (state, action) => { | ||
return { | return { | ||
isLoggingIn: true | isLoggingIn: true | ||
} | } | ||
}, | }, | ||
[loginSuccess]: (state, action: PayloadAction<UserInfo>) => { | loginSuccess: (state, action: PayloadAction<UserInfo>) => { | ||
return { | return { | ||
isLoggingIn: false, | isLoggingIn: false, | ||
isLoggedOut: false, | isLoggedOut: false, | ||
... | @@ -56,26 +56,27 @@ export const userSlice = createSlice({ | ... | @@ -56,26 +56,27 @@ export const userSlice = createSlice({ |
email: action.payload.email | email: action.payload.email | ||
} | } | ||
}, | }, | ||
[loginFailure]: (state, action: PayloadAction<UserError>) => { | loginFailure: (state, action: PayloadAction<UserError>) => { | ||
return { | return { | ||
loggingError: action.payload.error | loggingError: action.payload.error | ||
} | } | ||
} | }, | ||
[logout]: (state, action) => { | logout: (state, action) => { | ||
return { | return { | ||
isLoggedOut: true | isLoggedOut: true | ||
} | } | ||
} | }} | ||
}, | |||
}) | }) | ||
export const { loginRequest, loginSuccess, loginFailure, logout } = userSlice.actions; | |||
interface LoginRequestPayload { | interface LoginRequestPayload { | ||
username: string; | username: string; | ||
password: string; | password: string; | ||
} | } | ||
export const userLogin = (payload: LoginRequestPayload): AppThunk => dispatch => { | export const userLogin = (payload: LoginRequestPayload): AppThunk => dispatch => { | ||
dispatch(loginRequest) | dispatch(loginRequest({})) | ||
setTimeout(() => { | setTimeout(() => { | ||
dispatch(loginSuccess({name: payload.username, email: '[email protected]'})); | dispatch(loginSuccess({name: payload.username, email: '[email protected]'})); | ||
}, 2000); | }, 2000); | ||
... | @@ -86,3 +87,7 @@ export const userLogout = (): AppThunk => dispatch => { | ... | @@ -86,3 +87,7 @@ export const userLogout = (): AppThunk => dispatch => { |
dispatch(logout) | dispatch(logout) | ||
}, 1000) | }, 1000) | ||
} | } | ||
export const selectUser = (state: RootState) => state.user; | |||
export default userSlice.reducer; |
src/pages/Login/index.css
0 → 100644
src/pages/Login/index.tsx
0 → 100644
This diff is collapsed.
Please register or sign in to comment