Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import AccountModel from '../generated/models/AccountModel';
- import { Reducer } from 'redux';
- import { AppThunkAction } from './';
- export type AccountState = AccountModel | null;
- export interface AuthState {
- account: AccountState; // account details, or null if the current user is not authenticated
- isLoading: boolean; // true when a getAccount request is in progress (TODO: rename to isLoadingAccount)
- initialized: boolean; // false unti the first getAccount request starts
- }
- interface LoadAccountStartAction {
- type: 'LOAD_ACCOUNT_START';
- }
- const LoadAccountStartAction: LoadAccountStartAction = { type: 'LOAD_ACCOUNT_START' };
- interface LoadAccountSuccessAction {
- type: 'LOAD_ACCOUNT_SUCCESS';
- account: AccountModel;
- }
- interface LoadAccountFailAction {
- type: 'LOAD_ACCOUNT_FAIL';
- }
- const LoadAccountFailAction: LoadAccountFailAction = { type: 'LOAD_ACCOUNT_FAIL' };
- interface LogoutSuccessAction {
- type: 'AUTH_LOGOUT_SUCCESS';
- }
- export const LogoutSuccessAction: LogoutSuccessAction = { type: 'AUTH_LOGOUT_SUCCESS' };
- type KnownAction = LoadAccountStartAction | LoadAccountSuccessAction | LoadAccountFailAction | LogoutSuccessAction;
- export const actionCreators = {
- loadAccount: (): AppThunkAction<any> => (dispatch, getState) => {
- dispatch(LoadAccountStartAction);
- return loadAccount(dispatch);
- }
- };
- const initialState: AuthState = {
- account: null,
- isLoading: false,
- initialized: false
- };
- export const reducer: Reducer<AuthState> = (state: AuthState, action: KnownAction) => {
- switch (action.type) {
- case 'LOAD_ACCOUNT_START':
- return { ...state, isLoading: true, initialized: true };
- case 'LOAD_ACCOUNT_SUCCESS':
- const { account } = action;
- return { ...state, isLoading: false, account };
- case 'LOAD_ACCOUNT_FAIL':
- return { ...state, isLoading: false, account: null };
- case 'AUTH_LOGOUT_SUCCESS':
- return { ...state, account: null };
- }
- return state || initialState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement