diff --git a/src/ts/commons/redux/saga.ts b/src/ts/commons/redux/saga.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/ts/config/configureRedux.ts b/src/ts/config/configureRedux.ts index da144d5..78c24c2 100644 --- a/src/ts/config/configureRedux.ts +++ b/src/ts/config/configureRedux.ts @@ -1,5 +1,7 @@ import { Action, combineReducers } from 'redux'; import { routerReducer, RouterState } from 'react-router-redux'; +import { SagaIterator } from 'redux-saga'; +import { Effect, fork, ForkEffect } from 'redux-saga/effects'; import * as Member from 'member'; @@ -12,3 +14,26 @@ export const reducer = combineReducers({ member: Member.reducer, router: routerReducer, }); + +export function* sagas(): SagaIterator { + yield fork(Member.sagas); +} + + +// export function* sagas(): Iterator { +// function* itor(values: {[key: string]: string | any}): Iterator { +// for (let name in values) { +// if (values.hasOwnProperty(name)) { +// let saga = values[name]; +// console.log('key:' + name + ' value:' + saga); +// if (typeof saga === 'object') { +// itor(saga); +// } else { +// yield fork(saga); +// } +// } +// } +// } + +// itor(_sagas); +// } diff --git a/src/ts/index.tsx b/src/ts/index.tsx index 281f2d0..f9b98bd 100644 --- a/src/ts/index.tsx +++ b/src/ts/index.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom'; import createSagaMiddleware, { SagaMiddleware } from 'redux-saga'; +import { fork } from 'redux-saga/effects'; import { createHashHistory } from 'history'; @@ -12,8 +13,10 @@ import * as injectTapEventPlugin from 'react-tap-event-plugin'; import * as system from 'commons/util/system'; import configureStore from 'config/configureStore'; +import { sagas } from 'config/configureRedux'; import muiTheme from 'config/configureMuiTheme'; + import App from 'app/views/container/component/App'; injectTapEventPlugin(); @@ -40,6 +43,7 @@ function* app(): any { appContainer, ); + sagaMiddleware.run(sagas); ReactDOM.render( diff --git a/src/ts/member/index.ts b/src/ts/member/index.ts index 761baae..8b68733 100644 --- a/src/ts/member/index.ts +++ b/src/ts/member/index.ts @@ -1,4 +1,7 @@ import { Action, combineReducers } from 'redux'; +import { SagaIterator } from 'redux-saga'; +import { fork, ForkEffect } from 'redux-saga/effects'; + import IModule from 'commons/redux/Module'; import * as signin from './signin'; @@ -16,3 +19,7 @@ export const reducer = combineReducers({ signout: signout.reducer, signup: signup.reducer, }); + +export function* sagas(): SagaIterator { + yield fork(signin.sagas); +} diff --git a/src/ts/member/signin/redux/action/index.ts b/src/ts/member/signin/redux/action/index.ts index 3eff1e8..edaccdd 100644 --- a/src/ts/member/signin/redux/action/index.ts +++ b/src/ts/member/signin/redux/action/index.ts @@ -25,14 +25,14 @@ export const actions: Actions = { requestSuccess(member: Member): Action { return { - type: actionTypes.REQUEST, + type: actionTypes.REQUEST_SUCCESS, payload: member, }; }, requestFailure(error: Error): Action { return { - type: actionTypes.REQUEST, + type: actionTypes.REQUEST_FAILURE, error: error, }; }, diff --git a/src/ts/member/signin/redux/index.ts b/src/ts/member/signin/redux/index.ts index 34d7d96..0ee2789 100644 --- a/src/ts/member/signin/redux/index.ts +++ b/src/ts/member/signin/redux/index.ts @@ -1,2 +1,3 @@ export * from './state'; export * from './reducer'; +export * from './saga'; diff --git a/src/ts/member/signin/redux/saga/index.ts b/src/ts/member/signin/redux/saga/index.ts index 3deb814..dc2c7a2 100644 --- a/src/ts/member/signin/redux/saga/index.ts +++ b/src/ts/member/signin/redux/saga/index.ts @@ -1,43 +1,8 @@ -import { takeLatest, SagaIterator } from 'redux-saga'; -import { call, put } from 'redux-saga/effects'; +import { SagaIterator } from 'redux-saga'; +import { fork, ForkEffect } from 'redux-saga/effects'; -import Action from 'commons/redux/Action'; +import { watchSignin } from './signin'; -import Member from 'member/api/model/Member'; -import actions from '../action'; -import actionTypes from '../action/type'; -import SigninPayload from '../payload/SigninPayload'; - -export function* signin(action: Action): SagaIterator { - try { - const {signinId, signinPw} = action.payload; - // yield put({ - // type: types.SENDING_REQUEST, - // payload: {sendingRequest: true}, - // }); - - // const responseBody = yield call(api.login, signinId, signinPw); - - // if (responseBody.token === undefined) { - // throw new Error(MESSAGES.UNABLE_TO_FIND_TOKEN_IN_LOGIN_RESPONSE); - // } - let member: Member = { - email: 'crusader@loafle.com', - name: 'crusader', - phone: '02-900-9000', - companyName: 'LOAFLE', - }; - yield put(actions.requestSuccess(member)); - } catch (e) { - yield put(actions.requestFailure(e)); - } finally { - // yield put({ - // type: types.SENDING_REQUEST, - // payload: {sendingRequest: false}, - // }); - } -} - -export function* watchSignin(): any { - yield takeLatest(actionTypes.REQUEST, signin); +export function* sagas(): SagaIterator { + yield fork(watchSignin); } diff --git a/src/ts/member/signin/redux/saga/signin.ts b/src/ts/member/signin/redux/saga/signin.ts new file mode 100644 index 0000000..1f7ea65 --- /dev/null +++ b/src/ts/member/signin/redux/saga/signin.ts @@ -0,0 +1,52 @@ +import { channel, takeEvery, takeLatest, SagaIterator } from 'redux-saga'; +import { call, Effect, fork, ForkEffect, put, TakeEffect } from 'redux-saga/effects'; + +import Action from 'commons/redux/Action'; + +import Member from 'member/api/model/Member'; +import actions from '../action'; +import actionTypes from '../action/type'; +import SigninPayload from '../payload/SigninPayload'; + +const apiSignin = (signinId: string, signinPw: string): Promise => { + return new Promise(resolve => { + setTimeout(() => { + let member: Member = { + email: 'crusader@loafle.com', + name: 'crusader', + phone: '02-900-9000', + companyName: 'LOAFLE', + }; + + resolve(member); + }, 1000); + }); +}; + +function* signin(action: Action): Iterable { + try { + const {signinId, signinPw} = action.payload; + // yield put({ + // type: types.SENDING_REQUEST, + // payload: {sendingRequest: true}, + // }); + + const member = yield call(apiSignin, signinId, signinPw); + + // if (responseBody.token === undefined) { + // throw new Error(MESSAGES.UNABLE_TO_FIND_TOKEN_IN_LOGIN_RESPONSE); + // } + yield put(actions.requestSuccess(member)); + } catch (e) { + yield put(actions.requestFailure(e)); + } finally { + // yield put({ + // type: types.SENDING_REQUEST, + // payload: {sendingRequest: false}, + // }); + } +} + +export function* watchSignin(): Iterable { + yield takeLatest(actionTypes.REQUEST, signin); +} diff --git a/src/ts/member/signout/redux/action/index.ts b/src/ts/member/signout/redux/action/index.ts index d9ef053..3cda6f7 100644 --- a/src/ts/member/signout/redux/action/index.ts +++ b/src/ts/member/signout/redux/action/index.ts @@ -18,13 +18,13 @@ export const actions: Actions = { requestSuccess(): Action { return { - type: actionTypes.REQUEST, + type: actionTypes.REQUEST_SUCCESS, }; }, requestFailure(error: Error): Action { return { - type: actionTypes.REQUEST, + type: actionTypes.REQUEST_FAILURE, error: error, }; }, diff --git a/src/ts/member/signup/redux/action/index.ts b/src/ts/member/signup/redux/action/index.ts index e612a63..f53affc 100644 --- a/src/ts/member/signup/redux/action/index.ts +++ b/src/ts/member/signup/redux/action/index.ts @@ -21,14 +21,14 @@ export const actions: Actions = { requestSuccess(member: Member): Action { return { - type: actionTypes.REQUEST, + type: actionTypes.REQUEST_SUCCESS, payload: member, }; }, requestFailure(error: Error): Action { return { - type: actionTypes.REQUEST, + type: actionTypes.REQUEST_FAILURE, error: error, }; },