feat: Initial setup of theme.
This commit is contained in:
57
src/redux/AuthRedux.js
Normal file
57
src/redux/AuthRedux.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import { createActions, createReducer } from 'reduxsauce';
|
||||
import Immutable from 'seamless-immutable';
|
||||
import _ from 'underscore';
|
||||
import { getFetchingValue, getErrorValue } from '../services/Utils';
|
||||
|
||||
/* ------------- Types and Action Creators ------------- */
|
||||
const { Types, Creators } = createActions({
|
||||
loginRequest: ['payload'],
|
||||
loginSuccess: ['data'],
|
||||
loginFailure: ['error'],
|
||||
logout: null
|
||||
});
|
||||
|
||||
export const AuthTypes = Types;
|
||||
const AuthActions = Creators;
|
||||
export default AuthActions;
|
||||
|
||||
/* ------------- Initial State ------------- */
|
||||
export const INITIAL_STATE = Immutable({
|
||||
user: null,
|
||||
fetching: [],
|
||||
error: {}
|
||||
});
|
||||
|
||||
/* ------------- Selectors ------------- */
|
||||
export const AuthSelectors = {
|
||||
getUser: (state) => state.auth.user,
|
||||
getAuthData: (state) => state.auth,
|
||||
fetching: (state) => state.fetching
|
||||
};
|
||||
|
||||
/* ------------- Reducers ------------- */
|
||||
export const onLoginRequest = (state, { payload }) =>
|
||||
state.merge({
|
||||
fetching: _.uniq([...state.fetching, payload?.loader]),
|
||||
error: getErrorValue(state?.error, payload?.loader)
|
||||
});
|
||||
|
||||
export const onLoginSuccess = (state, { data }) =>
|
||||
state.merge({
|
||||
fetching: getFetchingValue(state.fetching, data?.loader),
|
||||
error: getErrorValue(state?.error, data?.loader),
|
||||
user: data.user
|
||||
});
|
||||
|
||||
export const onLoginFailure = (state, { error }) =>
|
||||
state.merge({
|
||||
fetching: _.without(state.fetching, error?.loader),
|
||||
error: { ...state.error, [error?.loader]: error?.error }
|
||||
});
|
||||
|
||||
/* ------------- Hookup Reducers To Types ------------- */
|
||||
export const authReducer = createReducer(INITIAL_STATE, {
|
||||
[Types.LOGIN_REQUEST]: onLoginRequest,
|
||||
[Types.LOGIN_SUCCESS]: onLoginSuccess,
|
||||
[Types.LOGIN_FAILURE]: onLoginFailure
|
||||
});
|
||||
36
src/redux/Store.js
Normal file
36
src/redux/Store.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { applyMiddleware, compose, createStore } from 'redux';
|
||||
import { persistReducer, persistStore } from 'redux-persist';
|
||||
import createSagaMiddleware from 'redux-saga';
|
||||
import storage from 'redux-persist/lib/storage';
|
||||
import rootSaga from '../sagas';
|
||||
import immutablePersistenceTransform from '../services/immutablePersistenceTransform';
|
||||
import rootReducer from './index';
|
||||
import { APP_ENV } from '../config';
|
||||
|
||||
const sagaMiddleware = createSagaMiddleware();
|
||||
const middleWare = [sagaMiddleware];
|
||||
|
||||
const persistConfig = {
|
||||
key: '@root',
|
||||
storage,
|
||||
blacklist: ['nav', 'navigation', 'network'],
|
||||
transforms: [immutablePersistenceTransform]
|
||||
};
|
||||
|
||||
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
||||
|
||||
// Add middleware to redux store
|
||||
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
||||
const middleware = applyMiddleware(...middleWare);
|
||||
|
||||
const enhancers =
|
||||
APP_ENV.APP_MODE === 'development' ? composeEnhancers(middleware) : compose(middleware);
|
||||
|
||||
const store = createStore(persistedReducer, enhancers);
|
||||
|
||||
sagaMiddleware.run(rootSaga);
|
||||
|
||||
const persistor = persistStore(store);
|
||||
|
||||
// Enable persistence
|
||||
export default { store, persistor };
|
||||
17
src/redux/index.js
Normal file
17
src/redux/index.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { combineReducers } from 'redux';
|
||||
import { authReducer } from './AuthRedux';
|
||||
|
||||
// Combine all reducers.
|
||||
const appReducer = combineReducers({
|
||||
auth: authReducer
|
||||
});
|
||||
|
||||
const rootReducer = (state, action) => {
|
||||
// Clear all data in redux store to initial.
|
||||
if (action.type === 'LOGOUT') {
|
||||
state = undefined;
|
||||
}
|
||||
|
||||
return appReducer(state, action);
|
||||
};
|
||||
export default rootReducer;
|
||||
Reference in New Issue
Block a user