Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import AccountModel from '../generated/models/AccountModel';
  2. import { Reducer } from 'redux';
  3. import { AppThunkAction } from './';
  4.  
  5. export type AccountState = AccountModel | null;
  6.  
  7. export interface AuthState {
  8.     account: AccountState;          // account details, or null if the current user is not authenticated
  9.     isLoading: boolean;             // true when a getAccount request is in progress (TODO: rename to isLoadingAccount)
  10.     initialized: boolean;           // false unti the first getAccount request starts
  11. }
  12.  
  13. interface LoadAccountStartAction {
  14.     type: 'LOAD_ACCOUNT_START';
  15. }
  16. const LoadAccountStartAction: LoadAccountStartAction = { type: 'LOAD_ACCOUNT_START' };
  17.  
  18. interface LoadAccountSuccessAction {
  19.     type: 'LOAD_ACCOUNT_SUCCESS';
  20.     account: AccountModel;
  21. }
  22.  
  23. interface LoadAccountFailAction {
  24.     type: 'LOAD_ACCOUNT_FAIL';
  25. }
  26.  
  27. const LoadAccountFailAction: LoadAccountFailAction = { type: 'LOAD_ACCOUNT_FAIL' };
  28.  
  29. interface LogoutSuccessAction {
  30.     type: 'AUTH_LOGOUT_SUCCESS';
  31. }
  32.  
  33. export const LogoutSuccessAction: LogoutSuccessAction = { type: 'AUTH_LOGOUT_SUCCESS' };
  34.  
  35. type KnownAction = LoadAccountStartAction | LoadAccountSuccessAction | LoadAccountFailAction | LogoutSuccessAction;
  36.  
  37.  
  38. export const actionCreators = {
  39.     loadAccount: (): AppThunkAction<any> => (dispatch, getState) => {
  40.         dispatch(LoadAccountStartAction);
  41.         return loadAccount(dispatch);
  42.     }
  43. };
  44.  
  45.  
  46. const initialState: AuthState = {
  47.     account: null,
  48.     isLoading: false,
  49.     initialized: false
  50. };
  51.  
  52. export const reducer: Reducer<AuthState> = (state: AuthState, action: KnownAction) => {
  53.    
  54.     switch (action.type) {
  55.         case 'LOAD_ACCOUNT_START':
  56.             return { ...state, isLoading: true, initialized: true };
  57.         case 'LOAD_ACCOUNT_SUCCESS':
  58.             const { account } = action;
  59.             return { ...state, isLoading: false, account };
  60.         case 'LOAD_ACCOUNT_FAIL':
  61.             return { ...state, isLoading: false, account: null };
  62.         case 'AUTH_LOGOUT_SUCCESS':
  63.             return { ...state, account: null };
  64.     }
  65.     return state || initialState;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement