Fix displayed field on remove

This commit is contained in:
soupette 2019-02-04 17:03:51 +01:00
parent 5ed07cb27a
commit da424a4ca7
4 changed files with 21 additions and 9 deletions

View File

@ -7,7 +7,7 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { isFunction, isObject } from 'lodash'; import { isEmpty, isFunction, isObject } from 'lodash';
import LoadingBar from 'components/LoadingBar'; import LoadingBar from 'components/LoadingBar';
@ -20,7 +20,7 @@ function PluginHeaderTitle({ description, title, titleId, withDescriptionAnim })
return ( return (
<div> <div>
<h1 className={styles.pluginHeaderTitleName} id={titleId}> <h1 className={styles.pluginHeaderTitleName} id={titleId}>
{contentTitle} {contentTitle}&nbsp;
</h1> </h1>
{withDescriptionAnim ? ( {withDescriptionAnim ? (
<LoadingBar /> <LoadingBar />
@ -32,8 +32,9 @@ function PluginHeaderTitle({ description, title, titleId, withDescriptionAnim })
} }
const formatData = data => { const formatData = data => {
if (isObject(data) && data.id) {
return <FormattedMessage id={data.id} defaultMessage={data.id} values={data.values} />; if (isObject(data)) {
return isEmpty(data.id) ? null : <FormattedMessage id={data.id} defaultMessage={data.id} values={data.values} />;
} }
if (isFunction(data)) { if (isFunction(data)) {

View File

@ -169,12 +169,11 @@ export class EditPage extends React.Component {
if (this.isCreating()) { if (this.isCreating()) {
return toString(this.props.editPage.pluginHeaderTitle); return toString(this.props.editPage.pluginHeaderTitle);
} }
const primaryKey = this.getModel().primaryKey;
const { match: { params: { id } } } = this.props;
const title = get(this.getSchema(), 'editDisplay.displayedField', primaryKey);
const valueToDisplay = get(this.props.editPage, ['initialRecord', title], id);
return isEmpty(valueToDisplay) ? id : truncate(valueToDisplay, { length: '24', separator: '.' }); const title = get(this.getSchema(), 'editDisplay.displayedField');
const valueToDisplay = get(this.props.editPage, ['initialRecord', title], null);
return isEmpty(valueToDisplay) ? null : truncate(valueToDisplay, { length: '24', separator: '.' });
}; };
/** /**

View File

@ -5,6 +5,7 @@ const {
getApisKeys, getApisKeys,
getApisUploadRelations, getApisUploadRelations,
getEditDisplayAvailableFieldsPath, getEditDisplayAvailableFieldsPath,
getEditDisplayDisplayedField,
getEditDisplayFieldsPath getEditDisplayFieldsPath
} = require('./utils/getters'); } = require('./utils/getters');
const splitted = str => str.split('.'); const splitted = str => str.split('.');
@ -302,6 +303,9 @@ module.exports = async cb => {
const defaultSortPath = apiPath.concat('defaultSort'); const defaultSortPath = apiPath.concat('defaultSort');
const currentAttr = attrPath.slice(-1); const currentAttr = attrPath.slice(-1);
const defaultSort = _.get(prevSchema.models, defaultSortPath); const defaultSort = _.get(prevSchema.models, defaultSortPath);
const displayedFieldPath = getEditDisplayDisplayedField(attrPath);
const displayedField = _.get(prevSchema.models, displayedFieldPath, null);
const primaryKey = _.get(prevSchema.models, [...apiPath, 'primaryKey'], null);
// If the user has deleted the default sort attribute in the content type builder // If the user has deleted the default sort attribute in the content type builder
// Replace it by new generated one from the current schema // Replace it by new generated one from the current schema
@ -309,6 +313,12 @@ module.exports = async cb => {
_.set(prevSchema.models, defaultSortPath, _.get(schema.models, defaultSortPath)); _.set(prevSchema.models, defaultSortPath, _.get(schema.models, defaultSortPath));
} }
// If the user has deleted the edit view displayed field (name in the header)
// Replace it by the model's primary key.
if (_.includes(currentAttr, displayedField)) {
_.set(prevSchema.models, displayedFieldPath, primaryKey);
}
// Update the displayed fields // Update the displayed fields
const updatedListDisplay = prevListDisplay.filter(obj => obj.name !== currentAttr.join()); const updatedListDisplay = prevListDisplay.filter(obj => obj.name !== currentAttr.join());

View File

@ -54,6 +54,7 @@ const getApisUploadRelations = (data, sameArray) => sameArray.map(apiPath => {
* @returns {Array} * @returns {Array}
*/ */
const getEditDisplayAvailableFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'availableFields', attrPath[attrPath.length - 1]]; const getEditDisplayAvailableFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'availableFields', attrPath[attrPath.length - 1]];
const getEditDisplayDisplayedField = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'displayedField'];
const getEditDisplayFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'fields']; const getEditDisplayFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'fields'];
@ -63,5 +64,6 @@ module.exports = {
getApisKeys, getApisKeys,
getApisUploadRelations, getApisUploadRelations,
getEditDisplayAvailableFieldsPath, getEditDisplayAvailableFieldsPath,
getEditDisplayDisplayedField,
getEditDisplayFieldsPath getEditDisplayFieldsPath
}; };