ttk-app-core
Version:
enterprise develop framework
70 lines (61 loc) • 3.09 kB
JavaScript
import { Map, fromJS, toJS } from 'immutable'
import { reducer as MetaReducer } from 'edf-meta-engine'
import { getInitState } from './data'
import moment from 'moment'
import config from './config'
import changeToOption from './utils/changeToOption'
class reducer {
constructor(option) {
this.metaReducer = option.metaReducer
this.config = config.current
}
init = (state, option) => {
const initState = getInitState()
return this.metaReducer.init(state, initState)
}
initOption = (state, { accountList, currencyList, accountDepthList,enabledPeriod,maxDocPeriod }) => {
state = this.metaReducer.sf(state, 'data.other.startAccountList', fromJS(changeToOption(accountList.glAccounts, 'codeAndName', 'code')))
state = this.metaReducer.sf(state, 'data.other.endAccountList', fromJS(changeToOption(accountList.glAccounts, 'codeAndName', 'code')))
state = this.metaReducer.sf(state, 'data.other.currencyList', fromJS(changeToOption(currencyList, 'name', 'id')))
state = this.metaReducer.sf(state, 'data.other.tmpCurrencyList', fromJS(currencyList))
state = this.metaReducer.sf(state, 'data.other.startAccountDepthList', fromJS(changeToOption(accountDepthList.values, 'value', 'key')))
state = this.metaReducer.sf(state, 'data.other.endAccountDepthList', fromJS(changeToOption(accountDepthList.values, 'value', 'key')))
if(enabledPeriod){
state = this.metaReducer.sf(state, 'data.other.enabledDate', fromJS(moment(new Date(enabledPeriod))))
}
if (maxDocPeriod){
state = this.metaReducer.sf(state, 'data.searchValue.date_start', moment(new Date(maxDocPeriod)))
state = this.metaReducer.sf(state, 'data.searchValue.date_end', moment(new Date(maxDocPeriod)))
}
return state
}
load = (state, value) => {
state = this.metaReducer.sf(state, 'data.list', fromJS(value.details))
return state
}
normalSearchChange = (state, { path, value }) => {
return this.metaReducer.sf(state, path, fromJS(value))
}
tableLoading = (state, loading) => {
return this.metaReducer.sf(state, 'data.loading', loading)
}
showOptionsChange = (state, { path, value }) => {
return this.metaReducer.sf(state, path, value)
}
searchUpdate = (state, value) => {
return this.metaReducer.sf(state, 'data.searchValue', fromJS(value))
}
optionsUpdate = (state, value) => {
return this.metaReducer.sf(state, 'data.showOption', fromJS(value))
}
sortReduce = (state, key, value) => {
state = this.metaReducer.sf(state, `data.sort.${key}`, fromJS(value))
state = this.metaReducer.sf(state, 'data.key', Math.random())
return state
}
}
export default function creator(option) {
const metaReducer = new MetaReducer(option),
o = new reducer({...option, metaReducer })
return {...metaReducer, ...o }
}