179 lines
4.9 KiB
JavaScript
Raw Normal View History

/*
*
2017-04-21 13:20:58 +02:00
* Edit
*
*/
import React from 'react';
import _ from 'lodash';
import { connect } from 'react-redux';
2017-01-26 18:53:52 +01:00
import { createStructuredSelector } from 'reselect';
2017-04-21 17:19:41 +02:00
2017-05-16 16:32:54 +02:00
import Container from 'components/Container';
import EditForm from 'components/EditForm';
import { makeSelectModels } from 'containers/App/selectors';
2017-01-26 18:53:52 +01:00
import {
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,
2017-04-21 17:52:18 +02:00
deleteRecord,
2017-01-26 18:53:52 +01:00
} from './actions';
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,
2017-01-26 18:53:52 +01:00
} from './selectors';
2017-05-11 10:54:44 +02:00
export class Edit extends React.Component {
2017-01-26 18:53:52 +01:00
componentWillMount() {
2017-04-21 17:19:41 +02:00
this.props.setCurrentModelName(this.props.routeParams.slug.toLowerCase());
2017-05-05 11:40:52 +02:00
// Detect that the current route is the `create` route or not
if (this.props.routeParams.id === 'create') {
this.props.setIsCreating();
} else {
this.props.loadRecord(this.props.routeParams.id);
}
2017-01-26 18:53:52 +01:00
}
render() {
2017-04-21 17:19:41 +02:00
// Detect current model structure from models list
const currentModel = this.props.models[this.props.currentModelName];
2017-01-26 18:53:52 +01:00
2017-04-21 17:19:41 +02:00
const PluginHeader = this.props.exposedComponents.PluginHeader;
2017-04-21 17:19:41 +02:00
let content = <p>Loading...</p>;
if (currentModel && currentModel.attributes) {
content = (
2017-04-21 17:19:41 +02:00
<EditForm
record={this.props.record}
currentModel={currentModel}
setRecordAttribute={this.props.setRecordAttribute}
editRecord={this.props.editRecord}
editing={this.props.editing}
/>
2017-04-11 11:34:59 +02:00
);
2017-01-26 18:53:52 +01:00
}
2017-05-05 11:40:52 +02:00
// Define plugin header actions
2017-05-11 10:54:44 +02:00
const pluginHeaderActions = [
{
label: 'Cancel',
class: 'btn-default',
},
{
label: this.props.editing ? 'Editing...' : 'Submit',
class: 'btn-primary',
onClick: this.props.editRecord,
disabled: this.props.editing,
},
];
2017-04-21 17:19:41 +02:00
2017-05-05 11:40:52 +02:00
// Add the `Delete` button only in edit mode
2017-05-11 10:54:44 +02:00
if (!this.props.isCreating) {
2017-05-05 11:40:52 +02:00
pluginHeaderActions.push({
2017-05-10 13:55:20 +02:00
label: 'Delete',
class: 'btn-danger',
onClick: this.props.deleteRecord,
disabled: this.props.deleting,
});
2017-05-05 11:40:52 +02:00
}
// Plugin header config
2017-05-11 10:54:44 +02:00
const pluginHeaderTitle =
_.upperFirst(this.props.routeParams.slug) || 'Content Manager';
const pluginHeaderDescription = this.props.isCreating
? 'New entry'
: `#${this.props.record.get('id')}`;
return (
2017-04-21 17:19:41 +02:00
<div className="col-md-12">
<div className="container-fluid">
2017-04-21 17:19:41 +02:00
<PluginHeader
title={{
id: 'plugin-content-manager-title',
2017-05-11 10:54:44 +02:00
defaultMessage: `${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-04-21 17:19:41 +02:00
/>
<Container>
2017-05-11 10:54:44 +02:00
<p />
2017-04-21 17:19:41 +02:00
<div className="row">
<div className="col-md-8">
{content}
</div>
</div>
</Container>
</div>
</div>
);
}
}
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,
]),
deleteRecord: React.PropTypes.func.isRequired,
deleting: React.PropTypes.bool.isRequired,
editing: React.PropTypes.bool.isRequired,
editRecord: React.PropTypes.func.isRequired,
exposedComponents: React.PropTypes.object.isRequired,
isCreating: React.PropTypes.bool.isRequired,
2017-05-11 10:54:44 +02:00
loadRecord: React.PropTypes.func.isRequired,
2017-05-11 11:20:01 +02:00
models: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.bool,
]),
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-05-11 10:54:44 +02:00
routeParams: React.PropTypes.object.isRequired,
2017-05-11 11:20:01 +02:00
setCurrentModelName: React.PropTypes.func.isRequired,
2017-05-11 10:54:44 +02:00
setIsCreating: React.PropTypes.func.isRequired,
setRecordAttribute: React.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(),
models: makeSelectModels(),
editing: makeSelectEditing(),
2017-04-21 17:52:18 +02:00
deleting: makeSelectDeleting(),
2017-05-05 11:40:52 +02:00
isCreating: makeSelectIsCreating(),
2017-01-26 18:53:52 +01:00
});
function mapDispatchToProps(dispatch) {
return {
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.
if (window.confirm('Are you sure ?')) { // eslint-disable-line no-alert
2017-04-21 17:52:18 +02:00
dispatch(deleteRecord());
}
},
dispatch,
};
}
2017-04-21 13:20:58 +02:00
export default connect(mapStateToProps, mapDispatchToProps)(Edit);