133 lines
3.4 KiB
JavaScript
Raw Normal View History

/*
*
2017-04-21 13:20:58 +02:00
* Edit
*
*/
import React from 'react';
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
import Container from 'components/Container';
2017-04-21 17:19:41 +02:00
import EditForm from 'components/EditForm';
2017-01-26 18:53:52 +01:00
import {
2017-04-21 17:19:41 +02:00
setCurrentModelName,
loadRecord,
2017-04-21 17:19:41 +02:00
setRecordAttribute,
editRecord,
2017-01-26 18:53:52 +01:00
} from './actions';
import {
makeSelectRecord,
makeSelectLoading,
2017-04-21 17:19:41 +02:00
makeSelectCurrentModelName,
makeSelectEditing,
2017-01-26 18:53:52 +01:00
} from './selectors';
2017-04-21 17:19:41 +02:00
import {
makeSelectModels,
} from 'containers/App/selectors';
2017-01-26 18:53:52 +01:00
2017-04-21 17:19:41 +02:00
export class Edit extends React.Component { // eslint-disable-line react/prefer-stateless-function
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());
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-04-21 17:19:41 +02:00
const headersActions = [{
label: 'Cancel',
class: 'btn-default',
}, {
label: this.props.editing ? 'Editing...' : 'Submit',
class: 'btn-primary',
onClick: this.props.editRecord,
disabled: this.props.editing,
}, {
label: 'Delete',
class: 'btn-danger',
}];
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',
defaultMessage: `Content Manager > ${this.props.routeParams.slug}`
}}
description={{
id: 'plugin-content-manager-description',
defaultMessage: `Manage your ${this.props.routeParams.slug}`
}}
actions={headersActions}
/>
<Container>
<p></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-04-21 17:19:41 +02:00
setCurrentModelName: React.PropTypes.func,
loadRecord: React.PropTypes.func,
loading: React.PropTypes.bool,
record: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.bool,
]),
2017-04-21 17:19:41 +02:00
models: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.bool,
]),
editRecord: React.PropTypes.func,
editing: React.PropTypes.bool,
};
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-01-26 18:53:52 +01:00
});
function mapDispatchToProps(dispatch) {
return {
2017-04-21 17:19:41 +02:00
setCurrentModelName: (currentModelName) => dispatch(setCurrentModelName(currentModelName)),
loadRecord: (id) => dispatch(loadRecord(id)),
2017-04-21 17:19:41 +02:00
setRecordAttribute: (key, value) => dispatch(setRecordAttribute(key, value)),
editRecord: () => dispatch(editRecord()),
dispatch,
};
}
2017-04-21 13:20:58 +02:00
export default connect(mapStateToProps, mapDispatchToProps)(Edit);