111 lines
2.4 KiB
Handlebars
111 lines
2.4 KiB
Handlebars
import { put, takeEvery, all, call } from 'redux-saga/effects';
|
|
|
|
import gql from 'graphql-tag';
|
|
|
|
// Action type
|
|
import { StoreAction } from 'store/utils/actions';
|
|
|
|
import { genericMutation, genericQuery } from 'store/utils/apollo';
|
|
|
|
import { config, zeus } from 'config/general';
|
|
|
|
import { deepOmit } from 'store/utils/helper';
|
|
import actions from './actions';
|
|
import selectors from './selectors';
|
|
import * as models from './models';
|
|
|
|
const { Zeus, $ } = zeus;
|
|
|
|
export const treatmentOut = values => {
|
|
const output = {
|
|
...values,
|
|
};
|
|
|
|
return deepOmit(output, ['_id', '__typename', 'createdAt', 'updatedAt', 'get.*']);
|
|
};
|
|
|
|
function* list(action: StoreAction) {
|
|
yield call(
|
|
genericQuery({
|
|
objectReturn: '{{camelCase name}}sGetMany',
|
|
search: action?.payload?.data,
|
|
model: models.{{camelCase name}}ListModel,
|
|
actionRes: actions.list,
|
|
}),
|
|
action,
|
|
);
|
|
}
|
|
|
|
function* detail(action) {
|
|
yield call(
|
|
genericQuery({
|
|
objectReturn: '{{camelCase name}}sGetOne',
|
|
search: {
|
|
id: action?.payload?.id,
|
|
},
|
|
model: models.{{camelCase name}}Model,
|
|
actionRes: actions.detail,
|
|
}),
|
|
action,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* MUTATIONS
|
|
*/
|
|
|
|
function* add(action: StoreAction) {
|
|
yield call(
|
|
genericMutation({
|
|
objectReturn: '{{camelCase name}}sAddOne',
|
|
model: models.{{camelCase name}}Model,
|
|
treatmentOut,
|
|
actionRes: actions.add,
|
|
}),
|
|
action,
|
|
);
|
|
}
|
|
|
|
function* update(action: StoreAction) {
|
|
yield call(
|
|
genericMutation({
|
|
objectReturn: '{{camelCase name}}sEditOne',
|
|
model: models.{{camelCase name}}Model,
|
|
inputs: {
|
|
id: action?.payload?.id,
|
|
},
|
|
treatmentOut,
|
|
actionRes: actions.update,
|
|
}),
|
|
action,
|
|
);
|
|
}
|
|
|
|
function* delete{{pascalCase name}}(action: StoreAction) {
|
|
yield call(
|
|
genericMutation({
|
|
objectReturn: '{{camelCase name}}sDeleteOne',
|
|
model: models.{{camelCase name}}Model,
|
|
inputs: {
|
|
id: action?.payload?.id,
|
|
},
|
|
actionRes: actions.delete,
|
|
}),
|
|
action,
|
|
);
|
|
}
|
|
|
|
function* {{camelCase name}}Watchers() {
|
|
yield all([
|
|
takeEvery(actions.list.request.constant, list),
|
|
takeEvery(actions.detail.request.constant, detail),
|
|
takeEvery(actions.add.request.constant, add),
|
|
takeEvery(actions.update.request.constant, update),
|
|
takeEvery(actions.delete.request.constant, delete{{pascalCase name}}),
|
|
]);
|
|
}
|
|
|
|
export default function* saga() {
|
|
yield call({{camelCase name}}Watchers);
|
|
}
|