From 1326e024abfcc08761fef412eb2107ec7e165b1d Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 13 Apr 2022 22:50:02 +0800 Subject: [PATCH] fix: the sub-table records can not be modified and deleted --- .../src/block-provider/TableFieldProvider.tsx | 7 +++- .../client/src/block-provider/hooks/index.ts | 7 +++- .../plugin-collection-manager/src/plugin.ts | 41 ++++++++++++++++++- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/packages/client/src/block-provider/TableFieldProvider.tsx b/packages/client/src/block-provider/TableFieldProvider.tsx index 680585a75..8b6491bc1 100644 --- a/packages/client/src/block-provider/TableFieldProvider.tsx +++ b/packages/client/src/block-provider/TableFieldProvider.tsx @@ -44,8 +44,8 @@ export class TableFieldResource { async list(options) { this.field.data = this.field.data || {}; - if (this.field?.data?.dataSource?.length) { - console.log('list', this.field.data.dataSource); + if (this.field.data.changed) { + console.log('list.dataSource', this.field.data.dataSource); return { data: { data: this.field.data.dataSource, @@ -85,12 +85,14 @@ export class TableFieldResource { console.log('create', options); const { values } = options; this.field.data.dataSource.push(values); + this.field.data.changed = true; } async update(options) { console.log('update', options); const { filterByTk, values } = options; this.field.data.dataSource[filterByTk] = values; + this.field.data.changed = true; } async destroy(options) { @@ -102,6 +104,7 @@ export class TableFieldResource { this.field.data.dataSource = this.field.data.dataSource.filter((item, index) => { return !filterByTk.includes(index); }); + this.field.data.changed = true; } } diff --git a/packages/client/src/block-provider/hooks/index.ts b/packages/client/src/block-provider/hooks/index.ts index 733af14dd..1bb312cd7 100644 --- a/packages/client/src/block-provider/hooks/index.ts +++ b/packages/client/src/block-provider/hooks/index.ts @@ -5,6 +5,7 @@ import { useHistory } from 'react-router-dom'; import { useActionContext } from '../../schema-component'; import { useBlockRequestContext, useFilterByTk } from '../BlockProvider'; import { useFormBlockContext } from '../FormBlockProvider'; +import { TableFieldResource } from '../TableFieldProvider'; export const usePickActionProps = () => { const form = useForm(); @@ -75,13 +76,14 @@ export const useUpdateActionProps = () => { return { async onClick() { await form.submit(); - await resource.update({ filterByTk, values: form.values, }); __parent?.service?.refresh?.(); - __parent?.__parent?.service?.refresh?.(); + if (!(resource instanceof TableFieldResource)) { + __parent?.__parent?.service?.refresh?.(); + } setVisible?.(false); }, }; @@ -111,6 +113,7 @@ export const useBulkDestroyActionProps = () => { await resource.destroy({ filterByTk: field.data?.selectedRowKeys, }); + field.data.selectedRowKeys = []; service?.refresh?.(); }, }; diff --git a/packages/plugin-collection-manager/src/plugin.ts b/packages/plugin-collection-manager/src/plugin.ts index 2b98f38f9..c4c1ed7e0 100644 --- a/packages/plugin-collection-manager/src/plugin.ts +++ b/packages/plugin-collection-manager/src/plugin.ts @@ -1,14 +1,15 @@ +import { Collection } from '@nocobase/database'; import { Plugin } from '@nocobase/server'; +import lodash from 'lodash'; import path from 'path'; import { CollectionRepository } from '.'; import { afterCreateForReverseField, beforeCreateForChildrenCollection, beforeCreateForReverseField, - beforeInitOptions, + beforeInitOptions } from './hooks'; import { CollectionModel, FieldModel } from './models'; -import lodash from 'lodash'; export class CollectionManagerPlugin extends Plugin { async beforeLoad() { @@ -78,6 +79,42 @@ export class CollectionManagerPlugin extends Plugin { await next(); }); + this.app.resourcer.use(async (ctx, next) => { + const { resourceName, actionName } = ctx.action; + if (actionName === 'update') { + const { updateAssociationValues = [] } = ctx.action.params; + const [collectionName, associationName] = resourceName.split('.'); + if (!associationName) { + const collection: Collection = ctx.db.getCollection(collectionName); + if (collection) { + for (const [, field] of collection.fields) { + if (field.options.interface === 'subTable') { + updateAssociationValues.push(field.name); + } + } + } + } else { + const association = ctx.db.getCollection(collectionName)?.getField?.(associationName); + if (association?.target) { + const collection: Collection = ctx.db.getCollection(association?.target); + if (collection) { + for (const [, field] of collection.fields) { + if (field.options.interface === 'subTable') { + updateAssociationValues.push(field.name); + } + } + } + } + } + if (updateAssociationValues.length) { + ctx.action.mergeParams({ + updateAssociationValues, + }); + } + } + await next(); + }); + this.app.acl.skip('collections', 'list', 'logged-in'); }