342 lines
10 KiB
JavaScript
Raw Normal View History

/*
*
2017-04-21 13:20:58 +02:00
* Edit
*
*/
// Dependencies.
import React from 'react';
import moment from 'moment';
import { connect } from 'react-redux';
import { bindActionCreators, compose } from 'redux';
2017-01-26 18:53:52 +01:00
import { createStructuredSelector } from 'reselect';
import PropTypes from 'prop-types';
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
// Components.
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';
// 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';
import templateObject from 'utils/templateObject';
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
// Styles.
2017-08-30 17:56:52 +02:00
import styles from './styles.scss';
2017-01-26 18:53:52 +01:00
// Actions.
2017-01-26 18:53:52 +01:00
import {
setInitialState,
2017-04-21 17:19:41 +02:00
setCurrentModelName,
2017-05-05 11:40:52 +02:00
setIsCreating,
loadRecord,
2017-04-21 17:19:41 +02:00
setRecordAttribute,
editRecord,
toggleNull,
cancelChanges,
setFormValidations,
setForm,
setFormErrors,
recordEdited,
resetEditSuccess,
2017-01-26 18:53:52 +01:00
} from './actions';
2017-08-30 17:56:52 +02:00
// Selectors.
2017-01-26 18:53:52 +01:00
import {
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,
makeSelectIsRelationComponentNull,
makeSelectForm,
makeSelectFormValidations,
makeSelectFormErrors,
makeSelectDidCheckErrors,
makeSelectEditSuccess,
2017-01-26 18:53:52 +01:00
} from './selectors';
import reducer from './reducer';
import saga from './sagas';
2017-05-11 10:54:44 +02:00
export class Edit extends React.Component {
constructor(props) {
super(props);
2017-09-19 17:19:35 +02:00
this.pluginHeaderActions = [
{
label: 'content-manager.containers.Edit.cancel',
kind: 'secondary',
onClick: this.props.cancelChanges,
type: 'button',
},
{
kind: 'primary',
label: this.props.editing ? 'content-manager.containers.Edit.editing' : 'content-manager.containers.Edit.submit',
onClick: this.handleSubmit,
disabled: this.props.editing,
type: 'submit',
},
];
// this.pluginHeaderSubActions = [
// {
// label: 'content-manager.containers.Edit.returnList',
// kind: 'back',
// onClick: () => router.goBack(),
// type: 'button',
// },
// ];
2017-09-19 17:19:35 +02:00
this.layout = bindLayout.call(this, layout);
}
componentDidMount() {
this.props.setInitialState();
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
}
document.addEventListener('keydown', this.handleSubmitOnEnterPress);
}
componentWillReceiveProps(nextProps) {
if (this.props.editSuccess !== nextProps.editSuccess) {
if (!isEmpty(this.props.location.search)) {
window.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', ''));
}
}
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleSubmitOnEnterPress);
this.props.recordEdited();
this.props.resetEditSuccess();
2017-01-26 18:53:52 +01:00
}
handleChange = (e) => {
let formattedValue = e.target.value;
if (isObject(e.target.value) && e.target.value._isAMomentObject === true) {
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);
}
this.props.setRecordAttribute(e.target.name, formattedValue);
}
handleSubmit = (e) => {
e.preventDefault();
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);
}
}
handleSubmitOnEnterPress = (e) => {
if (e.keyCode === 13) {
this.handleSubmit();
}
}
render() {
if (this.props.loading || !this.props.schema || !this.props.currentModelName) {
return <p>Loading...</p>;
2017-01-26 18:53:52 +01:00
}
// Plugin header config
const primaryKey = this.props.models[this.props.currentModelName].primaryKey;
const mainField = get(this.props.models, `${this.props.currentModelName}.info.mainField`) || primaryKey;
const pluginHeaderTitle = this.props.isCreating ? 'New entry' : templateObject({ mainField }, this.props.record.toJS()).mainField;
const pluginHeaderDescription = this.props.isCreating ? 'New entry' : `#${this.props.record && this.props.record.get(primaryKey)}`;
return (
2017-08-30 17:56:52 +02:00
<div>
<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={{
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
}}
actions={this.pluginHeaderActions}
fullWidth={this.props.isRelationComponentNull}
2017-04-21 17:19:41 +02:00
/>
2017-08-30 17:56:52 +02:00
<div className='row'>
<div className={this.props.isRelationComponentNull ? `col-lg-12` : `col-lg-9`}>
2017-09-19 17:19:35 +02:00
<div className={`${styles.main_wrapper}`}>
<EditForm
record={this.props.record}
currentModelName={this.props.currentModelName}
schema={this.props.schema}
setRecordAttribute={this.props.setRecordAttribute}
onChange={this.handleChange}
onSubmit={this.handleSubmit}
editing={this.props.editing}
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}
/>
</div>
2017-08-30 17:56:52 +02:00
</div>
<div className={`col-lg-3 ${this.props.isRelationComponentNull ? 'hidden-xl-down' : ''}`}>
<div className={styles.sub_wrapper}>
<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}
/>
</div>
2017-04-21 17:19:41 +02:00
</div>
2017-08-30 17:56:52 +02:00
</div>
</div>
</div>
);
}
}
Edit.contextTypes = {
plugins: PropTypes.object,
updatePlugin: PropTypes.func,
};
/* eslint-disable react/require-default-props */
2017-04-21 13:20:58 +02:00
Edit.propTypes = {
cancelChanges: PropTypes.func.isRequired,
currentModelName: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
2017-08-18 17:02:33 +02:00
]).isRequired,
didCheckErrors: PropTypes.bool.isRequired,
editing: PropTypes.bool.isRequired,
editRecord: PropTypes.func.isRequired,
editSuccess: PropTypes.bool.isRequired,
form: PropTypes.object.isRequired,
formErrors: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
]),
formValidations: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
]),
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,
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string,
slug: PropTypes.string,
2017-08-30 17:56:52 +02:00
}),
}).isRequired,
models: PropTypes.oneOfType([
PropTypes.object,
PropTypes.bool,
]).isRequired,
record: PropTypes.oneOfType([
PropTypes.object,
PropTypes.bool,
2017-08-18 17:02:33 +02:00
]).isRequired,
recordEdited: PropTypes.func,
resetEditSuccess: PropTypes.func,
schema: PropTypes.oneOfType([
PropTypes.object,
PropTypes.bool,
2017-08-18 17:02:33 +02:00
]).isRequired,
setCurrentModelName: PropTypes.func.isRequired,
setForm: PropTypes.func.isRequired,
setFormErrors: PropTypes.func.isRequired,
setFormValidations: PropTypes.func.isRequired,
setInitialState: PropTypes.func.isRequired,
setIsCreating: PropTypes.func.isRequired,
setRecordAttribute: PropTypes.func.isRequired,
toggleNull: PropTypes.func.isRequired,
};
2017-01-26 18:53:52 +01:00
const mapStateToProps = createStructuredSelector({
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(),
schema: makeSelectSchema(),
models: makeSelectModels(),
isRelationComponentNull: makeSelectIsRelationComponentNull(),
form: makeSelectForm(),
formValidations: makeSelectFormValidations(),
formErrors: makeSelectFormErrors(),
didCheckErrors: makeSelectDidCheckErrors(),
editSuccess: makeSelectEditSuccess(),
2017-01-26 18:53:52 +01:00
});
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
setInitialState,
setCurrentModelName,
setIsCreating,
loadRecord,
setRecordAttribute,
editRecord,
toggleNull,
cancelChanges,
setFormValidations,
setForm,
setFormErrors,
recordEdited,
resetEditSuccess,
2017-04-21 17:52:18 +02:00
},
dispatch
);
}
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);