fix: the sub-table records can not be modified and deleted

This commit is contained in:
chenos 2022-04-13 22:50:02 +08:00
parent 5308210991
commit 1326e024ab
3 changed files with 49 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -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?.();
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?.();
},
};

View File

@ -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');
}