2018-02-21 13:59:08 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* EditPage
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
2018-02-21 15:09:33 +01:00
|
|
|
import moment from 'moment';
|
2018-02-21 13:59:08 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
|
|
|
|
import { createStructuredSelector } from 'reselect';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2018-02-26 14:53:13 +01:00
|
|
|
import { get, includes, isEmpty, isObject, toNumber, toString, replace } from 'lodash';
|
2018-02-21 13:59:08 +01:00
|
|
|
import cn from 'classnames';
|
|
|
|
|
|
|
|
|
|
// You can find these components in either
|
|
|
|
|
// ./node_modules/strapi-helper-plugin/lib/src
|
|
|
|
|
// or strapi/packages/strapi-helper-plugin/lib/src
|
|
|
|
|
import BackHeader from 'components/BackHeader';
|
|
|
|
|
import PluginHeader from 'components/PluginHeader';
|
|
|
|
|
|
2018-02-21 15:33:58 +01:00
|
|
|
// Plugin's components
|
|
|
|
|
import Edit from 'components/Edit';
|
|
|
|
|
import EditRelations from 'components/EditRelations';
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
// App selectors
|
|
|
|
|
import { makeSelectModels, makeSelectSchema } from 'containers/App/selectors';
|
|
|
|
|
|
|
|
|
|
import injectReducer from 'utils/injectReducer';
|
|
|
|
|
import injectSaga from 'utils/injectSaga';
|
|
|
|
|
import getQueryParameters from 'utils/getQueryParameters';
|
2018-02-21 15:09:33 +01:00
|
|
|
import { bindLayout } from 'utils/bindLayout';
|
2018-02-21 18:45:18 +01:00
|
|
|
import { checkFormValidity } from 'utils/formValidations';
|
2018-02-21 15:09:33 +01:00
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
import {
|
2018-02-21 15:09:33 +01:00
|
|
|
changeData,
|
|
|
|
|
getData,
|
2018-03-19 11:57:47 +01:00
|
|
|
getLayout,
|
2018-02-21 13:59:08 +01:00
|
|
|
initModelProps,
|
2018-02-21 18:45:18 +01:00
|
|
|
onCancel,
|
2018-02-21 15:33:58 +01:00
|
|
|
resetProps,
|
2018-02-27 16:24:46 +01:00
|
|
|
setFileRelations,
|
2018-02-21 18:45:18 +01:00
|
|
|
setFormErrors,
|
2018-02-22 11:33:01 +01:00
|
|
|
submit,
|
2018-02-21 13:59:08 +01:00
|
|
|
} from './actions';
|
|
|
|
|
|
|
|
|
|
import reducer from './reducer';
|
|
|
|
|
import saga from './saga';
|
|
|
|
|
import makeSelectEditPage from './selectors';
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
|
|
|
|
export class EditPage extends React.Component {
|
|
|
|
|
componentDidMount() {
|
2018-02-21 15:09:33 +01:00
|
|
|
this.props.initModelProps(this.getModelName(), this.isCreating(), this.getSource(), this.getModelAttributes());
|
|
|
|
|
|
|
|
|
|
if (!this.isCreating()) {
|
|
|
|
|
const mainField = get(this.getModel(), 'info.mainField') || this.getModel().primaryKey;
|
|
|
|
|
this.props.getData(this.props.match.params.id, this.getSource(), mainField);
|
2018-03-19 11:57:47 +01:00
|
|
|
} else {
|
|
|
|
|
this.props.getLayout(this.getSource());
|
2018-02-21 15:09:33 +01:00
|
|
|
}
|
2018-02-27 16:24:46 +01:00
|
|
|
|
|
|
|
|
// Get all relations made with the upload plugin
|
2018-03-01 22:03:23 +01:00
|
|
|
const fileRelations = Object.keys(get(this.getSchema(), 'relations', {})).reduce((acc, current) => {
|
2018-03-02 11:49:23 +01:00
|
|
|
const association = get(this.getSchema(), ['relations', current], {});
|
|
|
|
|
|
|
|
|
|
if (association.plugin === 'upload' && association[association.type] === 'file') {
|
2018-03-01 22:03:23 +01:00
|
|
|
const relation = {
|
|
|
|
|
name: current,
|
2018-03-02 11:49:23 +01:00
|
|
|
multiple: association.nature === 'manyToManyMorph',
|
2018-03-01 22:03:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
acc.push(relation);
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
}, []);
|
|
|
|
|
|
2018-02-27 16:24:46 +01:00
|
|
|
// Update the reducer so we can use it to create the appropriate FormData in the saga
|
|
|
|
|
this.props.setFileRelations(fileRelations);
|
2018-02-21 13:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-22 11:33:01 +01:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
if (nextProps.editPage.submitSuccess !== this.props.editPage.submitSuccess) {
|
|
|
|
|
if (!isEmpty(this.props.location.search) && includes(this.props.location.search, '?redirectUrl')) {
|
|
|
|
|
const redirectUrl = this.props.location.search.split('?redirectUrl=')[1];
|
|
|
|
|
|
|
|
|
|
this.props.history.push({
|
|
|
|
|
pathname: redirectUrl.split('?')[0],
|
|
|
|
|
search: redirectUrl.split('?')[1],
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.props.history.push({
|
|
|
|
|
pathname: replace(this.props.location.pathname, '/create', ''),
|
|
|
|
|
search: `?source=${this.getSource()}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 15:33:58 +01:00
|
|
|
componentWillUnmount() {
|
|
|
|
|
this.props.resetProps();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 12:45:00 +01:00
|
|
|
/**
|
|
|
|
|
* Retrive the model's custom layout
|
|
|
|
|
* @return {[type]} [description]
|
|
|
|
|
*/
|
|
|
|
|
getLayout = () => (
|
2018-03-19 12:30:04 +01:00
|
|
|
bindLayout.call(this, this.props.editPage.layout)
|
2018-03-12 12:45:00 +01:00
|
|
|
)
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
/**
|
2018-02-22 10:35:19 +01:00
|
|
|
* Retrieve the model
|
2018-02-21 13:59:08 +01:00
|
|
|
* @type {Object}
|
|
|
|
|
*/
|
|
|
|
|
getModel = () => get(this.props.models, ['models', this.getModelName()]) || get(this.props.models, ['plugins', this.getSource(), 'models', this.getModelName()]);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the model's attributes
|
|
|
|
|
* @return {Object}
|
|
|
|
|
*/
|
|
|
|
|
getModelAttributes = () => this.getModel().attributes;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the model's name
|
|
|
|
|
* @return {String} model's name
|
|
|
|
|
*/
|
|
|
|
|
getModelName = () => this.props.match.params.slug.toLowerCase();
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
/**
|
|
|
|
|
* Retrieve model's schema
|
|
|
|
|
* @return {Object}
|
|
|
|
|
*/
|
|
|
|
|
getSchema = () => this.getSource() !== 'content-manager' ?
|
|
|
|
|
get(this.props.schema, ['plugins', this.getSource(), this.getModelName()])
|
|
|
|
|
: get(this.props.schema, [this.getModelName()]);
|
|
|
|
|
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
/**
|
|
|
|
|
* Retrieve the model's source
|
|
|
|
|
* @return {String}
|
|
|
|
|
*/
|
|
|
|
|
getSource = () => getQueryParameters(this.props.location.search, 'source');
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
handleChange = (e) => {
|
|
|
|
|
let value = e.target.value;
|
|
|
|
|
|
|
|
|
|
// Check if date
|
|
|
|
|
if (isObject(e.target.value) && e.target.value._isAMomentObject === true) {
|
|
|
|
|
value = moment(e.target.value, 'YYYY-MM-DD HH:mm:ss').format();
|
2018-02-26 16:24:55 +01:00
|
|
|
} else if (['float', 'integer', 'biginteger', 'decimal'].indexOf(get(this.getSchema(), ['fields', e.target.name, 'type'])) !== -1) {
|
2018-02-21 15:09:33 +01:00
|
|
|
value = toNumber(e.target.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const target = {
|
2018-02-21 17:14:35 +01:00
|
|
|
name: `record.${e.target.name}`,
|
2018-02-21 15:09:33 +01:00
|
|
|
value,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.props.changeData({ target });
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 18:45:18 +01:00
|
|
|
handleSubmit = (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const formErrors = checkFormValidity(this.generateFormFromRecord(), this.props.editPage.formValidations);
|
|
|
|
|
|
|
|
|
|
if (isEmpty(formErrors)) {
|
2018-02-22 11:33:01 +01:00
|
|
|
this.props.submit();
|
2018-02-21 18:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.props.setFormErrors(formErrors);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
componentDidCatch(error, info) {
|
|
|
|
|
console.log('err', error);
|
|
|
|
|
console.log('info', info);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
isCreating = () => this.props.match.params.id === 'create';
|
2018-02-21 13:59:08 +01:00
|
|
|
|
2018-02-26 14:53:13 +01:00
|
|
|
isRelationComponentNull = () => (
|
|
|
|
|
Object.keys(get(this.getSchema(), 'relations', {})).filter(relation => (
|
|
|
|
|
get(this.getSchema(), ['relations', relation, 'plugin']) !== 'upload'
|
|
|
|
|
)).length === 0
|
|
|
|
|
)
|
2018-02-21 15:33:58 +01:00
|
|
|
|
2018-02-21 18:45:18 +01:00
|
|
|
// NOTE: technical debt that needs to be redone
|
|
|
|
|
generateFormFromRecord = () => (
|
|
|
|
|
Object.keys(this.getModelAttributes()).reduce((acc, current) => {
|
|
|
|
|
acc[current] = get(this.props.editPage.record, current, '');
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}, {})
|
|
|
|
|
)
|
|
|
|
|
|
2018-03-06 09:31:44 +01:00
|
|
|
pluginHeaderActions = () => (
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
label: 'content-manager.containers.Edit.reset',
|
|
|
|
|
kind: 'secondary',
|
|
|
|
|
onClick: this.props.onCancel,
|
|
|
|
|
type: 'button',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
kind: 'primary',
|
|
|
|
|
label: 'content-manager.containers.Edit.submit',
|
|
|
|
|
onClick: this.handleSubmit,
|
|
|
|
|
type: 'submit',
|
|
|
|
|
loader: this.props.editPage.showLoader,
|
2018-03-06 14:02:56 +01:00
|
|
|
style: this.props.editPage.showLoader ? { marginRight: '18px' } : {},
|
2018-03-06 09:31:44 +01:00
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
);
|
2018-02-21 13:59:08 +01:00
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { editPage } = this.props;
|
2018-03-12 12:45:00 +01:00
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
return (
|
|
|
|
|
<div>
|
2018-02-21 18:45:18 +01:00
|
|
|
<form onSubmit={this.handleSubmit}>
|
2018-02-21 15:33:58 +01:00
|
|
|
<BackHeader onClick={() => this.props.history.goBack()} />
|
|
|
|
|
<div className={cn('container-fluid', styles.containerFluid)}>
|
|
|
|
|
<PluginHeader
|
2018-03-06 09:31:44 +01:00
|
|
|
actions={this.pluginHeaderActions()}
|
2018-02-21 15:33:58 +01:00
|
|
|
title={{ id: toString(editPage.pluginHeaderTitle) }}
|
|
|
|
|
/>
|
|
|
|
|
<div className="row">
|
|
|
|
|
<div className={this.isRelationComponentNull() ? 'col-lg-12' : 'col-lg-9'}>
|
|
|
|
|
<div className={styles.main_wrapper}>
|
2018-02-21 17:14:35 +01:00
|
|
|
<Edit
|
|
|
|
|
attributes={this.getModelAttributes()}
|
|
|
|
|
didCheckErrors={editPage.didCheckErrors}
|
2018-02-21 18:45:18 +01:00
|
|
|
formValidations={editPage.formValidations}
|
|
|
|
|
formErrors={editPage.formErrors}
|
2018-03-12 12:45:00 +01:00
|
|
|
layout={this.getLayout()}
|
2018-02-21 17:14:35 +01:00
|
|
|
modelName={this.getModelName()}
|
|
|
|
|
onChange={this.handleChange}
|
|
|
|
|
record={editPage.record}
|
|
|
|
|
schema={this.getSchema()}
|
|
|
|
|
/>
|
2018-02-21 15:33:58 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={cn('col-lg-3', this.isRelationComponentNull() ? 'hidden-xl-down' : '')}>
|
|
|
|
|
<div className={styles.sub_wrapper}>
|
2018-02-22 10:35:19 +01:00
|
|
|
{!this.isRelationComponentNull() && (
|
|
|
|
|
<EditRelations
|
|
|
|
|
currentModelName={this.getModelName()}
|
|
|
|
|
location={this.props.location}
|
|
|
|
|
changeData={this.props.changeData}
|
|
|
|
|
record={editPage.record}
|
|
|
|
|
schema={this.getSchema()}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2018-02-21 15:33:58 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
2018-02-21 13:59:08 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
EditPage.contextTypes = {
|
|
|
|
|
plugins: PropTypes.object,
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
EditPage.defaultProps = {
|
|
|
|
|
models: {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EditPage.propTypes = {
|
2018-02-21 15:09:33 +01:00
|
|
|
changeData: PropTypes.func.isRequired,
|
2018-02-21 13:59:08 +01:00
|
|
|
editPage: PropTypes.object.isRequired,
|
2018-02-21 15:09:33 +01:00
|
|
|
getData: PropTypes.func.isRequired,
|
2018-03-19 11:57:47 +01:00
|
|
|
getLayout: PropTypes.func.isRequired,
|
2018-02-21 13:59:08 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
|
|
|
|
initModelProps: PropTypes.func.isRequired,
|
|
|
|
|
location: PropTypes.object.isRequired,
|
|
|
|
|
match: PropTypes.object.isRequired,
|
|
|
|
|
models: PropTypes.object,
|
2018-02-21 18:45:18 +01:00
|
|
|
onCancel: PropTypes.func.isRequired,
|
2018-02-21 15:33:58 +01:00
|
|
|
resetProps: PropTypes.func.isRequired,
|
2018-02-21 15:09:33 +01:00
|
|
|
schema: PropTypes.object.isRequired,
|
2018-02-27 16:24:46 +01:00
|
|
|
setFileRelations: PropTypes.func.isRequired,
|
2018-02-21 18:45:18 +01:00
|
|
|
setFormErrors: PropTypes.func.isRequired,
|
2018-02-22 11:33:01 +01:00
|
|
|
submit: PropTypes.func.isRequired,
|
2018-02-21 13:59:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
|
return bindActionCreators(
|
|
|
|
|
{
|
2018-02-21 15:09:33 +01:00
|
|
|
changeData,
|
|
|
|
|
getData,
|
2018-03-19 11:57:47 +01:00
|
|
|
getLayout,
|
2018-02-21 13:59:08 +01:00
|
|
|
initModelProps,
|
2018-02-21 18:45:18 +01:00
|
|
|
onCancel,
|
2018-02-21 15:33:58 +01:00
|
|
|
resetProps,
|
2018-02-27 16:24:46 +01:00
|
|
|
setFileRelations,
|
2018-02-21 18:45:18 +01:00
|
|
|
setFormErrors,
|
2018-02-22 11:33:01 +01:00
|
|
|
submit,
|
2018-02-21 13:59:08 +01:00
|
|
|
},
|
|
|
|
|
dispatch,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = createStructuredSelector({
|
|
|
|
|
editPage: makeSelectEditPage(),
|
|
|
|
|
models: makeSelectModels(),
|
|
|
|
|
schema: makeSelectSchema(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const withConnect = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
|
|
|
|
|
const withReducer = injectReducer({ key: 'editPage', reducer });
|
|
|
|
|
const withSaga = injectSaga({ key: 'editPage', saga });
|
|
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
|
withReducer,
|
|
|
|
|
withSaga,
|
|
|
|
|
withConnect,
|
|
|
|
|
)(EditPage);
|