From 7ce31d8aa5d8818f7d812f041efe7d3149b1dd95 Mon Sep 17 00:00:00 2001 From: Abraham Rudolf Brahmana Date: Thu, 28 Oct 2021 18:16:20 +0700 Subject: [PATCH 1/3] feat: Create redux action for ecosystem categories --- src/redux/ecosystem/actions.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/redux/ecosystem/actions.tsx diff --git a/src/redux/ecosystem/actions.tsx b/src/redux/ecosystem/actions.tsx new file mode 100644 index 0000000..d526966 --- /dev/null +++ b/src/redux/ecosystem/actions.tsx @@ -0,0 +1,16 @@ +import { Dispatch } from "react-redux/node_modules/@types/react"; +import * as ecosystemService from "../../service/firestore/categories"; + +export const getCategories = () => { + return async (dispatch: Dispatch) => { + try { + const categories = await ecosystemService.getCategories(); + return dispatch({ + type: "CATEGORIES", + payload: categories, + }); + } catch (e) { + console.log(e); + } + }; +}; -- GitLab From fbce138050a3939abf6f260d0ad754728c8d51eb Mon Sep 17 00:00:00 2001 From: Abraham Rudolf Brahmana Date: Thu, 28 Oct 2021 18:33:53 +0700 Subject: [PATCH 2/3] feat: Add reducer to redux ecosystem --- src/redux/ecosystem/reducer.tsx | 20 ++++++++++++++++++++ src/redux/store.tsx | 2 ++ 2 files changed, 22 insertions(+) create mode 100644 src/redux/ecosystem/reducer.tsx diff --git a/src/redux/ecosystem/reducer.tsx b/src/redux/ecosystem/reducer.tsx new file mode 100644 index 0000000..7d093ae --- /dev/null +++ b/src/redux/ecosystem/reducer.tsx @@ -0,0 +1,20 @@ +import { AnyAction } from "redux"; +import { ICategory } from "../../types/firestore"; + +const initialState: ICategory[] = []; + +const ecosystemReducer = ( + state = initialState, + action: AnyAction +): ICategory[] => { + switch (action.type) { + case "CATEGORIES": { + const payload = action.payload as { categories: ICategory[] }; + return { ...state, cateories: payload.categories } as ICategory[]; + } + default: + return state; + } +}; + +export default ecosystemReducer; diff --git a/src/redux/store.tsx b/src/redux/store.tsx index 5b5e132..d175c2d 100644 --- a/src/redux/store.tsx +++ b/src/redux/store.tsx @@ -1,6 +1,7 @@ import { configureStore } from "@reduxjs/toolkit"; import userReducer from "./user/reducer"; +import ecosystemReducer from "./ecosystem/reducer"; import thunk from "redux-thunk"; // const rootReducer = combineReducers({ @@ -12,6 +13,7 @@ import thunk from "redux-thunk"; const store = configureStore({ reducer: { user: userReducer, + ecosystem: ecosystemReducer, }, middleware: [thunk], }); -- GitLab From 78d40884f20d1f793ebf67092e10902ded19e58d Mon Sep 17 00:00:00 2001 From: Abraham Rudolf Brahmana Date: Fri, 29 Oct 2021 18:15:06 +0700 Subject: [PATCH 3/3] fix: Reducer return to store the categoreis in global state --- src/redux/ecosystem/reducer.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/redux/ecosystem/reducer.tsx b/src/redux/ecosystem/reducer.tsx index 7d093ae..99982c4 100644 --- a/src/redux/ecosystem/reducer.tsx +++ b/src/redux/ecosystem/reducer.tsx @@ -9,8 +9,7 @@ const ecosystemReducer = ( ): ICategory[] => { switch (action.type) { case "CATEGORIES": { - const payload = action.payload as { categories: ICategory[] }; - return { ...state, cateories: payload.categories } as ICategory[]; + return action.payload as ICategory[]; } default: return state; -- GitLab