Change the type of sent data in the EditPage saga

This commit is contained in:
cyril lopez 2018-02-27 16:24:46 +01:00
parent d454530800
commit a6502d15c5
7 changed files with 60 additions and 22 deletions

View File

@ -14,6 +14,7 @@ import {
INIT_MODEL_PROPS, INIT_MODEL_PROPS,
ON_CANCEL, ON_CANCEL,
RESET_PROPS, RESET_PROPS,
SET_FILE_RELATIONS,
SET_FORM_ERRORS, SET_FORM_ERRORS,
SUBMIT, SUBMIT,
SUBMIT_SUCCESS, SUBMIT_SUCCESS,
@ -72,6 +73,13 @@ export function resetProps() {
}; };
} }
export function setFileRelations(fileRelations) {
return {
type: SET_FILE_RELATIONS,
fileRelations,
};
}
export function setFormErrors(formErrors) { export function setFormErrors(formErrors) {
return { return {
type: SET_FORM_ERRORS, type: SET_FORM_ERRORS,

View File

@ -10,6 +10,7 @@ export const GET_DATA_SUCCEEDED = 'ContentManager/EditPage/GET_DATA_SUCCEEDED';
export const INIT_MODEL_PROPS = 'ContentManager/EditPage/INIT_MODEL_PROPS'; export const INIT_MODEL_PROPS = 'ContentManager/EditPage/INIT_MODEL_PROPS';
export const ON_CANCEL = 'ContentManager/EditPage/ON_CANCEL'; export const ON_CANCEL = 'ContentManager/EditPage/ON_CANCEL';
export const RESET_PROPS = 'ContentManager/EditPage/RESET_PROPS'; export const RESET_PROPS = 'ContentManager/EditPage/RESET_PROPS';
export const SET_FILE_RELATIONS = 'ContentManager/EditPage/SET_FILE_RELATIONS';
export const SET_FORM_ERRORS = 'ContentManager/EditPage/SET_FORM_ERRORS'; export const SET_FORM_ERRORS = 'ContentManager/EditPage/SET_FORM_ERRORS';
export const SUBMIT = 'ContentManager/EditPage/SUBMIT'; export const SUBMIT = 'ContentManager/EditPage/SUBMIT';
export const SUBMIT_SUCCESS = 'ContentManager/EditPage/SUBMIT_SUCCESS'; export const SUBMIT_SUCCESS = 'ContentManager/EditPage/SUBMIT_SUCCESS';

View File

@ -41,6 +41,7 @@ import {
initModelProps, initModelProps,
onCancel, onCancel,
resetProps, resetProps,
setFileRelations,
setFormErrors, setFormErrors,
submit, submit,
} from './actions'; } from './actions';
@ -62,6 +63,14 @@ export class EditPage extends React.Component {
const mainField = get(this.getModel(), 'info.mainField') || this.getModel().primaryKey; const mainField = get(this.getModel(), 'info.mainField') || this.getModel().primaryKey;
this.props.getData(this.props.match.params.id, this.getSource(), mainField); this.props.getData(this.props.match.params.id, this.getSource(), mainField);
} }
// Get all relations made with the upload plugin
// TODO: check if collectionName or model === 'upload_file'
const fileRelations = Object.keys(get(this.getSchema(), 'relations', {})).filter(relation => (
get(this.getSchema(), ['relations', relation, 'plugin']) === 'upload'
));
// Update the reducer so we can use it to create the appropriate FormData in the saga
this.props.setFileRelations(fileRelations);
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
@ -256,6 +265,7 @@ EditPage.propTypes = {
onCancel: PropTypes.func.isRequired, onCancel: PropTypes.func.isRequired,
resetProps: PropTypes.func.isRequired, resetProps: PropTypes.func.isRequired,
schema: PropTypes.object.isRequired, schema: PropTypes.object.isRequired,
setFileRelations: PropTypes.func.isRequired,
setFormErrors: PropTypes.func.isRequired, setFormErrors: PropTypes.func.isRequired,
submit: PropTypes.func.isRequired, submit: PropTypes.func.isRequired,
}; };
@ -268,6 +278,7 @@ function mapDispatchToProps(dispatch) {
initModelProps, initModelProps,
onCancel, onCancel,
resetProps, resetProps,
setFileRelations,
setFormErrors, setFormErrors,
submit, submit,
}, },

View File

@ -11,12 +11,14 @@ import {
INIT_MODEL_PROPS, INIT_MODEL_PROPS,
ON_CANCEL, ON_CANCEL,
RESET_PROPS, RESET_PROPS,
SET_FILE_RELATIONS,
SET_FORM_ERRORS, SET_FORM_ERRORS,
SUBMIT_SUCCESS, SUBMIT_SUCCESS,
} from './constants'; } from './constants';
const initialState = fromJS({ const initialState = fromJS({
didCheckErrors: true, didCheckErrors: true,
fileRelations: List([]),
formErrors: List([]), formErrors: List([]),
formValidations: List([]), formValidations: List([]),
isCreating: false, isCreating: false,
@ -52,6 +54,8 @@ function editPageReducer(state = initialState, action) {
.update('record', () => state.get('initialRecord')); .update('record', () => state.get('initialRecord'));
case RESET_PROPS: case RESET_PROPS:
return initialState; return initialState;
case SET_FILE_RELATIONS:
return state.set('fileRelations', List(action.fileRelations));
case SET_FORM_ERRORS: case SET_FORM_ERRORS:
return state return state
.update('didCheckErrors', (v) => v = !v) .update('didCheckErrors', (v) => v = !v)

View File

@ -1,5 +1,5 @@
import { LOCATION_CHANGE } from 'react-router-redux'; import { LOCATION_CHANGE } from 'react-router-redux';
import { get, isArray } from 'lodash'; import { get, isArray, isNumber, isString, map } from 'lodash';
import { import {
call, call,
cancel, cancel,
@ -18,6 +18,7 @@ import templateObject from 'utils/templateObject';
import { getDataSucceeded, setFormErrors, submitSuccess } from './actions'; import { getDataSucceeded, setFormErrors, submitSuccess } from './actions';
import { GET_DATA,SUBMIT } from './constants'; import { GET_DATA,SUBMIT } from './constants';
import { import {
makeSelectFileRelations,
makeSelectIsCreating, makeSelectIsCreating,
makeSelectModelName, makeSelectModelName,
makeSelectRecord, makeSelectRecord,
@ -43,45 +44,52 @@ function* dataGet(action) {
export function* submit() { export function* submit() {
const currentModelName = yield select(makeSelectModelName()); const currentModelName = yield select(makeSelectModelName());
const record = yield select(makeSelectRecord()); const fileRelations = yield select(makeSelectFileRelations());
const recordJSON = record.toJSON();
const source = yield select(makeSelectSource());
const isCreating = yield select(makeSelectIsCreating()); const isCreating = yield select(makeSelectIsCreating());
const record = yield select(makeSelectRecord());
const source = yield select(makeSelectSource());
try { try {
// const recordCleaned = Object.keys(recordJSON).reduce((acc, current) => { const recordCleaned = Object.keys(record).reduce((acc, current) => {
// acc.append(current, cleanData(recordJSON[current], 'value', 'id')); const cleanedData = cleanData(record[current], 'value', 'id');
//
// return acc; if (isString(cleanData) || isNumber(cleanData)) {
// }, new FormData()); acc.append(current, cleanedData);
const recordCleaned = Object.keys(recordJSON).reduce((acc, current) => { } else if (fileRelations.includes(current)) {
acc[current] = cleanData(recordJSON[current], 'value', 'id'); // Don't stringify the file
map(record[current], (file) => acc.append(current, file));
} else {
acc.append(current, JSON.stringify(cleanData(record[current], 'value', 'id')));
}
return acc; return acc;
}, {}); }, new FormData());
console.log(recordCleaned); const id = isCreating ? '' : record.id;
const id = isCreating ? '' : recordCleaned.id;
const params = { source }; const params = { source };
// Change the request helper default headers so we can pass a FormData
const headers = {
'X-Forwarded-Host': 'strapi',
};
const requestUrl = `/content-manager/explorer/${currentModelName}/${id}`; const requestUrl = `/content-manager/explorer/${currentModelName}/${id}`;
// Call our request helper (see 'utils/request') // Call our request helper (see 'utils/request')
// Pass false and false as arguments so the request helper doesn't stringify
// the body and doesn't watch for the server to restart
yield call(request, requestUrl, { yield call(request, requestUrl, {
method: isCreating ? 'POST' : 'PUT', method: isCreating ? 'POST' : 'PUT',
// headers: { headers,
// 'X-Forwarded-Host': 'strapi',
// },
body: recordCleaned, body: recordCleaned,
params, params,
}); }, false, false);
strapi.notification.success('content-manager.success.record.save'); strapi.notification.success('content-manager.success.record.save');
// Redirect the user to the ListPage container
yield put(submitSuccess()); yield put(submitSuccess());
} catch(err) { } catch(err) {
// NOTE: leave the error log // NOTE: leave the error log
console.log(err.response); console.log(err.response);
if (isArray(err.response.payload.message)) { if (isArray(err.response.payload.message)) {
const errors = err.response.payload.message.reduce((acc, current) => { const errors = err.response.payload.message.reduce((acc, current) => {

View File

@ -25,6 +25,11 @@ const makeSelectEditPage = () => createSelector(
* *
* Other specific selectors * Other specific selectors
*/ */
const makeSelectFileRelations = () => createSelector(
selectEditPageDomain(),
(substate) => substate.get('fileRelations').toJS(),
);
const makeSelectIsCreating = () => createSelector( const makeSelectIsCreating = () => createSelector(
selectEditPageDomain(), selectEditPageDomain(),
(substate) => substate.get('isCreating'), (substate) => substate.get('isCreating'),
@ -37,7 +42,7 @@ const makeSelectModelName = () => createSelector(
const makeSelectRecord = () => createSelector( const makeSelectRecord = () => createSelector(
selectEditPageDomain(), selectEditPageDomain(),
(substate) => substate.get('record'), (substate) => substate.get('record').toJS(),
); );
const makeSelectSource = () => createSelector( const makeSelectSource = () => createSelector(
@ -47,6 +52,7 @@ const makeSelectSource = () => createSelector(
export default makeSelectEditPage; export default makeSelectEditPage;
export { export {
makeSelectFileRelations,
makeSelectIsCreating, makeSelectIsCreating,
makeSelectModelName, makeSelectModelName,
makeSelectRecord, makeSelectRecord,

View File

@ -80,7 +80,7 @@ module.exports = {
update: async ctx => { update: async ctx => {
const { source } = ctx.request.query; const { source } = ctx.request.query;
console.log('hhhh', ctx.request.body); console.log('hhhh', ctx.request.body.files.pictures[0]);
try { try {
// Return the last one which is the current model. // Return the last one which is the current model.
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].edit(ctx.params, ctx.request.body, source); ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].edit(ctx.params, ctx.request.body, source);