Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function *handle_get_list({ model, filter, wheres }) {
yield put({ type: 'START_LOADING', model, key: `${model.key}.items` })
const { store } = app.context
const modelState = store.getState().model[model.key]
try {
const { items, total } = yield api(model).query(filter || modelState.filter, wheres || modelState.wheres)
yield put({ type: 'GET_ITEMS', model: model, items: items || [], filter, wheres, count: total })
} catch(err) {
app.error(err)
yield put({ type: 'GET_ITEMS', model: model, items: [], filter, wheres, count: 0 })
}
yield put({ type: 'END_LOADING', model, key: `${model.key}.items` })
}
const data = yield api(model).save(item, partial)
yield put({ type: 'SAVE_ITEM', model, item: data || item, partial, success: true })
if(promise) {
promise.resolve(data)
}
if( message !== false) {
const object = model.title || model.name
const noticeMessage = message || (item.id == undefined ?
_t('Create {{object}} success', { object }) :
_t('Save {{object}} success', { object }))
yield put({ type: '@@xadmin/ADD_NOTICE', payload: {
type: 'success', headline: _t('Success'), message: noticeMessage
} })
}
} catch(err) {
app.error(err)
if(promise) {
promise.reject(err)
}
}
yield put({ type: 'END_LOADING', model , key: `${model.key}.save` })
}
function *handle_get_item({ model, id }) {
yield put({ type: 'START_LOADING', model, key: `${model.key}.get` })
const { _t } = app.context
try {
const item = yield api(model).get(id)
if(item) {
yield put({ type: 'GET_ITEM', model, item, success: true })
}
} catch(err) {
app.error(err)
}
yield put({ type: 'END_LOADING', model, key: `${model.key}.get` })
}
yield put({ type: 'START_LOADING', model, key: `${model.key}.delete_items` })
const { _t } = app.context
const API = api(model)
try {
if(API.batchDelte) {
yield API.batchDelte(items.map(item=>item.id))
} else {
yield all(items.map(item => call([ API, API.delete ], item.id)))
}
for(let item of items) {
yield put({ type: 'SELECT_ITEMS', selected: false, item, model })
}
yield put({ type: 'GET_ITEMS', model })
} catch(err) {
app.error(err)
}
yield put({ type: 'END_LOADING', model, key: `${model.key}.delete_items` })
}
function *handle_delete_item({ model, item, message }) {
yield put({ type: 'START_LOADING', model, key: `${model.key}.delete` })
const { _t } = app.context
try {
yield api(model).delete(item.id)
yield put({ type: 'SELECT_ITEMS', selected: false, item, model })
yield put({ type: '@@xadmin/ADD_NOTICE', payload: {
type: 'success', headline: _t('Success'), message: message || _t('Delete {{object}} success', { object: model.title || model.name })
} })
yield put({ type: 'GET_ITEMS', model })
} catch(err) {
app.error(err)
}
yield put({ type: 'END_LOADING', model, key: `${model.key}.delete` })
}
}
yield put({ type: 'GET_ITEMS', model })
if(promise) {
promise.resolve(ret)
}
if( message !== false) {
const object = model.title || model.name
const noticeMessage = message || _t('Batch Save {{object}} success', { object })
yield put({ type: '@@xadmin/ADD_NOTICE', payload: {
type: 'success', headline: 'Success', message: noticeMessage
} })
}
} catch(err) {
app.error(err)
if(promise) {
promise.reject(err)
}
}
yield put({ type: 'END_LOADING', model , key: `${model.key}.save_items` })
}