update it

This commit is contained in:
Sukchan Lee 2017-06-18 17:52:39 +09:00
parent 34b5ba903f
commit 6d5357aefa
7 changed files with 199 additions and 9 deletions

View File

@ -39,6 +39,7 @@
"react-transition-group": "^1.1.3",
"redux": "^3.6.0",
"redux-actions": "^2.0.3",
"redux-crud-store": "^5.2.2",
"redux-saga": "^0.15.3",
"styled-components": "2.0.0-19",
"yarn": "^0.24.5"

View File

@ -0,0 +1,26 @@
import {
fetchCollection, fetchRecord, createRecord, updateRecord, deleteRecord
} from 'redux-crud-store'
const MODEL = 'subscribers'
const PATH = '/Subscriber'
export function fetchSubscribers(params = {}) {
return fetchCollection(MODEL, PATH, params)
}
export function fetchSubscriber(id, params = {}) {
return fetchRecord(MODEL, id, `${PATH}/${id}`, params)
}
export function createSubscriber(data = {}) {
return createRecord(MODEL, PATH, data)
}
export function updateSubscriber(id, data = {}) {
return updateRecord(MODEL, id, `${PATH}/${id}`, data)
}
export function deleteSubscriber(id) {
return deleteRecord(MODEL, id, `${PATH}/${id}`)
}

View File

@ -1,14 +1,43 @@
import { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux'
import { fetchSubscribers } from 'actions/subscriber';
import { select } from 'redux-crud-store'
class SubscriberContainer extends Component {
componentWillMount() {
const { subscribers, dispatch } = this.props
if (subscribers.needsFetch) {
dispatch(subscribers.fetch)
}
}
componentWillReceiveProps(nextProps) {
const { subscribers } = nextProps
const { dispatch } = this.props
if (subscribers.needsFetch) {
dispatch(subscribers.fetch)
}
}
render() {
return (
<div>
<p>asdfsdfasdf</p>
const { subscribers } = this.props
if (subscribers.isLoading) {
return <div>
<p>loading...</p>
</div>
)
} else {
return <div>
<p>success...</p>
</div>
}
}
}
export default SubscriberContainer;
function mapStateToProps(state, ownProps) {
return { subscribers: select(fetchSubscribers(), state.db) }
}
export default connect(mapStateToProps)(SubscriberContainer)

View File

@ -1,9 +1,11 @@
import { combineReducers } from 'redux';
import auth from './auth';
import { crudReducer } from 'redux-crud-store'
import ui from './ui';
export default combineReducers({
auth,
db: crudReducer,
ui
});

117
webui/src/sagas/crud.js Normal file
View File

@ -0,0 +1,117 @@
/* @flow */
/* global Generator */
/*
import 'regenerator-runtime/runtime'
import { takeEvery, fork, put, call } from 'redux-saga/effects'
import {
FETCH, FETCH_ONE, CREATE, UPDATE, DELETE, API_CALL, GARBAGE_COLLECT
} from './actionTypes'
// TODO: The `Effect` type is not actually defined. Because 'redux-saga' does
// not use @flow annotations, flow pretends that this import succeeds.
import type { Effect } from 'redux-saga'
import type { CrudAction } from './actionTypes'
// Generator type parameters are: Generator<+Yield,+Return,-Next>
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
function* garbageCollector() {
yield call(delay, 10 * 60 * 1000) // initial 10 minute delay
for (;;) {
yield call(delay, 5 * 60 * 1000) // every 5 minutes thereafter
yield put({ type: GARBAGE_COLLECT, meta: { now: Date.now() } })
}
}
export const apiGeneric = (apiClient: Object) =>
function* _apiGeneric(action: CrudAction<any>): Generator<Effect, void, any> {
const { method, path, params, data, fetchConfig } = action.payload
const { success, failure } = action.meta
const meta = {
...action.meta,
fetchTime: Date.now()
}
try {
const response = yield call(apiClient[method], path, { params, data, fetchConfig })
yield put({ meta, type: success, payload: response })
} catch (error) {
yield put({ meta, type: failure, payload: error, error: true })
}
}
const watchFetch = (apiClient) => function* _watchFetch() {
yield* takeEvery(FETCH, apiGeneric(apiClient))
}
const watchFetchOne = (apiClient) => function* _watchFetchOne() {
yield* takeEvery(FETCH_ONE, apiGeneric(apiClient))
}
const watchCreate = (apiClient) => function* _watchCreate() {
yield* takeEvery(CREATE, apiGeneric(apiClient))
}
const watchUpdate = (apiClient) => function* _watchUpdate() {
yield* takeEvery(UPDATE, apiGeneric(apiClient))
}
const watchDelete = (apiClient) => function* _watchDelete() {
yield* takeEvery(DELETE, apiGeneric(apiClient))
}
const watchApiCall = (apiClient) => function* _watchApiCall() {
yield* takeEvery(API_CALL, apiGeneric(apiClient))
}
export default function crudSaga(apiClient: Object) {
return function* _crudSaga(): Generator<Effect, void, any> {
yield [
fork(watchFetch(apiClient)),
fork(watchFetchOne(apiClient)),
fork(watchCreate(apiClient)),
fork(watchUpdate(apiClient)),
fork(watchDelete(apiClient)),
fork(watchApiCall(apiClient)),
fork(garbageCollector)
]
}
}
*/
import { all, takeEvery, put, call } from 'redux-saga/effects';
import { ApiClient } from 'redux-crud-store';
import { crudActions } from 'redux-crud-store';
const apiClient = new ApiClient({ basePath: '/api/db' })
function* apiGeneric(action) {
const { method, path, params, data, fetchConfig } = action.payload
const { success, failure } = action.meta
const meta = {
...action.meta,
fetchTime: Date.now()
}
try {
const response = yield call(apiClient[method], path, { params, data, fetchConfig })
yield put({ meta, type: success, payload: response })
} catch (error) {
yield put({ meta, type: failure, payload: error, error: true })
}
}
function* watchFetch() {
yield takeEvery(crudActions.FETCH, apiGeneric)
}
export default function* () {
yield all([
watchFetch()
])
}

View File

@ -1,8 +1,10 @@
import { all } from 'redux-saga/effects';
import auth from './auth';
import crud from './crud';
export default function* rootSaga() {
yield all([
auth()
auth(),
crud()
])
}

View File

@ -2149,7 +2149,7 @@ ieee754@^1.1.4:
version "1.1.8"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.8.tgz#be33d40ac10ef1926701f6f08a2d86fbfd1ad3e4"
immutable@^3.8.1:
"immutable@>= 3.7.6", immutable@^3.8.1:
version "3.8.1"
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.1.tgz#200807f11ab0f72710ea485542de088075f68cd2"
@ -2531,6 +2531,10 @@ lodash.isarray@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-4.0.0.tgz#2aca496b28c4ca6d726715313590c02e6ea34403"
lodash.isequal@^4.1.2:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
lodash.isnumber@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
@ -3545,7 +3549,16 @@ redux-actions@^2.0.3:
lodash-es "^4.17.4"
reduce-reducers "^0.1.0"
redux-saga@^0.15.3:
redux-crud-store@^5.2.2:
version "5.2.2"
resolved "https://registry.yarnpkg.com/redux-crud-store/-/redux-crud-store-5.2.2.tgz#b6aa18d7dd32f215eb060a5a38ff156ce392ca23"
dependencies:
immutable ">= 3.7.6"
lodash.isequal "^4.1.2"
redux-saga ">= 0.9.5"
regenerator-runtime "^0.10.5"
"redux-saga@>= 0.9.5", redux-saga@^0.15.3:
version "0.15.3"
resolved "https://registry.yarnpkg.com/redux-saga/-/redux-saga-0.15.3.tgz#be2b86b4ad46bf0d84fcfcb0ca96cfc33db91acb"
@ -3562,7 +3575,7 @@ regenerate@^1.2.1:
version "1.3.2"
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.2.tgz#d1941c67bad437e1be76433add5b385f95b19260"
regenerator-runtime@^0.10.0:
regenerator-runtime@^0.10.0, regenerator-runtime@^0.10.5:
version "0.10.5"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658"