/* * * Edit * */ // Dependencies. import React from 'react'; import moment from 'moment'; import { connect } from 'react-redux'; import { bindActionCreators, compose } from 'redux'; import { createStructuredSelector } from 'reselect'; import PropTypes from 'prop-types'; import { get, isObject } from 'lodash'; import { router } from 'app'; // Components. import EditForm from 'components/EditForm'; import EditFormRelations from 'components/EditFormRelations'; import PluginHeader from 'components/PluginHeader'; // Selectors. import { makeSelectModels, makeSelectSchema } from 'containers/App/selectors'; // Utils. import injectReducer from 'utils/injectReducer'; import injectSaga from 'utils/injectSaga'; import templateObject from 'utils/templateObject'; // Styles. import styles from './styles.scss'; // Actions. import { setInitialState, setCurrentModelName, setIsCreating, loadRecord, setRecordAttribute, editRecord, toggleNull, cancelChanges, } from './actions'; // Selectors. import { makeSelectRecord, makeSelectLoading, makeSelectCurrentModelName, makeSelectEditing, makeSelectDeleting, makeSelectIsCreating, makeSelectIsRelationComponentNull, } from './selectors'; import reducer from './reducer'; import saga from './sagas'; export class Edit extends React.Component { constructor(props) { super(props); this.pluginHeaderActions = [ { label: 'content-manager.containers.Edit.cancel', handlei18n: true, buttonBackground: 'secondary', buttonSize: 'buttonMd', onClick: this.props.cancelChanges, type: 'button', }, { handlei18n: true, buttonBackground: 'primary', buttonSize: 'buttonLg', label: this.props.editing ? 'content-manager.containers.Edit.editing' : 'content-manager.containers.Edit.submit', onClick: this.props.editRecord, disabled: this.props.editing, type: 'submit', }, ]; this.pluginHeaderSubActions = [ { label: 'content-manager.containers.Edit.returnList', handlei18n: true, buttonBackground: 'back', onClick: () => router.goBack(), type: 'button', }, ]; } componentDidMount() { this.props.setInitialState(); this.props.setCurrentModelName(this.props.match.params.slug.toLowerCase()); // Detect that the current route is the `create` route or not if (this.props.match.params.id === 'create') { this.props.setIsCreating(); } else { this.props.loadRecord(this.props.match.params.id); } document.addEventListener('keydown', this.handleSubmitOnEnterPress); } componentWillUnmount() { document.removeEventListener('keydown', this.handleSubmitOnEnterPress); } handleChange = (e) => { if (isObject(e.target.value) && e.target.value._isAMomentObject === true) { e.target.value = moment(e.target.value, 'YYYY-MM-DD HH:mm:ss').format(); } this.props.setRecordAttribute(e.target.name, e.target.value); } handleSubmit = () => { this.props.editRecord(); } handleSubmitOnEnterPress = (e) => { if (e.keyCode === 13) { this.props.editRecord(); } } render() { if (this.props.loading || !this.props.schema || !this.props.currentModelName) { return
Loading...
; } // 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 (