2018-02-21 13:59:08 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* EditPage
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
2018-02-21 15:09:33 +01:00
|
|
|
import moment from 'moment';
|
2018-02-21 13:59:08 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
|
|
|
|
import { createStructuredSelector } from 'reselect';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2018-02-21 17:14:35 +01:00
|
|
|
import { get, isObject, toNumber, toString, size } from 'lodash';
|
2018-02-21 13:59:08 +01:00
|
|
|
import cn from 'classnames';
|
|
|
|
|
|
|
|
|
|
// You can find these components in either
|
|
|
|
|
// ./node_modules/strapi-helper-plugin/lib/src
|
|
|
|
|
// or strapi/packages/strapi-helper-plugin/lib/src
|
|
|
|
|
import BackHeader from 'components/BackHeader';
|
|
|
|
|
import PluginHeader from 'components/PluginHeader';
|
|
|
|
|
|
2018-02-21 15:33:58 +01:00
|
|
|
// Plugin's components
|
|
|
|
|
import Edit from 'components/Edit';
|
|
|
|
|
import EditRelations from 'components/EditRelations';
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
// App selectors
|
|
|
|
|
import { makeSelectModels, makeSelectSchema } from 'containers/App/selectors';
|
|
|
|
|
|
|
|
|
|
import injectReducer from 'utils/injectReducer';
|
|
|
|
|
import injectSaga from 'utils/injectSaga';
|
|
|
|
|
import getQueryParameters from 'utils/getQueryParameters';
|
2018-02-21 15:09:33 +01:00
|
|
|
import { bindLayout } from 'utils/bindLayout';
|
|
|
|
|
|
|
|
|
|
// Layout
|
|
|
|
|
import layout from '../../../../config/layout';
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
import {
|
2018-02-21 15:09:33 +01:00
|
|
|
changeData,
|
|
|
|
|
getData,
|
2018-02-21 13:59:08 +01:00
|
|
|
initModelProps,
|
2018-02-21 15:33:58 +01:00
|
|
|
resetProps,
|
2018-02-21 13:59:08 +01:00
|
|
|
} from './actions';
|
|
|
|
|
|
|
|
|
|
import reducer from './reducer';
|
|
|
|
|
import saga from './saga';
|
|
|
|
|
import makeSelectEditPage from './selectors';
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
|
|
|
|
export class EditPage extends React.Component {
|
|
|
|
|
componentDidMount() {
|
2018-02-21 15:09:33 +01:00
|
|
|
this.props.initModelProps(this.getModelName(), this.isCreating(), this.getSource(), this.getModelAttributes());
|
|
|
|
|
this.layout = bindLayout.call(
|
|
|
|
|
this,
|
|
|
|
|
get(this.context.plugins.toJS(), `${this.getSource()}.layout`, layout),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!this.isCreating()) {
|
|
|
|
|
const mainField = get(this.getModel(), 'info.mainField') || this.getModel().primaryKey;
|
|
|
|
|
this.props.getData(this.props.match.params.id, this.getSource(), mainField);
|
|
|
|
|
}
|
2018-02-21 13:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-21 15:33:58 +01:00
|
|
|
componentWillUnmount() {
|
|
|
|
|
this.props.resetProps();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
/**
|
|
|
|
|
* Retrive the model
|
|
|
|
|
* @type {Object}
|
|
|
|
|
*/
|
|
|
|
|
getModel = () => get(this.props.models, ['models', this.getModelName()]) || get(this.props.models, ['plugins', this.getSource(), 'models', this.getModelName()]);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the model's attributes
|
|
|
|
|
* @return {Object}
|
|
|
|
|
*/
|
|
|
|
|
getModelAttributes = () => this.getModel().attributes;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the model's name
|
|
|
|
|
* @return {String} model's name
|
|
|
|
|
*/
|
|
|
|
|
getModelName = () => this.props.match.params.slug.toLowerCase();
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
/**
|
|
|
|
|
* Retrieve model's schema
|
|
|
|
|
* @return {Object}
|
|
|
|
|
*/
|
|
|
|
|
getSchema = () => this.getSource() !== 'content-manager' ?
|
|
|
|
|
get(this.props.schema, ['plugins', this.getSource(), this.getModelName()])
|
|
|
|
|
: get(this.props.schema, [this.getModelName()]);
|
|
|
|
|
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
/**
|
|
|
|
|
* Retrieve the model's source
|
|
|
|
|
* @return {String}
|
|
|
|
|
*/
|
|
|
|
|
getSource = () => getQueryParameters(this.props.location.search, 'source');
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
handleChange = (e) => {
|
|
|
|
|
let value = e.target.value;
|
|
|
|
|
|
|
|
|
|
// Check if date
|
|
|
|
|
if (isObject(e.target.value) && e.target.value._isAMomentObject === true) {
|
|
|
|
|
value = moment(e.target.value, 'YYYY-MM-DD HH:mm:ss').format();
|
|
|
|
|
} else if (['float', 'integer', 'biginteger', 'decimal'].indexOf(this.getSchema().fields[e.target.name].type) !== -1) {
|
|
|
|
|
value = toNumber(e.target.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const target = {
|
2018-02-21 17:14:35 +01:00
|
|
|
name: `record.${e.target.name}`,
|
2018-02-21 15:09:33 +01:00
|
|
|
value,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.props.changeData({ target });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layout = bindLayout.call(this, layout);
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
componentDidCatch(error, info) {
|
|
|
|
|
console.log('err', error);
|
|
|
|
|
console.log('info', info);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
isCreating = () => this.props.match.params.id === 'create';
|
2018-02-21 13:59:08 +01:00
|
|
|
|
2018-02-21 15:33:58 +01:00
|
|
|
isRelationComponentNull = () => size(get(this.getSchema(), 'relations')) === 0;
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
pluginHeaderActions = [
|
|
|
|
|
{
|
|
|
|
|
label: 'content-manager.containers.Edit.reset',
|
|
|
|
|
kind: 'secondary',
|
|
|
|
|
onClick: () => {},
|
|
|
|
|
type: 'button',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
kind: 'primary',
|
2018-02-21 15:09:33 +01:00
|
|
|
label: !this.isCreating() ? 'content-manager.containers.Edit.editing' : 'content-manager.containers.Edit.submit',
|
2018-02-21 13:59:08 +01:00
|
|
|
onClick: () => {},
|
|
|
|
|
type: 'submit',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { editPage } = this.props;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2018-02-21 15:33:58 +01:00
|
|
|
<form onSubmit={(e) => { e.preventDefault(); }}>
|
|
|
|
|
<BackHeader onClick={() => this.props.history.goBack()} />
|
|
|
|
|
<div className={cn('container-fluid', styles.containerFluid)}>
|
|
|
|
|
<PluginHeader
|
|
|
|
|
actions={this.pluginHeaderActions}
|
|
|
|
|
title={{ id: toString(editPage.pluginHeaderTitle) }}
|
|
|
|
|
/>
|
|
|
|
|
<div className="row">
|
|
|
|
|
<div className={this.isRelationComponentNull() ? 'col-lg-12' : 'col-lg-9'}>
|
|
|
|
|
<div className={styles.main_wrapper}>
|
2018-02-21 17:14:35 +01:00
|
|
|
<Edit
|
|
|
|
|
attributes={this.getModelAttributes()}
|
|
|
|
|
didCheckErrors={editPage.didCheckErrors}
|
|
|
|
|
layout={this.layout}
|
|
|
|
|
modelName={this.getModelName()}
|
|
|
|
|
onChange={this.handleChange}
|
|
|
|
|
record={editPage.record}
|
|
|
|
|
schema={this.getSchema()}
|
|
|
|
|
/>
|
2018-02-21 15:33:58 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={cn('col-lg-3', this.isRelationComponentNull() ? 'hidden-xl-down' : '')}>
|
|
|
|
|
<div className={styles.sub_wrapper}>
|
2018-02-21 17:14:35 +01:00
|
|
|
{!this.isRelationComponentNull() && <EditRelations />}
|
2018-02-21 15:33:58 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
2018-02-21 13:59:08 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 15:09:33 +01:00
|
|
|
EditPage.contextTypes = {
|
|
|
|
|
plugins: PropTypes.object,
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-21 13:59:08 +01:00
|
|
|
EditPage.defaultProps = {
|
|
|
|
|
models: {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EditPage.propTypes = {
|
2018-02-21 15:09:33 +01:00
|
|
|
changeData: PropTypes.func.isRequired,
|
2018-02-21 13:59:08 +01:00
|
|
|
editPage: PropTypes.object.isRequired,
|
2018-02-21 15:09:33 +01:00
|
|
|
getData: PropTypes.func.isRequired,
|
2018-02-21 13:59:08 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
|
|
|
|
initModelProps: PropTypes.func.isRequired,
|
|
|
|
|
location: PropTypes.object.isRequired,
|
|
|
|
|
match: PropTypes.object.isRequired,
|
|
|
|
|
models: PropTypes.object,
|
2018-02-21 15:33:58 +01:00
|
|
|
resetProps: PropTypes.func.isRequired,
|
2018-02-21 15:09:33 +01:00
|
|
|
schema: PropTypes.object.isRequired,
|
2018-02-21 13:59:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
|
return bindActionCreators(
|
|
|
|
|
{
|
2018-02-21 15:09:33 +01:00
|
|
|
changeData,
|
|
|
|
|
getData,
|
2018-02-21 13:59:08 +01:00
|
|
|
initModelProps,
|
2018-02-21 15:33:58 +01:00
|
|
|
resetProps,
|
2018-02-21 13:59:08 +01:00
|
|
|
},
|
|
|
|
|
dispatch,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = createStructuredSelector({
|
|
|
|
|
editPage: makeSelectEditPage(),
|
|
|
|
|
models: makeSelectModels(),
|
|
|
|
|
schema: makeSelectSchema(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const withConnect = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
|
|
|
|
|
const withReducer = injectReducer({ key: 'editPage', reducer });
|
|
|
|
|
const withSaga = injectSaga({ key: 'editPage', saga });
|
|
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
|
withReducer,
|
|
|
|
|
withSaga,
|
|
|
|
|
withConnect,
|
|
|
|
|
)(EditPage);
|