Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { LoginActions, LoginActionKeys } from "actions/loginActions";
- // Interfaces
- export interface LoginState {
- isLoggedIn: boolean;
- isLoggingIn: boolean;
- loginError: boolean;
- username: string;
- password: string;
- profile?: Profile | null;
- }
- export interface Profile {
- nickname: string;
- name: string;
- picture: string;
- }
- // Initial State
- const loginInitialState: LoginState = {
- isLoggedIn: false,
- isLoggingIn: false,
- loginError: false,
- username: "",
- password: "",
- profile: null
- };
- export default (
- state: LoginState = loginInitialState,
- action: LoginActions
- ): LoginState => {
- switch (action.type) {
- case LoginActionKeys.LOGIN_START: {
- return {
- ...state,
- isLoggingIn: true,
- loginError: false
- };
- }
- case LoginActionKeys.LOGIN_SUCCESS: {
- return {
- ...state,
- isLoggedIn: true,
- isLoggingIn: false
- };
- }
- case LoginActionKeys.LOGIN_ERROR: {
- return {
- ...state,
- isLoggedIn: false,
- isLoggingIn: true,
- loginError: true
- };
- }
- case LoginActionKeys.LOGOUT: {
- return {
- ...state,
- isLoggedIn: false,
- profile: null,
- loginError: false
- };
- }
- case LoginActionKeys.STORE_USER_PROFILE: {
- return {
- ...state,
- profile: action.data.profile
- };
- }
- case LoginActionKeys.UPDATE_INFO: {
- return {
- ...state,
- [action.data.field]: action.data.value
- };
- }
- default: {
- return state;
- }
- }
- };
- const loginState = state => state.login as LoginState;
- // Selectors
- export const selectors = {
- isLoggedIn: state => loginState(state).isLoggedIn,
- isLoggingIn: state => loginState(state).isLoggingIn,
- loginError: state => loginState(state).loginError,
- userProfile: state => loginState(state).profile
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement