add login page with login action
Showing
... | ... | @@ -3,6 +3,8 @@ |
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@material-ui/core": "^4.9.13", | ||
"@material-ui/icons": "^4.9.1", | ||
"@reduxjs/toolkit": "^1.2.5", | ||
"@testing-library/jest-dom": "^4.2.4", | ||
"@testing-library/react": "^9.3.2", | ||
... | ... | @@ -13,12 +15,19 @@ |
"@types/react-dom": "^16.9.0", | ||
"@types/react-redux": "^7.1.7", | ||
"@types/react-router-dom": "^5.1.5", | ||
"@types/redux-logger": "^3.0.7", | ||
"@types/yup": "^0.28.1", | ||
"add": "^2.0.6", | ||
"formik": "^2.1.4", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"react-redux": "^7.2.0", | ||
"react-router-dom": "^5.1.2", | ||
"react-scripts": "3.4.1", | ||
"typescript": "~3.8.2" | ||
"redux-logger": "^3.0.6", | ||
"typescript": "~3.8.2", | ||
"yarn": "^1.22.4", | ||
"yup": "^0.28.5" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
... | ... |
import { createAction, createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { AppThunk } from '../../app/store'; | ||
import { AppThunk, RootState } from '../store'; | ||
interface UserState { | ||
name?: string; | ||
... | ... | @@ -26,29 +26,29 @@ const initialState: UserState = { |
isLoggedOut: true | ||
} | ||
const loginRequest = createAction('user/login'); | ||
const loginSuccess = createAction('user/login/success', function prepare(info: UserInfo) { | ||
return { | ||
payload: info | ||
} | ||
}); | ||
const loginFailure = createAction('user/login/failure', function prepare(error: UserError) { | ||
return { | ||
payload: error | ||
} | ||
}); | ||
const logout = createAction('user/logout'); | ||
// const loginRequest = createAction('user/login'); | ||
// const loginSuccess = createAction('user/login/success', function prepare(info: UserInfo) { | ||
// return { | ||
// payload: info | ||
// } | ||
// }); | ||
// const loginFailure = createAction('user/login/failure', function prepare(error: UserError) { | ||
// return { | ||
// payload: error | ||
// } | ||
// }); | ||
// const logout = createAction('user/logout'); | ||
export const userSlice = createSlice({ | ||
name: 'user', | ||
initialState, | ||
reducers: { | ||
[loginRequest]: (state, action) => { | ||
loginRequest: (state, action) => { | ||
return { | ||
isLoggingIn: true | ||
} | ||
}, | ||
[loginSuccess]: (state, action: PayloadAction<UserInfo>) => { | ||
loginSuccess: (state, action: PayloadAction<UserInfo>) => { | ||
return { | ||
isLoggingIn: false, | ||
isLoggedOut: false, | ||
... | ... | @@ -56,26 +56,27 @@ export const userSlice = createSlice({ |
email: action.payload.email | ||
} | ||
}, | ||
[loginFailure]: (state, action: PayloadAction<UserError>) => { | ||
loginFailure: (state, action: PayloadAction<UserError>) => { | ||
return { | ||
loggingError: action.payload.error | ||
} | ||
} | ||
[logout]: (state, action) => { | ||
}, | ||
logout: (state, action) => { | ||
return { | ||
isLoggedOut: true | ||
} | ||
} | ||
}, | ||
}} | ||
}) | ||
export const { loginRequest, loginSuccess, loginFailure, logout } = userSlice.actions; | ||
interface LoginRequestPayload { | ||
username: string; | ||
password: string; | ||
} | ||
export const userLogin = (payload: LoginRequestPayload): AppThunk => dispatch => { | ||
dispatch(loginRequest) | ||
dispatch(loginRequest({})) | ||
setTimeout(() => { | ||
dispatch(loginSuccess({name: payload.username, email: '[email protected]'})); | ||
}, 2000); | ||
... | ... | @@ -86,3 +87,7 @@ export const userLogout = (): AppThunk => dispatch => { |
dispatch(logout) | ||
}, 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