From 8883eacd2a5e2f3f5637a6b71123dfcb2a64c3d5 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Thu, 12 Dec 2019 19:31:50 +0530 Subject: :fire: :zap: Major Update * Adds Actions, Redicers and Middlewares * Adds Http Service * Adds Cancel option for Http Service * Adds HOC's for API Loader, Sidebar and Headers * Adds Random key generator for Routes --- src/reducers/error.reducer.js | 18 ++++++++++++++++++ src/reducers/loading.reducer.js | 12 ++++++++++++ src/reducers/login.reducer.js | 21 +++++++++++++++++++++ src/reducers/root.reducer.js | 6 ++++++ 4 files changed, 57 insertions(+) create mode 100644 src/reducers/error.reducer.js create mode 100644 src/reducers/loading.reducer.js create mode 100644 src/reducers/login.reducer.js (limited to 'src/reducers') diff --git a/src/reducers/error.reducer.js b/src/reducers/error.reducer.js new file mode 100644 index 0000000..83d1128 --- /dev/null +++ b/src/reducers/error.reducer.js @@ -0,0 +1,18 @@ +export default function ErrorReducer(state = {}, action) { + const { type, error } = action; + const matches = /(.*)_(REQUEST|FAILED|ERROR)/.exec( + type + ); + if (!matches) return state; + const [, requestName, requestState] = matches; + return { + errorMessage: + requestState === "FAILED" || requestState === "ERROR" + ? error + ? error + : "" + : "", + [requestName]: requestState === "FAILED" || requestState === "ERROR" ? true: false + }; + } + \ No newline at end of file diff --git a/src/reducers/loading.reducer.js b/src/reducers/loading.reducer.js new file mode 100644 index 0000000..5ff7651 --- /dev/null +++ b/src/reducers/loading.reducer.js @@ -0,0 +1,12 @@ +export default function LoadingReducer(state = {}, action) { + console.log("Reducer",action) + const { type } = action; + const matches = /(.*)_(REQUEST|SUCCESS|FAILED|ERROR|SUBMIT|CANCEL)/.exec(type); + if (!matches) return state; + const [, requestName, requestState] = matches; + return { + ...state, + [requestName]: (requestState === 'REQUEST' || requestState === 'SUBMIT') + }; + +} \ No newline at end of file diff --git a/src/reducers/login.reducer.js b/src/reducers/login.reducer.js new file mode 100644 index 0000000..3229e35 --- /dev/null +++ b/src/reducers/login.reducer.js @@ -0,0 +1,21 @@ +import { LOGIN_REQUEST, LOGIN_SUCCESS } from "../utils/constants"; +import Storage from "../services/storage.service"; + + +let initialState = { + token: "" +}; + +export const LoginReducer = (state=initialState, action)=>{ + switch (action.type) { + case LOGIN_REQUEST: return state; + case LOGIN_SUCCESS: + + Storage.set('token', action.payload.token); + return { + ...state, + token: action.payload.token + }; + default: return state; + } +} \ No newline at end of file diff --git a/src/reducers/root.reducer.js b/src/reducers/root.reducer.js index de1a3b8..c5796ea 100644 --- a/src/reducers/root.reducer.js +++ b/src/reducers/root.reducer.js @@ -1,9 +1,15 @@ import { combineReducers } from "redux"; import { connectRouter } from "connected-react-router"; +import LoadingReducer from "./loading.reducer"; +import ErrorReducer from "./error.reducer"; +import { LoginReducer } from "./login.reducer"; const createRootReducer = history => combineReducers({ router: connectRouter(history), + loading: LoadingReducer, + error: ErrorReducer, + login: LoginReducer }); export default createRootReducer; -- cgit v1.2.3