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) => { _.forEach(preLifecycle, (fn, key) => {
if (_.isFunction(target[model.toLowerCase()][fn])) { 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 validationsIndex = findIndex(this.props.formValidations, ['name', attr]);
const validations = get(this.props.formValidations[validationsIndex], 'validations') || {}; 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]) ? acc[current] = isFunction(currentLayout[attr][current]) ?
currentLayout[attr][current](this) : currentLayout[attr][current](this) :
currentLayout[attr][current]; currentLayout[attr][current];

View File

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

View File

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

View File

@ -21,24 +21,27 @@ import {
SET_CURRENT_MODEL_NAME, SET_CURRENT_MODEL_NAME,
} from './constants'; } from './constants';
export function changeLimit(limit) { export function changeLimit(limit, source) {
return { return {
type: CHANGE_LIMIT, type: CHANGE_LIMIT,
limit, limit: limit <= 0 ? 20 : limit,
source,
}; };
} }
export function changePage(page) { export function changePage(page, source) {
return { return {
type: CHANGE_PAGE, type: CHANGE_PAGE,
page, page: page <= 0 ? 1 : page,
source,
}; };
} }
export function changeSort(sort) { export function changeSort(sort, source) {
return { return {
type: CHANGE_SORT, type: CHANGE_SORT,
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) { 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'; getQueryParameters('sort')) || 'id';
if (!isEmpty(props.location.search)) { if (!isEmpty(props.location.search)) {
this.props.changePage(toInteger(getQueryParameters('page'))); this.props.changePage(toInteger(getQueryParameters('page')), this.state.source);
this.props.changeLimit(toInteger(getQueryParameters('limit'))); this.props.changeLimit(toInteger(getQueryParameters('limit')), this.state.source);
} }
this.props.changeSort(sort); this.props.changeSort(sort, this.state.source);
// Load records // Load records
this.props.loadRecords(this.state.source); this.props.loadRecords(this.state.source);
@ -114,7 +114,7 @@ export class List extends React.Component {
} }
handleChangeLimit = ({ target }) => { handleChangeLimit = ({ target }) => {
this.props.changeLimit(parseInt(target.value)); this.props.changeLimit(toInteger(target.value), this.state.source);
router.push({ router.push({
pathname: this.props.location.pathname, pathname: this.props.location.pathname,
search: `?page=${this.props.currentPage}&limit=${target.value}&sort=${this.props.sort}&source=${this.state.source}`, 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, pathname: this.props.location.pathname,
search: `?page=${page}&limit=${this.props.limit}&sort=${this.props.sort}&source=${this.state.source}`, 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) => { handleChangeSort = (sort) => {
@ -134,7 +134,7 @@ export class List extends React.Component {
pathname: this.props.location.pathname, pathname: this.props.location.pathname,
search: `?page=${this.props.currentPage}&limit=${this.props.limit}&sort=${sort}&source=${this.state.source}`, 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) => { 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", "path": "/models",
"handler": "ContentManager.models", "handler": "ContentManager.models",
"config": { "config": {
"policies": [] "policies": ["routing"]
} }
}, },
{ {
@ -13,7 +13,7 @@
"path": "/explorer/:model", "path": "/explorer/:model",
"handler": "ContentManager.find", "handler": "ContentManager.find",
"config": { "config": {
"policies": [] "policies": ["routing"]
} }
}, },
{ {
@ -21,7 +21,7 @@
"path": "/explorer/:model/count", "path": "/explorer/:model/count",
"handler": "ContentManager.count", "handler": "ContentManager.count",
"config": { "config": {
"policies": [] "policies": ["routing"]
} }
}, },
{ {
@ -29,14 +29,14 @@
"path": "/explorer/:model/:id", "path": "/explorer/:model/:id",
"handler": "ContentManager.findOne", "handler": "ContentManager.findOne",
"config": { "config": {
"policies": [] "policies": ["routing"]
} }
},{ },{
"method": "POST", "method": "POST",
"path": "/explorer/:model", "path": "/explorer/:model",
"handler": "ContentManager.create", "handler": "ContentManager.create",
"config": { "config": {
"policies": [] "policies": ["routing"]
} }
}, },
{ {
@ -44,7 +44,7 @@
"path": "/explorer/:model/:id", "path": "/explorer/:model/:id",
"handler": "ContentManager.update", "handler": "ContentManager.update",
"config": { "config": {
"policies": [] "policies": ["routing"]
} }
}, },
{ {
@ -52,7 +52,7 @@
"path": "/explorer/:model/:id", "path": "/explorer/:model/:id",
"handler": "ContentManager.delete", "handler": "ContentManager.delete",
"config": { "config": {
"policies": [] "policies": ["routing"]
} }
} }
] ]

View File

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

View File

@ -1,5 +1,9 @@
module.exports = { module.exports = {
user: { user: {
actions: {
create: 'create'
},
attributes: {
username: { username: {
className: 'col-md-6' className: 'col-md-6'
}, },
@ -19,4 +23,5 @@ module.exports = {
className: 'd-none' className: 'd-none'
} }
} }
}
}; };

View File

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

View File

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