fix(plugin-workflow): fix form event parse variables (#3106)

This commit is contained in:
Junyi 2023-11-28 20:58:15 +08:00 committed by GitHub
parent d2d885b2a6
commit bd6fbcd319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 130 deletions

View File

@ -4,7 +4,7 @@ import { App, message } from 'antd';
import _, { cloneDeep } from 'lodash'; import _, { cloneDeep } from 'lodash';
import get from 'lodash/get'; import get from 'lodash/get';
import omit from 'lodash/omit'; import omit from 'lodash/omit';
import { ChangeEvent, useContext, useEffect } from 'react'; import { ChangeEvent, useCallback, useContext, useEffect } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useReactToPrint } from 'react-to-print'; import { useReactToPrint } from 'react-to-print';
@ -97,35 +97,30 @@ export function getFormValues({
return form.values; return form.values;
} }
export const useCreateActionProps = () => { export function useCollectValuesToSubmit() {
const form = useForm(); const form = useForm();
const { field, resource, __parent } = useBlockRequestContext();
const { setVisible, fieldSchema } = useActionContext();
const navigate = useNavigate();
const actionSchema = useFieldSchema();
const actionField = useField();
const { fields, getField, getTreeParentField, name } = useCollection();
const compile = useCompile();
const filterByTk = useFilterByTk(); const filterByTk = useFilterByTk();
const currentRecord = useRecord(); const { field, resource } = useBlockRequestContext();
const { modal } = App.useApp(); const { fields, getField, getTreeParentField, name } = useCollection();
const fieldNames = fields.map((field) => field.name);
const { fieldSchema } = useActionContext();
const { getActiveFieldsName } = useFormActiveFields() || {};
const variables = useVariables(); const variables = useVariables();
const localVariables = useLocalVariables({ currentForm: form }); const localVariables = useLocalVariables({ currentForm: form });
const { getActiveFieldsName } = useFormActiveFields() || {}; const actionSchema = useFieldSchema();
const { t } = useTranslation(); const currentRecord = useRecord();
const action = actionField.componentProps.saveMode || 'create';
const filterKeys = actionField.componentProps.filterKeys?.checked || []; return useCallback(async () => {
return { const { assignedValues: originalAssignedValues = {}, overwriteValues } = actionSchema?.['x-action-settings'] ?? {};
async onClick() { const values = getFormValues({
const fieldNames = fields.map((field) => field.name); filterByTk,
const { field,
assignedValues: originalAssignedValues = {}, form,
onSuccess, fieldNames,
overwriteValues, getField,
skipValidator, resource,
triggerWorkflows, actionFields: getActiveFieldsName?.('form') || [],
} = actionSchema?.['x-action-settings'] ?? {}; });
const addChild = fieldSchema?.['x-component-props']?.addChild;
const assignedValues = {}; const assignedValues = {};
const waitList = Object.keys(originalAssignedValues).map(async (key) => { const waitList = Object.keys(originalAssignedValues).map(async (key) => {
@ -148,34 +143,63 @@ export const useCreateActionProps = () => {
} }
}); });
await Promise.all(waitList); await Promise.all(waitList);
if (!skipValidator) {
await form.submit();
}
const values = getFormValues({
filterByTk,
field,
form,
fieldNames,
getField,
resource,
actionFields: getActiveFieldsName?.('form') || [],
});
// const values = omitBy(formValues, (value) => isEqual(JSON.stringify(value), '[{}]')); // const values = omitBy(formValues, (value) => isEqual(JSON.stringify(value), '[{}]'));
const addChild = fieldSchema?.['x-component-props']?.addChild;
if (addChild) { if (addChild) {
const treeParentField = getTreeParentField(); const treeParentField = getTreeParentField();
values[treeParentField?.name ?? 'parent'] = omit(currentRecord?.__parent, ['children']); values[treeParentField?.name ?? 'parent'] = omit(currentRecord?.__parent, ['children']);
values[treeParentField?.foreignKey ?? 'parentId'] = currentRecord?.__parent?.id; values[treeParentField?.foreignKey ?? 'parentId'] = currentRecord?.__parent?.id;
} }
return {
...values,
...overwriteValues,
...assignedValues,
};
}, [
actionSchema,
currentRecord?.__parent,
field,
fieldNames,
fieldSchema,
filterByTk,
form,
getActiveFieldsName,
getField,
getTreeParentField,
localVariables,
name,
resource,
variables,
]);
}
export const useCreateActionProps = () => {
const form = useForm();
const { field, resource, __parent } = useBlockRequestContext();
const { setVisible } = useActionContext();
const navigate = useNavigate();
const actionSchema = useFieldSchema();
const actionField = useField();
const compile = useCompile();
const { modal } = App.useApp();
const { t } = useTranslation();
const collectValues = useCollectValuesToSubmit();
const action = actionField.componentProps.saveMode || 'create';
const filterKeys = actionField.componentProps.filterKeys?.checked || [];
return {
async onClick() {
const { onSuccess, skipValidator, triggerWorkflows } = actionSchema?.['x-action-settings'] ?? {};
if (!skipValidator) {
await form.submit();
}
const values = await collectValues();
actionField.data = field.data || {}; actionField.data = field.data || {};
actionField.data.loading = true; actionField.data.loading = true;
try { try {
const data = await resource[action]({ const data = await resource[action]({
values: { values,
...values,
...overwriteValues,
...assignedValues,
},
filterKeys: filterKeys, filterKeys: filterKeys,
// TODO(refactor): should change to inject by plugin // TODO(refactor): should change to inject by plugin
triggerWorkflows: triggerWorkflows?.length triggerWorkflows: triggerWorkflows?.length

View File

@ -4,14 +4,10 @@ import {
useAPIClient, useAPIClient,
useActionContext, useActionContext,
useBlockRequestContext, useBlockRequestContext,
useCollection, useCollectValuesToSubmit,
useCollectionDataSource, useCollectionDataSource,
useCollectionManager, useCollectionManager,
useCompile, useCompile,
useCurrentUserContext,
useFilterByTk,
useFormActiveFields,
useRecord,
} from '@nocobase/client'; } from '@nocobase/client';
import { isURL, parse } from '@nocobase/utils/client'; import { isURL, parse } from '@nocobase/utils/client';
import { App, message } from 'antd'; import { App, message } from 'antd';
@ -104,92 +100,32 @@ export default {
useActionTriggerable: true, useActionTriggerable: true,
}; };
function getFormValues({
filterByTk,
form,
getField,
actionFields,
}: {
filterByTk;
form;
getField;
actionFields: any[];
}) {
if (filterByTk) {
if (actionFields) {
const keys = Object.keys(form.values).filter((key) => {
const f = getField(key);
return !actionFields.includes(key) && ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(f?.type);
});
return omit({ ...form.values }, keys);
}
}
return form.values;
}
export function useTriggerWorkflowsActionProps() { export function useTriggerWorkflowsActionProps() {
const api = useAPIClient(); const api = useAPIClient();
const form = useForm(); const form = useForm();
const { field, resource, __parent } = useBlockRequestContext(); const { field, __parent } = useBlockRequestContext();
const { setVisible, fieldSchema } = useActionContext(); const { setVisible } = useActionContext();
const navigate = useNavigate(); const navigate = useNavigate();
const actionSchema = useFieldSchema(); const actionSchema = useFieldSchema();
const actionField = useField(); const actionField = useField();
const { fields, getField, getTreeParentField } = useCollection();
const compile = useCompile(); const compile = useCompile();
const filterByTk = useFilterByTk();
const currentRecord = useRecord();
const currentUserContext = useCurrentUserContext();
const { modal } = App.useApp(); const { modal } = App.useApp();
const { getActiveFieldsName } = useFormActiveFields() || {}; const collectValues = useCollectValuesToSubmit();
const currentUser = currentUserContext?.data?.data;
const filterKeys = actionField.componentProps.filterKeys || []; const filterKeys = actionField.componentProps.filterKeys || [];
return { return {
async onClick() { async onClick() {
const { const { onSuccess, skipValidator, triggerWorkflows } = actionSchema?.['x-action-settings'] ?? {};
assignedValues: originalAssignedValues = {},
onSuccess,
overwriteValues,
skipValidator,
triggerWorkflows,
} = actionSchema?.['x-action-settings'] ?? {};
const addChild = fieldSchema?.['x-component-props']?.addChild;
const assignedValues = parse(originalAssignedValues)({
// @deprecated
currentTime: new Date(),
// @deprecated
currentRecord,
// @deprecated
currentUser,
$user: currentUser,
$nRecord: currentRecord,
$nForm: form.values,
$nDate: {
now: new Date(),
},
});
if (!skipValidator) { if (!skipValidator) {
await form.submit(); await form.submit();
} }
const values = getFormValues({ filterByTk, form, getField, actionFields: getActiveFieldsName?.('form') || [] }); const values = await collectValues();
// const values = omitBy(formValues, (value) => isEqual(JSON.stringify(value), '[{}]'));
if (addChild) {
const treeParentField = getTreeParentField();
values[treeParentField?.name ?? 'parent'] = currentRecord;
values[treeParentField?.foreignKey ?? 'parentId'] = currentRecord.id;
}
actionField.data = field.data || {}; actionField.data = field.data || {};
actionField.data.loading = true; actionField.data.loading = true;
try { try {
const data = await api.resource('workflows').trigger({ const data = await api.resource('workflows').trigger({
values: { values,
...values,
...overwriteValues,
...assignedValues,
},
filterKeys: filterKeys, filterKeys: filterKeys,
// TODO(refactor): should change to inject by plugin // TODO(refactor): should change to inject by plugin
triggerWorkflows: triggerWorkflows?.length triggerWorkflows: triggerWorkflows?.length