Allow redirect action in CM & harmonize pre/post event with Promises

This commit is contained in:
Aurelsicoko 2017-11-29 15:00:50 +01:00
parent 904484df37
commit c971f9c054
12 changed files with 87 additions and 60 deletions

View File

@ -96,7 +96,9 @@ module.exports = function (strapi) {
_.forEach(preLifecycle, (fn, key) => {
if (_.isFunction(target[model.toLowerCase()][fn])) {
collection.schema.pre(key, target[model.toLowerCase()][fn]);
collection.schema.pre(key, function (next) {
target[model.toLowerCase()][fn](this).then(next).catch(err => strapi.log.error(err));
});
}
});

View File

@ -64,7 +64,7 @@ class EditForm extends React.Component {
const validationsIndex = findIndex(this.props.formValidations, ['name', attr]);
const validations = get(this.props.formValidations[validationsIndex], 'validations') || {};
const layout = Object.keys(get(currentLayout, attr, {})).reduce((acc, current) => {
const layout = Object.keys(get(currentLayout, `attributes.${attr}`, {})).reduce((acc, current) => {
acc[current] = isFunction(currentLayout[attr][current]) ?
currentLayout[attr][current](this) :
currentLayout[attr][current];

View File

@ -122,7 +122,6 @@ export class Edit extends React.Component {
componentWillReceiveProps(nextProps) {
if (this.props.editSuccess !== nextProps.editSuccess) {
if (!isEmpty(this.props.location.search)) {
strapi.notification.success('content-manager.success.record.save');
router.push(replace(this.props.location.search, '?redirectUrl=', ''));
} else {
router.push(replace(this.props.location.pathname, 'create', ''));

View File

@ -71,6 +71,8 @@ export function* editRecord(action) {
params,
});
console.log(recordCleaned);
yield put(recordEdited());
strapi.notification.success('content-manager.success.record.save');
} catch (err) {

View File

@ -21,24 +21,27 @@ import {
SET_CURRENT_MODEL_NAME,
} from './constants';
export function changeLimit(limit) {
export function changeLimit(limit, source) {
return {
type: CHANGE_LIMIT,
limit,
limit: limit <= 0 ? 20 : limit,
source,
};
}
export function changePage(page) {
export function changePage(page, source) {
return {
type: CHANGE_PAGE,
page,
page: page <= 0 ? 1 : page,
source,
};
}
export function changeSort(sort) {
export function changeSort(sort, source) {
return {
type: CHANGE_SORT,
sort,
source,
};
}

View File

@ -83,7 +83,7 @@ export class List extends React.Component {
}
if (!isEmpty(nextProps.location.search) && this.props.location.search !== nextProps.location.search) {
this.props.loadRecords();
this.props.loadRecords(this.state.source);
}
}
@ -97,11 +97,11 @@ export class List extends React.Component {
getQueryParameters('sort')) || 'id';
if (!isEmpty(props.location.search)) {
this.props.changePage(toInteger(getQueryParameters('page')));
this.props.changeLimit(toInteger(getQueryParameters('limit')));
this.props.changePage(toInteger(getQueryParameters('page')), this.state.source);
this.props.changeLimit(toInteger(getQueryParameters('limit')), this.state.source);
}
this.props.changeSort(sort);
this.props.changeSort(sort, this.state.source);
// Load records
this.props.loadRecords(this.state.source);
@ -114,7 +114,7 @@ export class List extends React.Component {
}
handleChangeLimit = ({ target }) => {
this.props.changeLimit(parseInt(target.value));
this.props.changeLimit(toInteger(target.value), this.state.source);
router.push({
pathname: this.props.location.pathname,
search: `?page=${this.props.currentPage}&limit=${target.value}&sort=${this.props.sort}&source=${this.state.source}`,
@ -126,7 +126,7 @@ export class List extends React.Component {
pathname: this.props.location.pathname,
search: `?page=${page}&limit=${this.props.limit}&sort=${this.props.sort}&source=${this.state.source}`,
});
this.props.changePage(page);
this.props.changePage(page, this.state.source);
}
handleChangeSort = (sort) => {
@ -134,7 +134,7 @@ export class List extends React.Component {
pathname: this.props.location.pathname,
search: `?page=${this.props.currentPage}&limit=${this.props.limit}&sort=${sort}&source=${this.state.source}`,
});
this.props.changeSort(sort);
this.props.changeSort(sort, this.state.source);
}
handleDelete = (e) => {

View File

@ -0,0 +1,13 @@
const _ = require('lodash');
module.exports = async (ctx, next) => {
const { source } = ctx.request.query;
ctx.request.query.redirectQuery = {};
if (source && _.get(strapi.plugins, [source, 'config', 'layout', 'actions', ctx.request.route.action])) {
ctx.request.query.redirectQuery = _.get(strapi.plugins, [source, 'config', 'layout', 'actions']);
}
await next();
};

View File

@ -5,7 +5,7 @@
"path": "/models",
"handler": "ContentManager.models",
"config": {
"policies": []
"policies": ["routing"]
}
},
{
@ -13,7 +13,7 @@
"path": "/explorer/:model",
"handler": "ContentManager.find",
"config": {
"policies": []
"policies": ["routing"]
}
},
{
@ -21,7 +21,7 @@
"path": "/explorer/:model/count",
"handler": "ContentManager.count",
"config": {
"policies": []
"policies": ["routing"]
}
},
{
@ -29,14 +29,14 @@
"path": "/explorer/:model/:id",
"handler": "ContentManager.findOne",
"config": {
"policies": []
"policies": ["routing"]
}
},{
"method": "POST",
"path": "/explorer/:model",
"handler": "ContentManager.create",
"config": {
"policies": []
"policies": ["routing"]
}
},
{
@ -44,7 +44,7 @@
"path": "/explorer/:model/:id",
"handler": "ContentManager.update",
"config": {
"policies": []
"policies": ["routing"]
}
},
{
@ -52,7 +52,7 @@
"path": "/explorer/:model/:id",
"handler": "ContentManager.delete",
"config": {
"policies": []
"policies": ["routing"]
}
}
]

View File

@ -35,10 +35,12 @@ module.exports = {
},
find: async ctx => {
const { limit, skip = 0, sort, query, queryAttribute, source } = ctx.request.query;
const { limit, skip = 0, sort, query, queryAttribute, source, redirectQuery } = ctx.request.query;
console.log(redirectQuery);
// Find entries using `queries` system
const entries = await strapi.query(ctx.params.model, source).find({
const entries = await strapi.query(ctx.params.model, source)[redirectQuery['find'] || 'find']({
limit,
skip,
sort,
@ -50,10 +52,10 @@ module.exports = {
},
count: async ctx => {
const { source } = ctx.request.query;
const { source, redirectQuery } = ctx.request.query;
// Count using `queries` system
const count = await strapi.query(ctx.params.model, source).count();
const count = await strapi.query(ctx.params.model, source)[redirectQuery['count'] || 'count']();
ctx.body = {
count: _.isNumber(count) ? count : _.toNumber(count)
@ -61,10 +63,10 @@ module.exports = {
},
findOne: async ctx => {
const { source } = ctx.request.query;
const { source, redirectQuery } = ctx.request.query;
// Find an entry using `queries` system
const entry = await strapi.query(ctx.params.model, source).findOne({
const entry = await strapi.query(ctx.params.model, source)[redirectQuery['findOne'] || 'findOne']({
id: ctx.params.id
});
@ -77,10 +79,10 @@ module.exports = {
},
create: async ctx => {
const { source } = ctx.request.query;
const { source, redirectQuery } = ctx.request.query;
// Create an entry using `queries` system
const entryCreated = await strapi.query(ctx.params.model, source).create({
const entryCreated = await strapi.query(ctx.params.model, source)[redirectQuery['create'] || 'create']({
values: ctx.request.body
});
@ -88,10 +90,10 @@ module.exports = {
},
update: async ctx => {
const { source } = ctx.request.query;
const { source, redirectQuery } = ctx.request.query;
// Add current model to the flow of updates.
const entry = strapi.query(ctx.params.model, source).update({
const entry = strapi.query(ctx.params.model, source)[redirectQuery['update'] || 'update']({
id: ctx.params.id,
values: ctx.request.body
});
@ -101,10 +103,10 @@ module.exports = {
},
delete: async ctx => {
const { source } = ctx.request.query;
const { source, redirectQuery } = ctx.request.query;
const params = ctx.params;
const response = await strapi.query(params.model, source).findOne({
const response = await strapi.query(params.model, source)[redirectQuery['findOne'] || 'findOne']({
id: params.id
});
@ -121,11 +123,11 @@ module.exports = {
if (!_.isEmpty(params.values)) {
// Run update to remove all relationships.
await strapi.query(params.model, source).update(params);
await strapi.query(params.model, source)[redirectQuery['update'] || 'update'](params);
}
// Delete an entry using `queries` system
const entryDeleted = await strapi.query(params.model, source).delete({
const entryDeleted = await strapi.query(params.model, source)[redirectQuery['delete'] || 'delete']({
id: params.id
});

View File

@ -1,22 +1,27 @@
module.exports = {
user: {
username: {
className: 'col-md-6'
actions: {
create: 'create'
},
email: {
className: 'col-md-6'
},
username: {
className: 'col-md-6'
},
provider: {
className: 'd-none'
},
resetPasswordToken: {
className: 'd-none'
},
role: {
className: 'd-none'
attributes: {
username: {
className: 'col-md-6'
},
email: {
className: 'col-md-6'
},
username: {
className: 'col-md-6'
},
provider: {
className: 'd-none'
},
resetPasswordToken: {
className: 'd-none'
},
role: {
className: 'd-none'
}
}
}
};

View File

@ -8,9 +8,8 @@ module.exports = {
// Before saving a value.
// Fired before an `insert` or `update` query.
// beforeSave: function (next) {
// // Use `this` to get your current object
// next();
// beforeSave: (model) => {
// return Promise.resolve();
// },
// After saving a value.
@ -21,10 +20,11 @@ module.exports = {
// Before fetching all values.
// Fired before a `fetchAll` operation.
// beforeFetchAll: function (next) {
// // Use `this` to get your current object
// next();
// },
beforeFetchAll: (model) => {
// Use `this` to get your current object
console.log(model);
return Promise.resolve();
},
// After fetching all values.
// Fired after a `fetchAll` operation.

View File

@ -84,7 +84,8 @@ module.exports = {
p.indexOf('hook') !== -1 ||
p.indexOf('middleware') !== -1 ||
p.indexOf('language') !== -1 ||
p.indexOf('queries') !== -1
p.indexOf('queries') !== -1 ||
p.indexOf('layout') !== -1
);
const optional = difference(files, aggregate);