fix(Data-template): fix bug when deleting fields (#1907)

* chore: add translation

* fix(Data-template): fix bug when deleting fields

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
被雨水过滤的空气-Rairn 2023-05-22 17:17:33 +08:00 committed by GitHub
parent d35f67d2e1
commit 110b00bc01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 64 additions and 16 deletions

View File

@ -708,4 +708,6 @@ export default {
"Render Failed": "Render Failed",
"Feedback": "Feedback",
"Try again": "Try again",
"Data template": "Data template",
"Template fields have been removed and need to be reconfigured": "Template fields have been removed and need to be reconfigured",
};

View File

@ -686,5 +686,7 @@ export default {
"UpdatedAt": "Registro del último usuario actualizado de una fila",
"Column width": "Ancho de columna",
"Sortable": "Clasificable",
"Enable link": "Activar enlace"
"Enable link": "Activar enlace",
"Data template": "Plantilla de datos",
"Template fields have been removed and need to be reconfigured": "Los campos de la plantilla se han eliminado y deben reconfigurarse",
};

View File

@ -617,4 +617,6 @@ export default {
'Add template': 'テンプレートを追加',
'Display data template selector': 'データテンプレートセレクターを表示',
'Form data templates': 'フォームデータテンプレート',
"Data template": "データテンプレート",
"Template fields have been removed and need to be reconfigured": "テンプレートフィールドが削除されました。再設定する必要があります",
}

View File

@ -667,4 +667,6 @@ export default {
'Add template': 'Adicionar modelo',
'Display data template selector': 'Exibir seletor de modelo de dados',
'Form data templates': 'Modelos de dados do formulário',
"Data template": "Modelo de dados",
"Template fields have been removed and need to be reconfigured": "Os campos do modelo foram removidos e precisam ser reconfigurados",
};

View File

@ -521,4 +521,6 @@ export default {
'Add template': "Добавить шаблон",
'Display data template selector': "Отображать селектор шаблона данных",
'Form data templates': "Шаблоны данных формы",
"Data template": "Шаблон данных",
"Template fields have been removed and need to be reconfigured": "Поля шаблона были удалены и требуют повторной настройки",
}

View File

@ -520,4 +520,6 @@ export default {
'Add template': 'Şablon ekle',
'Display data template selector': 'Veri şablonu seçicisini görüntüle',
'Form data templates': 'Form veri şablonları',
"Data template": "Veri şablonu",
"Template fields have been removed and need to be reconfigured": "Şablon alanları kaldırıldı ve yeniden yapılandırılması gerekiyor",
}

View File

@ -777,6 +777,7 @@ export default {
'Add template': '添加模板',
'Display data template selector': '显示数据模板选择框',
'Form data templates': '表单数据模板',
'Data template': '数据模板',
'Reload Application': '重载应用',
'The application is reloading, please do not close the page.': '应用正在重新加载,请勿关闭页面。',
@ -785,5 +786,6 @@ export default {
"Allows to clear cache, reboot application": "允许清除缓存,重启应用",
'The will interrupt service, it may take a few seconds to restart. Are you sure to continue?': '重启将会中断当前服务,这个过程可能需要一点时间,确定要继续吗?',
'Reboot': '重启',
"Template fields have been removed and need to be reconfigured": "模板字段已被删除,需要重新配置",
'Clear cache': '清除缓存',
}

View File

@ -1,5 +1,5 @@
import { useFieldSchema } from '@formily/react';
import { forEach } from '@nocobase/utils/client';
import { forEach, showToast } from '@nocobase/utils/client';
import { Select } from 'antd';
import _ from 'lodash';
import React, { useCallback, useEffect } from 'react';
@ -25,12 +25,28 @@ const useDataTemplates = () => {
const fieldSchema = useFieldSchema();
const { t } = useTranslation();
const { items = [], display = true } = findDataTemplates(fieldSchema);
const { getCollectionJoinField } = useCollectionManager();
// 过滤掉已经被删除的字段
items.forEach((item) => {
item.fields = item.fields
.map((field) => {
const joinField = getCollectionJoinField(`${item.collection}.${field}`);
if (joinField) {
return field;
}
return '';
})
.filter(Boolean);
});
const templates: any = [
{
key: 'none',
title: t('None'),
},
].concat(items.map<any>((t, i) => ({ key: i, ...t })));
const defaultTemplate = items.find((item) => item.default);
return {
templates,
@ -42,14 +58,13 @@ const useDataTemplates = () => {
export const Templates = ({ style = {}, form }) => {
const { templates, display, enabled, defaultTemplate } = useDataTemplates();
const { getCollectionField } = useCollectionManager();
const [value, setValue] = React.useState(defaultTemplate?.key || 'none');
const api = useAPIClient();
const { t } = useTranslation();
useEffect(() => {
if (defaultTemplate) {
fetchTemplateData(api, defaultTemplate)
fetchTemplateData(api, defaultTemplate, t)
.then((data) => {
if (form) {
forEach(data, (value, key) => {
@ -58,6 +73,7 @@ export const Templates = ({ style = {}, form }) => {
}
});
}
return data;
})
.catch((err) => {
console.error(err);
@ -68,7 +84,8 @@ export const Templates = ({ style = {}, form }) => {
const handleChange = useCallback(async (value, option) => {
setValue(value);
if (option.key !== 'none') {
fetchTemplateData(api, option).then((data) => {
fetchTemplateData(api, option, t)
.then((data) => {
if (form) {
forEach(data, (value, key) => {
if (value) {
@ -76,6 +93,10 @@ export const Templates = ({ style = {}, form }) => {
}
});
}
return data;
})
.catch((err) => {
console.error(err);
});
} else {
form?.reset();
@ -110,7 +131,10 @@ function findDataTemplates(fieldSchema): ITemplate {
return {} as ITemplate;
}
async function fetchTemplateData(api, template: { collection: string; dataId: number; fields: string[] }) {
async function fetchTemplateData(api, template: { collection: string; dataId: number; fields: string[] }, t) {
if (template.fields.length === 0) {
return showToast(t('Template fields have been removed and need to be reconfigured'));
}
return api
.resource(template.collection)
.get({

View File

@ -3,10 +3,12 @@ export * from './common';
export * from './date';
export * from './forEach';
export * from './getValuesByPath';
export * from './json-templates';
export * from './merge';
export * from './notification';
export * from './number';
export * from './parse-filter';
export * from './registry';
// export * from './toposort';
export * from './uid';
export * from './json-templates';

View File

@ -0,0 +1,8 @@
import { notification } from 'antd';
export const showToast = (message, type = 'info', duration = 5000) => {
notification[type]({
message,
duration,
});
};