2017-01-26 17:53:47 +01:00
|
|
|
/*
|
|
|
|
*
|
2017-04-21 13:20:58 +02:00
|
|
|
* Edit
|
2017-01-26 17:53:47 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
// Dependencies.
|
2017-09-26 16:36:28 +02:00
|
|
|
import React from 'react';
|
2017-09-12 17:57:15 +02:00
|
|
|
import moment from 'moment';
|
2017-01-26 17:53:47 +01:00
|
|
|
import { connect } from 'react-redux';
|
2017-09-14 11:10:05 +02:00
|
|
|
import { bindActionCreators, compose } from 'redux';
|
2017-01-26 18:53:52 +01:00
|
|
|
import { createStructuredSelector } from 'reselect';
|
2017-09-18 09:34:29 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2017-10-30 13:40:24 +01:00
|
|
|
import { map, get, isObject, isEmpty, replace, toNumber, toString } from 'lodash';
|
2017-06-20 19:33:21 +02:00
|
|
|
import { router } from 'app';
|
2017-04-21 17:19:41 +02:00
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
// Components.
|
2017-10-31 11:23:49 +01:00
|
|
|
import BackHeader from 'components/BackHeader';
|
2017-05-16 16:32:54 +02:00
|
|
|
import EditForm from 'components/EditForm';
|
2017-06-18 17:23:58 +02:00
|
|
|
import EditFormRelations from 'components/EditFormRelations';
|
2017-08-30 17:56:52 +02:00
|
|
|
import PluginHeader from 'components/PluginHeader';
|
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
// Selectors.
|
|
|
|
import { makeSelectModels, makeSelectSchema } from 'containers/App/selectors';
|
|
|
|
|
|
|
|
// Utils.
|
2017-08-30 17:56:52 +02:00
|
|
|
import injectReducer from 'utils/injectReducer';
|
|
|
|
import injectSaga from 'utils/injectSaga';
|
2017-08-31 16:00:45 +02:00
|
|
|
import templateObject from 'utils/templateObject';
|
2017-09-19 13:49:49 +02:00
|
|
|
import { checkFormValidity } from '../../utils/formValidations';
|
2017-09-19 17:19:35 +02:00
|
|
|
import { bindLayout } from '../../utils/bindLayout';
|
|
|
|
|
|
|
|
// Layout
|
|
|
|
import layout from '../../../../config/layout';
|
2017-08-30 17:56:52 +02:00
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
// Styles.
|
2017-08-30 17:56:52 +02:00
|
|
|
import styles from './styles.scss';
|
2017-01-26 18:53:52 +01:00
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
// Actions.
|
2017-01-26 18:53:52 +01:00
|
|
|
import {
|
2017-06-19 20:17:10 +02:00
|
|
|
setInitialState,
|
2017-04-21 17:19:41 +02:00
|
|
|
setCurrentModelName,
|
2017-05-05 11:40:52 +02:00
|
|
|
setIsCreating,
|
2017-03-20 22:08:49 +01:00
|
|
|
loadRecord,
|
2017-04-21 17:19:41 +02:00
|
|
|
setRecordAttribute,
|
|
|
|
editRecord,
|
2017-09-07 17:16:31 +02:00
|
|
|
toggleNull,
|
2017-09-19 08:34:52 +02:00
|
|
|
cancelChanges,
|
2017-09-19 13:49:49 +02:00
|
|
|
setFormValidations,
|
|
|
|
setForm,
|
|
|
|
setFormErrors,
|
2017-09-19 17:10:14 +02:00
|
|
|
recordEdited,
|
2017-09-20 16:22:04 +02:00
|
|
|
resetEditSuccess,
|
2017-01-26 18:53:52 +01:00
|
|
|
} from './actions';
|
2017-08-30 17:56:52 +02:00
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
// Selectors.
|
|
|
|
|
2017-01-26 18:53:52 +01:00
|
|
|
import {
|
2017-03-20 22:08:49 +01:00
|
|
|
makeSelectRecord,
|
|
|
|
makeSelectLoading,
|
2017-04-21 17:19:41 +02:00
|
|
|
makeSelectCurrentModelName,
|
|
|
|
makeSelectEditing,
|
2017-04-21 17:52:18 +02:00
|
|
|
makeSelectDeleting,
|
2017-05-05 11:40:52 +02:00
|
|
|
makeSelectIsCreating,
|
2017-09-07 17:16:31 +02:00
|
|
|
makeSelectIsRelationComponentNull,
|
2017-09-19 13:49:49 +02:00
|
|
|
makeSelectForm,
|
|
|
|
makeSelectFormValidations,
|
|
|
|
makeSelectFormErrors,
|
|
|
|
makeSelectDidCheckErrors,
|
2017-09-20 16:22:04 +02:00
|
|
|
makeSelectEditSuccess,
|
2017-01-26 18:53:52 +01:00
|
|
|
} from './selectors';
|
2017-01-26 17:53:47 +01:00
|
|
|
|
2017-08-31 17:26:44 +02:00
|
|
|
import reducer from './reducer';
|
|
|
|
import saga from './sagas';
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
export class Edit extends React.Component {
|
2017-09-14 11:10:05 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-19 17:19:35 +02:00
|
|
|
|
2017-09-19 08:34:52 +02:00
|
|
|
this.pluginHeaderActions = [
|
|
|
|
{
|
|
|
|
label: 'content-manager.containers.Edit.cancel',
|
2017-10-03 12:26:52 +02:00
|
|
|
kind: 'secondary',
|
2017-09-19 08:34:52 +02:00
|
|
|
onClick: this.props.cancelChanges,
|
|
|
|
type: 'button',
|
|
|
|
},
|
|
|
|
{
|
2017-10-03 12:26:52 +02:00
|
|
|
kind: 'primary',
|
2017-09-19 08:34:52 +02:00
|
|
|
label: this.props.editing ? 'content-manager.containers.Edit.editing' : 'content-manager.containers.Edit.submit',
|
2017-09-19 13:49:49 +02:00
|
|
|
onClick: this.handleSubmit,
|
2017-09-19 08:34:52 +02:00
|
|
|
disabled: this.props.editing,
|
|
|
|
type: 'submit',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2017-09-19 17:19:35 +02:00
|
|
|
this.layout = bindLayout.call(this, layout);
|
2017-09-14 11:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-06-19 20:17:10 +02:00
|
|
|
this.props.setInitialState();
|
2017-09-25 15:54:43 +02:00
|
|
|
this.props.setCurrentModelName(this.props.match.params.slug.toLowerCase());
|
|
|
|
this.props.setFormValidations(this.props.models[this.props.match.params.slug.toLowerCase()].attributes);
|
|
|
|
this.props.setForm(this.props.models[this.props.match.params.slug.toLowerCase()].attributes);
|
2017-05-05 11:40:52 +02:00
|
|
|
// Detect that the current route is the `create` route or not
|
2017-08-30 17:56:52 +02:00
|
|
|
if (this.props.match.params.id === 'create') {
|
2017-05-05 11:40:52 +02:00
|
|
|
this.props.setIsCreating();
|
|
|
|
} else {
|
2017-08-30 17:56:52 +02:00
|
|
|
this.props.loadRecord(this.props.match.params.id);
|
2017-05-05 11:40:52 +02:00
|
|
|
}
|
2017-09-19 08:34:52 +02:00
|
|
|
|
|
|
|
document.addEventListener('keydown', this.handleSubmitOnEnterPress);
|
|
|
|
}
|
|
|
|
|
2017-09-20 16:22:04 +02:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (this.props.editSuccess !== nextProps.editSuccess) {
|
|
|
|
if (!isEmpty(this.props.location.search)) {
|
2017-09-25 17:27:04 +02:00
|
|
|
window.Strapi.notification.success('content-manager.success.record.save');
|
2017-09-20 16:22:04 +02:00
|
|
|
router.push(replace(this.props.location.search, '?redirectUrl=', ''));
|
|
|
|
} else {
|
|
|
|
router.push(replace(this.props.location.pathname, 'create', ''));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 08:34:52 +02:00
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('keydown', this.handleSubmitOnEnterPress);
|
2017-09-19 17:10:14 +02:00
|
|
|
this.props.recordEdited();
|
2017-09-20 16:22:04 +02:00
|
|
|
this.props.resetEditSuccess();
|
2017-01-26 18:53:52 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
handleChange = (e) => {
|
2017-09-27 15:04:28 +02:00
|
|
|
let formattedValue = e.target.value;
|
|
|
|
|
2017-09-12 17:57:15 +02:00
|
|
|
if (isObject(e.target.value) && e.target.value._isAMomentObject === true) {
|
2017-09-27 15:04:28 +02:00
|
|
|
formattedValue = moment(e.target.value, 'YYYY-MM-DD HH:mm:ss').format();
|
|
|
|
} else if (['float', 'integer', 'bigint'].indexOf(this.props.schema[this.props.currentModelName].fields[e.target.name].type) !== -1) {
|
|
|
|
formattedValue = toNumber(e.target.value);
|
2017-09-12 17:57:15 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:04:28 +02:00
|
|
|
this.props.setRecordAttribute(e.target.name, formattedValue);
|
2017-09-19 08:34:52 +02:00
|
|
|
}
|
2017-08-31 16:00:45 +02:00
|
|
|
|
2017-09-20 16:22:04 +02:00
|
|
|
handleSubmit = (e) => {
|
|
|
|
e.preventDefault();
|
2017-09-19 13:49:49 +02:00
|
|
|
const form = this.props.form.toJS();
|
|
|
|
map(this.props.record.toJS(), (value, key) => form[key] = value);
|
|
|
|
const formErrors = checkFormValidity(form, this.props.formValidations.toJS());
|
|
|
|
|
|
|
|
if (isEmpty(formErrors)) {
|
|
|
|
this.props.editRecord();
|
|
|
|
} else {
|
|
|
|
this.props.setFormErrors(formErrors);
|
|
|
|
}
|
2017-09-19 08:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmitOnEnterPress = (e) => {
|
|
|
|
if (e.keyCode === 13) {
|
2017-10-31 13:14:38 +01:00
|
|
|
this.handleSubmit(e);
|
2017-09-19 08:34:52 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-31 16:00:45 +02:00
|
|
|
|
2017-01-26 17:53:47 +01:00
|
|
|
render() {
|
2017-08-31 16:00:45 +02:00
|
|
|
if (this.props.loading || !this.props.schema || !this.props.currentModelName) {
|
|
|
|
return <p>Loading...</p>;
|
2017-01-26 18:53:52 +01:00
|
|
|
}
|
2017-03-20 22:08:49 +01:00
|
|
|
|
2017-04-25 17:45:57 +02:00
|
|
|
// Plugin header config
|
2017-09-07 17:16:31 +02:00
|
|
|
const primaryKey = this.props.models[this.props.currentModelName].primaryKey;
|
|
|
|
const mainField = get(this.props.models, `${this.props.currentModelName}.info.mainField`) || primaryKey;
|
2017-08-31 16:00:45 +02:00
|
|
|
const pluginHeaderTitle = this.props.isCreating ? 'New entry' : templateObject({ mainField }, this.props.record.toJS()).mainField;
|
2017-09-07 17:16:31 +02:00
|
|
|
const pluginHeaderDescription = this.props.isCreating ? 'New entry' : `#${this.props.record && this.props.record.get(primaryKey)}`;
|
2017-10-31 11:23:49 +01:00
|
|
|
|
2017-01-26 17:53:47 +01:00
|
|
|
return (
|
2017-08-30 17:56:52 +02:00
|
|
|
<div>
|
2017-10-31 11:23:49 +01:00
|
|
|
<BackHeader onClick={() => router.goBack()} />
|
2017-08-30 17:56:52 +02:00
|
|
|
<div className={`container-fluid ${styles.containerFluid}`}>
|
2017-04-21 17:19:41 +02:00
|
|
|
<PluginHeader
|
2017-08-30 17:56:52 +02:00
|
|
|
title={{
|
2017-10-30 13:40:24 +01:00
|
|
|
id: toString(pluginHeaderTitle),
|
2017-08-30 17:56:52 +02:00
|
|
|
}}
|
2017-04-21 17:19:41 +02:00
|
|
|
description={{
|
|
|
|
id: 'plugin-content-manager-description',
|
2017-05-11 10:54:44 +02:00
|
|
|
defaultMessage: `${pluginHeaderDescription}`,
|
2017-04-21 17:19:41 +02:00
|
|
|
}}
|
2017-09-19 08:34:52 +02:00
|
|
|
actions={this.pluginHeaderActions}
|
2017-09-07 17:16:31 +02:00
|
|
|
fullWidth={this.props.isRelationComponentNull}
|
2017-04-21 17:19:41 +02:00
|
|
|
/>
|
2017-08-30 17:56:52 +02:00
|
|
|
<div className='row'>
|
2017-09-19 13:56:51 +02:00
|
|
|
<div className={this.props.isRelationComponentNull ? `col-lg-12` : `col-lg-9`}>
|
2017-09-19 17:19:35 +02:00
|
|
|
<div className={`${styles.main_wrapper}`}>
|
2017-09-19 08:34:52 +02:00
|
|
|
<EditForm
|
|
|
|
record={this.props.record}
|
|
|
|
currentModelName={this.props.currentModelName}
|
|
|
|
schema={this.props.schema}
|
|
|
|
setRecordAttribute={this.props.setRecordAttribute}
|
2017-10-12 17:07:02 +02:00
|
|
|
onChange={this.handleChange}
|
|
|
|
onSubmit={this.handleSubmit}
|
2017-09-19 08:34:52 +02:00
|
|
|
editing={this.props.editing}
|
2017-09-19 13:49:49 +02:00
|
|
|
formErrors={this.props.formErrors.toJS()}
|
|
|
|
didCheckErrors={this.props.didCheckErrors}
|
|
|
|
formValidations={this.props.formValidations.toJS()}
|
2017-09-19 17:19:35 +02:00
|
|
|
layout={this.layout}
|
2017-09-19 08:34:52 +02:00
|
|
|
/>
|
2017-08-31 16:00:45 +02:00
|
|
|
</div>
|
2017-08-30 17:56:52 +02:00
|
|
|
</div>
|
2017-09-19 13:56:51 +02:00
|
|
|
<div className={`col-lg-3 ${this.props.isRelationComponentNull ? 'hidden-xl-down' : ''}`}>
|
2017-09-07 17:16:31 +02:00
|
|
|
<div className={styles.sub_wrapper}>
|
2017-09-19 08:34:52 +02:00
|
|
|
<EditFormRelations
|
|
|
|
currentModelName={this.props.currentModelName}
|
|
|
|
record={this.props.record}
|
|
|
|
schema={this.props.schema}
|
|
|
|
setRecordAttribute={this.props.setRecordAttribute}
|
|
|
|
isNull={this.props.isRelationComponentNull}
|
|
|
|
toggleNull={this.props.toggleNull}
|
|
|
|
/>
|
2017-09-07 17:16:31 +02:00
|
|
|
</div>
|
2017-04-21 17:19:41 +02:00
|
|
|
</div>
|
2017-08-30 17:56:52 +02:00
|
|
|
</div>
|
2017-03-20 22:08:49 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-01-26 17:53:47 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 14:35:08 +02:00
|
|
|
|
|
|
|
Edit.contextTypes = {
|
|
|
|
plugins: PropTypes.object,
|
|
|
|
updatePlugin: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2017-09-19 13:49:49 +02:00
|
|
|
/* eslint-disable react/require-default-props */
|
2017-04-21 13:20:58 +02:00
|
|
|
Edit.propTypes = {
|
2017-09-19 08:34:52 +02:00
|
|
|
cancelChanges: PropTypes.func.isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
currentModelName: PropTypes.oneOfType([
|
|
|
|
PropTypes.bool,
|
|
|
|
PropTypes.string,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-09-19 13:49:49 +02:00
|
|
|
didCheckErrors: PropTypes.bool.isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
editing: PropTypes.bool.isRequired,
|
|
|
|
editRecord: PropTypes.func.isRequired,
|
2017-09-20 16:22:04 +02:00
|
|
|
editSuccess: PropTypes.bool.isRequired,
|
2017-09-19 13:49:49 +02:00
|
|
|
form: PropTypes.object.isRequired,
|
|
|
|
formErrors: PropTypes.oneOfType([
|
|
|
|
PropTypes.array,
|
|
|
|
PropTypes.object,
|
|
|
|
]),
|
|
|
|
formValidations: PropTypes.oneOfType([
|
|
|
|
PropTypes.array,
|
|
|
|
PropTypes.object,
|
|
|
|
]),
|
2017-09-18 09:34:29 +02:00
|
|
|
isCreating: PropTypes.bool.isRequired,
|
|
|
|
isRelationComponentNull: PropTypes.bool.isRequired,
|
|
|
|
loading: PropTypes.bool.isRequired,
|
|
|
|
loadRecord: PropTypes.func.isRequired,
|
2017-09-20 15:21:58 +02:00
|
|
|
location: PropTypes.object.isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
match: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
slug: PropTypes.string,
|
2017-08-30 17:56:52 +02:00
|
|
|
}),
|
|
|
|
}).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
models: PropTypes.oneOfType([
|
|
|
|
PropTypes.object,
|
|
|
|
PropTypes.bool,
|
2017-08-31 16:00:45 +02:00
|
|
|
]).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
record: PropTypes.oneOfType([
|
|
|
|
PropTypes.object,
|
|
|
|
PropTypes.bool,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-09-19 17:10:14 +02:00
|
|
|
recordEdited: PropTypes.func,
|
2017-09-20 16:22:04 +02:00
|
|
|
resetEditSuccess: PropTypes.func,
|
2017-09-18 09:34:29 +02:00
|
|
|
schema: PropTypes.oneOfType([
|
|
|
|
PropTypes.object,
|
|
|
|
PropTypes.bool,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
setCurrentModelName: PropTypes.func.isRequired,
|
2017-09-19 13:49:49 +02:00
|
|
|
setForm: PropTypes.func.isRequired,
|
|
|
|
setFormErrors: PropTypes.func.isRequired,
|
|
|
|
setFormValidations: PropTypes.func.isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
setInitialState: PropTypes.func.isRequired,
|
|
|
|
setIsCreating: PropTypes.func.isRequired,
|
|
|
|
setRecordAttribute: PropTypes.func.isRequired,
|
|
|
|
toggleNull: PropTypes.func.isRequired,
|
2017-03-20 22:08:49 +01:00
|
|
|
};
|
|
|
|
|
2017-01-26 18:53:52 +01:00
|
|
|
const mapStateToProps = createStructuredSelector({
|
2017-03-20 22:08:49 +01:00
|
|
|
record: makeSelectRecord(),
|
|
|
|
loading: makeSelectLoading(),
|
2017-04-21 17:19:41 +02:00
|
|
|
currentModelName: makeSelectCurrentModelName(),
|
|
|
|
editing: makeSelectEditing(),
|
2017-04-21 17:52:18 +02:00
|
|
|
deleting: makeSelectDeleting(),
|
2017-05-05 11:40:52 +02:00
|
|
|
isCreating: makeSelectIsCreating(),
|
2017-06-17 17:01:50 +02:00
|
|
|
schema: makeSelectSchema(),
|
2017-08-31 16:00:45 +02:00
|
|
|
models: makeSelectModels(),
|
2017-09-07 17:16:31 +02:00
|
|
|
isRelationComponentNull: makeSelectIsRelationComponentNull(),
|
2017-09-19 13:49:49 +02:00
|
|
|
form: makeSelectForm(),
|
|
|
|
formValidations: makeSelectFormValidations(),
|
|
|
|
formErrors: makeSelectFormErrors(),
|
|
|
|
didCheckErrors: makeSelectDidCheckErrors(),
|
2017-09-20 16:22:04 +02:00
|
|
|
editSuccess: makeSelectEditSuccess(),
|
2017-01-26 18:53:52 +01:00
|
|
|
});
|
2017-01-26 17:53:47 +01:00
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
2017-09-14 11:10:05 +02:00
|
|
|
return bindActionCreators(
|
|
|
|
{
|
|
|
|
setInitialState,
|
|
|
|
setCurrentModelName,
|
|
|
|
setIsCreating,
|
|
|
|
loadRecord,
|
|
|
|
setRecordAttribute,
|
|
|
|
editRecord,
|
|
|
|
toggleNull,
|
2017-09-19 08:34:52 +02:00
|
|
|
cancelChanges,
|
2017-09-19 13:49:49 +02:00
|
|
|
setFormValidations,
|
|
|
|
setForm,
|
|
|
|
setFormErrors,
|
2017-09-19 17:10:14 +02:00
|
|
|
recordEdited,
|
2017-09-20 16:22:04 +02:00
|
|
|
resetEditSuccess,
|
2017-04-21 17:52:18 +02:00
|
|
|
},
|
2017-09-14 11:10:05 +02:00
|
|
|
dispatch
|
|
|
|
);
|
2017-01-26 17:53:47 +01:00
|
|
|
}
|
|
|
|
|
2017-08-30 17:56:52 +02:00
|
|
|
const withConnect = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
|
|
|
const withReducer = injectReducer({ key: 'edit', reducer });
|
|
|
|
const withSaga = injectSaga({ key: 'edit', saga });
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withReducer,
|
|
|
|
withSaga,
|
|
|
|
withConnect,
|
|
|
|
)(Edit);
|