Skip to content

Commit

Permalink
Merge pull request #14 from HTHstudy/hthDev
Browse files Browse the repository at this point in the history
Dynamic reudcer(react) 적용 완료
  • Loading branch information
CreadDiscans committed Mar 18, 2021
2 parents dc4e296 + a1f4742 commit f9de48b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
9 changes: 4 additions & 5 deletions react/src/app/Reducers.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { combineReducers } from 'redux';
import { penderReducer } from 'redux-pender';

import auth, { AuthState } from 'auth/Auth.action';
import { AuthState } from 'auth/Auth.action';
import board, { BoardState } from 'board/Board.action';
import dashboard, { DashboardState } from 'dashboard/Dashboard.action';
import mypage, { MypageState } from 'mypage/Mypage.action';
import shared, { SharedState } from 'component/Shared.action';
export default combineReducers({
auth,

export default (asyncReducers={}) => combineReducers({
board,
dashboard,
mypage,
shared,
pender: penderReducer,
// ...asyncReducers
...asyncReducers
})

export type RootState = {
Expand Down
29 changes: 12 additions & 17 deletions react/src/app/core/configureStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createStore, applyMiddleware, compose } from 'redux';
import penderMiddleware from 'redux-pender';

import modules from 'app/Reducers';
// import modules from 'app/Reducers';
import * as ApiType from 'types/api.types';
import { getHandleActions } from './connection';
import createReducer from '../Reducers';
Expand All @@ -12,30 +12,25 @@ const isDevelopment = process.env.NODE_ENV === 'development';

const composeEnhancers = isDevelopment ? (window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] || compose) : compose;

const initState = () => {
let userProfile:ApiType.Profile|undefined;
if (typeof localStorage === 'undefined') {
userProfile = undefined;
} else {
const str = localStorage.getItem('user_profile')
userProfile = str? JSON.parse(str):undefined
}
return userProfile
}

const configureStore = (initialState:any) => {
if (initialState === undefined) initialState = {}
if (initialState.auth === undefined) initialState.auth = {}
initialState.auth.userProfile = initState();
const store:any = createStore(modules, initialState, composeEnhancers(
applyMiddleware(penderMiddleware())
));

const store:any = createStore(
createReducer(),
initialState,
composeEnhancers(
applyMiddleware(penderMiddleware())
)
);
store.asyncReducers = {}

if(module.hot) {
module.hot.accept('../Reducers', ()=> {
const nextRootReducer = require('../Reducers').default;
store.replaceReducer(nextRootReducer);
});
}

store.injectReducer = (actions, initState) => {
const asyncReducer = getHandleActions(actions, initState)
store.asyncReducers[actions.name] = asyncReducer
Expand Down
7 changes: 4 additions & 3 deletions react/src/app/core/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ export const connectWithoutDone:any = (mapStateToProps:any, mapDispatchToProps:a

const getActions = (Action:any) => {
const actions:any = {}
Object.keys(Action).forEach(key=>
actions[key] = createAction(key, Action[key]))
Object.keys(Action).filter(k=>k!=='name').forEach(key=> {
actions[key] = createAction(key, Action[key])
})
return actions
}

export const getHandleActions = (Action:any, initState:any) => {
const arr = Object.keys(Action).map(key=>
const arr = Object.keys(Action).filter(k=>k!=='name').map(key=>
pender({
type:key,
onSuccess:(state:any, {payload})=>({...state, ...payload})
Expand Down
5 changes: 3 additions & 2 deletions react/src/auth/Auth.action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Api } from 'app/core/Api';
import * as ApiType from 'types/api.types';
import { getHandleActions } from 'app/core/connection';
import * as CustomType from 'types/custom.types';
import { store } from "../app/core/Store";

export type AuthState = {
userProfile?: ApiType.Profile
Expand All @@ -13,6 +14,7 @@ const initState: AuthState = {
};

export const AuthAction = {
name:'auth',
setFcm: async (profile: ApiType.Profile | undefined, fcmToken: string) => {
if (profile) {
const devices = await Api.list<ApiType.Device[]>('/api-device/', {
Expand Down Expand Up @@ -65,7 +67,6 @@ export const AuthAction = {
}).then(res => ({}))
},
socialSign: async (sns: string, uid: string, name: string, email: string | undefined, token: string, fcmToken: string | undefined) => {
// email = undefined // email 없을 때 테스트
const res = await Api.create<{ token: string, profile: ApiType.Profile }>('/social/', {
sns: sns,
uid: uid,
Expand All @@ -80,4 +81,4 @@ export const AuthAction = {
}
}

export default getHandleActions(AuthAction, initState)
store.injectReducer(AuthAction, initState)

0 comments on commit f9de48b

Please sign in to comment.