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-01-26 17:53:47 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2017-08-30 17:56:52 +02:00
|
|
|
import { compose } from 'redux';
|
2017-01-26 18:53:52 +01:00
|
|
|
import { createStructuredSelector } from 'reselect';
|
2017-08-31 16:00:45 +02:00
|
|
|
import { get } 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-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-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-04-21 17:52:18 +02:00
|
|
|
deleteRecord,
|
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-01-26 18:53:52 +01:00
|
|
|
} from './selectors';
|
2017-01-26 17:53:47 +01:00
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
export class Edit extends React.Component {
|
2017-01-26 18:53:52 +01:00
|
|
|
componentWillMount() {
|
2017-06-19 20:17:10 +02:00
|
|
|
this.props.setInitialState();
|
2017-08-30 17:56:52 +02:00
|
|
|
this.props.setCurrentModelName(this.props.match.params.slug.toLowerCase());
|
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-01-26 18:53:52 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
handleChange = (e) => {
|
|
|
|
this.props.setRecordAttribute(e.target.name, e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
|
|
|
this.props.editRecord();
|
|
|
|
};
|
|
|
|
|
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-08-31 16:00:45 +02:00
|
|
|
const content = (
|
|
|
|
<EditForm
|
|
|
|
record={this.props.record}
|
|
|
|
currentModelName={this.props.currentModelName}
|
|
|
|
schema={this.props.schema}
|
|
|
|
setRecordAttribute={this.props.setRecordAttribute}
|
|
|
|
handleChange={this.handleChange}
|
|
|
|
handleSubmit={this.handleSubmit}
|
|
|
|
editing={this.props.editing}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const relations = (
|
|
|
|
<EditFormRelations
|
|
|
|
currentModelName={this.props.currentModelName}
|
|
|
|
record={this.props.record}
|
|
|
|
schema={this.props.schema}
|
|
|
|
setRecordAttribute={this.props.setRecordAttribute}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2017-05-05 11:40:52 +02:00
|
|
|
// Define plugin header actions
|
2017-05-11 10:54:44 +02:00
|
|
|
const pluginHeaderActions = [
|
|
|
|
{
|
2017-08-16 17:05:02 +02:00
|
|
|
label: 'content-manager.containers.Edit.cancel',
|
2017-08-30 17:56:52 +02:00
|
|
|
handlei18n: true,
|
|
|
|
buttonBackground: 'secondary',
|
2017-08-31 16:00:45 +02:00
|
|
|
buttonSize: 'buttonMd',
|
|
|
|
onClick: () => router.push(`/plugins/content-manager/${this.props.currentModelName}`),
|
2017-05-11 10:54:44 +02:00
|
|
|
},
|
|
|
|
{
|
2017-08-30 17:56:52 +02:00
|
|
|
handlei18n: true,
|
|
|
|
buttonBackground: 'primary',
|
|
|
|
buttonSize: 'buttonLg',
|
2017-08-16 17:05:02 +02:00
|
|
|
label: this.props.editing ? 'content-manager.containers.Edit.editing' : 'content-manager.containers.Edit.submit',
|
2017-05-11 10:54:44 +02:00
|
|
|
onClick: this.props.editRecord,
|
|
|
|
disabled: this.props.editing,
|
|
|
|
},
|
|
|
|
];
|
2017-04-21 17:19:41 +02:00
|
|
|
|
2017-08-31 16:00:45 +02:00
|
|
|
const pluginHeaderSubActions = [
|
|
|
|
{
|
|
|
|
label: 'content-manager.containers.Edit.returnList',
|
|
|
|
handlei18n: true,
|
|
|
|
buttonBackground: 'back',
|
|
|
|
onClick: () => router.goBack(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2017-05-05 11:40:52 +02:00
|
|
|
// Add the `Delete` button only in edit mode
|
2017-08-30 17:56:52 +02:00
|
|
|
// if (!this.props.isCreating) {
|
|
|
|
// pluginHeaderActions.push({
|
|
|
|
// label: 'content-manager.containers.Edit.delete',
|
|
|
|
// class: 'btn-danger',
|
|
|
|
// onClick: this.props.deleteRecord,
|
|
|
|
// disabled: this.props.deleting,
|
|
|
|
// });
|
|
|
|
// }
|
2017-05-05 11:40:52 +02:00
|
|
|
|
2017-04-25 17:45:57 +02:00
|
|
|
// Plugin header config
|
2017-08-31 16:00:45 +02:00
|
|
|
const mainField = get(this.props.models, `${this.props.currentModelName}.info.mainField`) || this.props.record.first();
|
|
|
|
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('id')}`;
|
2017-04-25 17:45:57 +02:00
|
|
|
|
2017-01-26 17:53:47 +01:00
|
|
|
return (
|
2017-08-30 17:56:52 +02:00
|
|
|
<div>
|
|
|
|
<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: pluginHeaderTitle,
|
|
|
|
}}
|
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-05-05 11:40:52 +02:00
|
|
|
actions={pluginHeaderActions}
|
2017-08-31 16:00:45 +02:00
|
|
|
subActions={pluginHeaderSubActions}
|
2017-04-21 17:19:41 +02:00
|
|
|
/>
|
2017-08-30 17:56:52 +02:00
|
|
|
<div className='row'>
|
2017-08-31 16:00:45 +02:00
|
|
|
<div className={`col-lg-9`}>
|
|
|
|
<div className={styles.main_wrapper}>
|
|
|
|
{content}
|
|
|
|
</div>
|
2017-08-30 17:56:52 +02:00
|
|
|
</div>
|
2017-08-31 16:00:45 +02:00
|
|
|
<div className="col-lg-3">
|
2017-08-30 17:56:52 +02:00
|
|
|
{relations}
|
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-04-21 13:20:58 +02:00
|
|
|
Edit.propTypes = {
|
2017-05-11 11:20:01 +02:00
|
|
|
currentModelName: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.bool,
|
|
|
|
React.PropTypes.string,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-08-30 17:56:52 +02:00
|
|
|
// deleteRecord: React.PropTypes.func.isRequired,
|
|
|
|
// deleting: React.PropTypes.bool.isRequired,
|
2017-05-11 11:20:01 +02:00
|
|
|
editing: React.PropTypes.bool.isRequired,
|
|
|
|
editRecord: React.PropTypes.func.isRequired,
|
|
|
|
isCreating: React.PropTypes.bool.isRequired,
|
2017-06-19 20:17:10 +02:00
|
|
|
loading: React.PropTypes.bool.isRequired,
|
2017-05-11 10:54:44 +02:00
|
|
|
loadRecord: React.PropTypes.func.isRequired,
|
2017-08-30 17:56:52 +02:00
|
|
|
match: React.PropTypes.shape({
|
|
|
|
params: React.PropTypes.shape({
|
|
|
|
id: React.PropTypes.string,
|
|
|
|
slug: React.PropTypes.string,
|
|
|
|
}),
|
|
|
|
}).isRequired,
|
2017-08-31 16:00:45 +02:00
|
|
|
models: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.object,
|
|
|
|
React.PropTypes.bool,
|
|
|
|
]).isRequired,
|
2017-05-11 11:20:01 +02:00
|
|
|
record: React.PropTypes.oneOfType([
|
2017-04-21 17:19:41 +02:00
|
|
|
React.PropTypes.object,
|
|
|
|
React.PropTypes.bool,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-06-17 17:01:50 +02:00
|
|
|
schema: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.object,
|
|
|
|
React.PropTypes.bool,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-05-11 11:20:01 +02:00
|
|
|
setCurrentModelName: React.PropTypes.func.isRequired,
|
2017-06-19 20:17:10 +02:00
|
|
|
setInitialState: React.PropTypes.func.isRequired,
|
2017-05-11 10:54:44 +02:00
|
|
|
setIsCreating: React.PropTypes.func.isRequired,
|
|
|
|
setRecordAttribute: React.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-01-26 18:53:52 +01:00
|
|
|
});
|
2017-01-26 17:53:47 +01:00
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return {
|
2017-06-19 20:17:10 +02:00
|
|
|
setInitialState: () => dispatch(setInitialState()),
|
2017-05-11 10:54:44 +02:00
|
|
|
setCurrentModelName: currentModelName =>
|
|
|
|
dispatch(setCurrentModelName(currentModelName)),
|
2017-05-05 11:40:52 +02:00
|
|
|
setIsCreating: () => dispatch(setIsCreating()),
|
2017-05-11 10:54:44 +02:00
|
|
|
loadRecord: id => dispatch(loadRecord(id)),
|
|
|
|
setRecordAttribute: (key, value) =>
|
|
|
|
dispatch(setRecordAttribute(key, value)),
|
2017-04-21 17:19:41 +02:00
|
|
|
editRecord: () => dispatch(editRecord()),
|
2017-04-21 17:52:18 +02:00
|
|
|
deleteRecord: () => {
|
|
|
|
// TODO: improve confirmation UX.
|
2017-06-08 17:16:20 +01:00
|
|
|
if (window.confirm('Are you sure ?')) {
|
|
|
|
// eslint-disable-line no-alert
|
2017-04-21 17:52:18 +02:00
|
|
|
dispatch(deleteRecord());
|
|
|
|
}
|
|
|
|
},
|
2017-01-26 17:53:47 +01: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);
|