import React from 'react' import Modal from 'react-modal' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import isEqual from 'lodash/isEqual' import cloneDeep from 'lodash/cloneDeep' import ConfirmDelete from './confirm-delete' import Template from './util/template' import Admin from './../admin' import { ConfirmDeleteModalStyles } from './helpers/modal-styles' import { fetchEntity, createEntity, updateEntity, deleteEntity } from '../actions/index' import { withExitPrompt } from './util/with-exit-prompt' interface TemplateDetailsProps { deleteEntity: any; errors: string[]; fetchEntity: any; fields: string[]; template: any; template_errors: string[]; templateSaved: any; templates: any; updateEntity: any; } interface TemplateDetailsState { changesToSave: boolean; deleteModalOpen: boolean; errors: { fields: any; template_errors: string[]; } fetching: boolean; fieldRefs: any; fields: any; fieldsSaved: any; template: any; templateSaved: any; templates: any; saveButtonRef?: any; } class TemplateDetails extends React.Component { constructor(props) { super(props); this.state = { fetching: true, changesToSave: false, deleteModalOpen: false, template: {}, templateSaved: {}, fieldRefs: {}, fields: [], fieldsSaved: [], templates: [], errors: { template_errors: [], fields: [] } }; } componentDidMount() { let pathDirectories = window.location.pathname.split('/'); this.props.fetchEntity({ id: pathDirectories[pathDirectories.length - 1], directory: "templates", entityName: "template" }).then(() => { let errors = { fields: Array(this.props.fields.length).fill([]), template_errors: [] }; this.setState({ fetching: false, template: this.props.template, templateSaved: cloneDeep(this.props.template), errors, fields: this.props.fields, fieldsSaved: cloneDeep(this.props.fields), templates: this.props.templates }, () => { Admin.setUpNiceSelect({ selector: 'select', func: Admin.changeField.bind(this, this.changeFieldArgs()) }); }); }); } clickSave() { let fields = Template.getUpdatedFieldsArray.call(this); this.setState({ fetching: true, fields: fields }, () => { let pathDirectories = window.location.pathname.split('/'); this.props.updateEntity({ id: pathDirectories[pathDirectories.length - 1], directory: "templates", entityName: "template", entity: this.state.template, additionalData: { fields: Template.convertFieldsToSnakeCase(fields) } }).then(() => { this.setState({ fetching: false, template: this.props.template, templateSaved: cloneDeep(this.props.template), fields: this.props.fields, fieldsSaved: cloneDeep(this.props.fields), changesToSave: false }, () => { this.state.saveButtonRef.resetChangesToSave(); }); }, () => { this.setState({ fetching: false, errors: { template_errors: this.props.template_errors, fields: this.props.fields } }); }); }); } changeFieldArgs() { return { thing: 'template', errorsArray: this.state.errors['template_errors'], changesFunction: this.checkForChanges.bind(this) } } checkForChanges() { return !isEqual(this.state.template, this.state.templateSaved); } render() { return(

Object Template Details

{ Admin.renderSpinner(this.state.fetching) } { Admin.renderGrayedOut(this.state.fetching) }
{ Admin.renderImageField.bind(this)({ columnWidth: 3, entity: "template", property: "imageId", imageUrlProperty: "imageUrl", columnHeader: "Image", createImageRecord: true }) } { Admin.renderField.bind(this)({ columnWidth: 3, entity: "template", property: "name", errorsArray: this.state.errors.template_errors }) } { Admin.renderField.bind(this)({ columnWidth: 4, entity: "template", property: "plural", errorsArray: this.state.errors.template_errors }) } { Admin.renderBooleanDropDown.bind(this)({ columnWidth: 2, entity: "template", property: "editable" }) }
{ Template.renderFieldsZone.call(this, this.state.templates) } { Template.renderButtons.call(this) }
); } componentDidUpdate() { $(".match-height-layout").matchHeight(); Admin.resetNiceSelect({ selector: ".editable-dropdown-column select", func: Admin.changeField.bind(this, this.changeFieldArgs()) }); } } const mapStateToProps = (reducers, props) => { return reducers.standardReducer; }; function mapDispatchToProps(dispatch) { return bindActionCreators({ fetchEntity, createEntity, updateEntity, deleteEntity }, dispatch); } export default connect(mapStateToProps, mapDispatchToProps)(withExitPrompt(TemplateDetails));