import React, { useCallback, useState, useEffect } from 'react';
import {
SchemaSettings,
ActionDesigner,
useSchemaToolbar,
useDesignable,
useCompile,
DefaultValueProvider,
SchemaSettingsItemGroup,
SchemaSettingsModalItem,
SchemaSettingsActionModalItem,
SchemaSettingsSelectItem,
FlagProvider,
} from '@nocobase/client';
import { isValid, uid } from '@formily/shared';
import { useTranslation } from 'react-i18next';
import { useField, useFieldSchema, ISchema } from '@formily/react';
const MenuGroup = (props) => {
const fieldSchema = useFieldSchema();
const { t } = useTranslation();
const compile = useCompile();
const actionTitle = fieldSchema.title ? compile(fieldSchema.title) : '';
return (
${actionTitle}`}>{props.children}
);
};
function UpdateMode() {
const { dn } = useDesignable();
const { t } = useTranslation();
const fieldSchema = useFieldSchema();
return (
{
fieldSchema['x-action-settings']['updateMode'] = value;
dn.emit('patch', {
schema: {
'x-uid': fieldSchema['x-uid'],
'x-action-settings': fieldSchema['x-action-settings'],
},
});
dn.refresh();
}}
/>
);
}
function AfterSuccess() {
const { dn } = useDesignable();
const { t } = useTranslation();
const fieldSchema = useFieldSchema();
return (
{
fieldSchema['x-action-settings']['onSuccess'] = onSuccess;
dn.emit('patch', {
schema: {
['x-uid']: fieldSchema['x-uid'],
'x-action-settings': fieldSchema['x-action-settings'],
},
});
}}
/>
);
}
function AssignedFieldValues() {
const { dn } = useDesignable();
const { t } = useTranslation();
const fieldSchema = useFieldSchema();
const field = useField();
const [initialSchema, setInitialSchema] = useState();
useEffect(() => {
const schemaUid = uid();
const schema: ISchema = {
type: 'void',
'x-uid': schemaUid,
'x-component': 'Grid',
'x-initializer': 'CustomFormItemInitializers',
};
setInitialSchema(schema);
}, [field.address]);
const tips = {
'customize:update': t(
'After clicking the custom button, the following fields of the current record will be saved according to the following form.',
),
'customize:save': t(
'After clicking the custom button, the following fields of the current record will be saved according to the following form.',
),
};
const actionType = fieldSchema['x-action'] ?? '';
const onSubmit = useCallback(
(assignedValues) => {
fieldSchema['x-action-settings']['assignedValues'] = assignedValues;
dn.emit('patch', {
schema: {
['x-uid']: fieldSchema['x-uid'],
'x-action-settings': fieldSchema['x-action-settings'],
},
});
},
[dn, fieldSchema],
);
return (
false}>
);
}
const bulkUpdateActionSettings = new SchemaSettings({
name: 'ActionSettings:customize:bulkUpdate',
items: [
{
name: 'Customize',
Component: MenuGroup,
children: [
{
name: 'editButton',
Component: ActionDesigner.ButtonEditor,
useComponentProps() {
const { buttonEditorProps } = useSchemaToolbar();
return buttonEditorProps;
},
},
{
name: 'updateMode',
Component: UpdateMode,
useVisible() {
const fieldSchema = useFieldSchema();
const isUpdateModePopupAction = ['customize:bulkUpdate', 'customize:bulkEdit'].includes(
fieldSchema['x-action'],
);
return isUpdateModePopupAction;
},
},
{
name: 'assignFieldValues',
Component: AssignedFieldValues,
useVisible() {
const fieldSchema = useFieldSchema();
return isValid(fieldSchema?.['x-action-settings']?.assignedValues);
},
},
{
name: 'afterSuccess',
Component: AfterSuccess,
useVisible() {
const fieldSchema = useFieldSchema();
return isValid(fieldSchema?.['x-action-settings']?.onSuccess);
},
},
{
name: 'remove',
sort: 100,
Component: ActionDesigner.RemoveButton as any,
useComponentProps() {
const { removeButtonProps } = useSchemaToolbar();
return removeButtonProps;
},
},
],
},
],
});
export { bulkUpdateActionSettings };