fix: action loading, refresh context, form submit and validate (#523)

This commit is contained in:
SemmyWong 2022-06-22 13:41:45 +08:00 committed by GitHub
parent 88dc2db0f2
commit a8ce758cab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 27 deletions

View File

@ -223,17 +223,20 @@ export const useCustomizeRequestActionProps = () => {
const compile = useCompile(); const compile = useCompile();
const form = useForm(); const form = useForm();
const { fields, getField } = useCollection(); const { fields, getField } = useCollection();
const { field, resource } = useBlockRequestContext(); const { field, resource, __parent, service } = useBlockRequestContext();
const currentRecord = useRecord(); const currentRecord = useRecord();
const currentUserContext = useCurrentUserContext(); const currentUserContext = useCurrentUserContext();
const currentUser = currentUserContext?.data?.data; const currentUser = currentUserContext?.data?.data;
const actionField = useField();
return { return {
async onClick() { async onClick() {
const { skipValidator, onSuccess, requestSettings } = actionSchema?.['x-action-settings'] ?? {}; const { skipValidator, onSuccess, requestSettings } = actionSchema?.['x-action-settings'] ?? {};
const xAction = actionSchema?.['x-action'];
if (!requestSettings['url']) { if (!requestSettings['url']) {
return; return;
} }
if (skipValidator === false) { if (skipValidator === false && xAction === 'customize:form:request') {
await form.submit(); await form.submit();
} }
@ -241,7 +244,7 @@ export const useCustomizeRequestActionProps = () => {
const params = requestSettings['params'] ? JSON.parse(requestSettings['params']) : {}; const params = requestSettings['params'] ? JSON.parse(requestSettings['params']) : {};
const data = requestSettings['data'] ? JSON.parse(requestSettings['data']) : {}; const data = requestSettings['data'] ? JSON.parse(requestSettings['data']) : {};
const methods = ['POST', 'PUT', 'PATCH']; const methods = ['POST', 'PUT', 'PATCH'];
if (actionSchema['x-action'] === 'customize:form:request' && methods.includes(requestSettings['method'])) { if (xAction === 'customize:form:request' && methods.includes(requestSettings['method'])) {
const fieldNames = fields.map((field) => field.name); const fieldNames = fields.map((field) => field.name);
const values = getFormValues(filterByTk, field, form, fieldNames, getField, resource); const values = getFormValues(filterByTk, field, form, fieldNames, getField, resource);
Object.assign(data, values); Object.assign(data, values);
@ -253,29 +256,40 @@ export const useCustomizeRequestActionProps = () => {
params: SchemaCompiler.compile(params, { currentRecord, currentUser }), params: SchemaCompiler.compile(params, { currentRecord, currentUser }),
data: SchemaCompiler.compile(data, { currentRecord, currentUser }), data: SchemaCompiler.compile(data, { currentRecord, currentUser }),
}; };
actionField.data = field.data || {};
await apiClient.request({ actionField.data.loading = true;
...requestBody, try {
}); await apiClient.request({
...requestBody,
if (!onSuccess?.successMessage) {
return;
}
if (onSuccess?.manualClose) {
Modal.success({
title: compile(onSuccess?.successMessage),
onOk: async () => {
if (onSuccess?.redirecting && onSuccess?.redirectTo) {
if (isURL(onSuccess.redirectTo)) {
window.location.href = onSuccess.redirectTo;
} else {
history.push(onSuccess.redirectTo);
}
}
},
}); });
} else { actionField.data.loading = false;
message.success(compile(onSuccess?.successMessage));
service?.refresh?.();
if (!(resource instanceof TableFieldResource)) {
__parent?.service?.refresh?.();
}
if (!onSuccess?.successMessage) {
return;
}
if (onSuccess?.manualClose) {
Modal.success({
title: compile(onSuccess?.successMessage),
onOk: async () => {
if (onSuccess?.redirecting && onSuccess?.redirectTo) {
if (isURL(onSuccess.redirectTo)) {
window.location.href = onSuccess.redirectTo;
} else {
history.push(onSuccess.redirectTo);
}
}
},
});
} else {
message.success(compile(onSuccess?.successMessage));
}
} finally {
actionField.data.loading = false;
} }
}, },
}; };
@ -311,8 +325,8 @@ export const useUpdateActionProps = () => {
...assignedValues, ...assignedValues,
}, },
}); });
actionField.data.loading = false; actionField.data.loading = false;
if (!(resource instanceof TableFieldResource)) { if (!(resource instanceof TableFieldResource)) {
__parent?.__parent?.service?.refresh?.(); __parent?.__parent?.service?.refresh?.();
} }
__parent?.service?.refresh?.(); __parent?.service?.refresh?.();

View File

@ -112,6 +112,7 @@ export const FormActionInitializers = {
'x-designer': 'Action.Designer', 'x-designer': 'Action.Designer',
'x-action-settings': { 'x-action-settings': {
requestSettings: {}, requestSettings: {},
skipValidator: false,
onSuccess: { onSuccess: {
manualClose: false, manualClose: false,
redirecting: false, redirecting: false,
@ -241,6 +242,7 @@ export const CreateFormActionInitializers = {
'x-designer': 'Action.Designer', 'x-designer': 'Action.Designer',
'x-action-settings': { 'x-action-settings': {
requestSettings: {}, requestSettings: {},
skipValidator: false,
onSuccess: { onSuccess: {
manualClose: false, manualClose: false,
redirecting: false, redirecting: false,
@ -370,6 +372,7 @@ export const UpdateFormActionInitializers = {
'x-designer': 'Action.Designer', 'x-designer': 'Action.Designer',
'x-action-settings': { 'x-action-settings': {
requestSettings: {}, requestSettings: {},
skipValidator: false,
onSuccess: { onSuccess: {
manualClose: false, manualClose: false,
redirecting: false, redirecting: false,

View File

@ -124,6 +124,7 @@ export const ReadPrettyFormActionInitializers = {
'x-designer': 'Action.Designer', 'x-designer': 'Action.Designer',
'x-action-settings': { 'x-action-settings': {
requestSettings: {}, requestSettings: {},
skipValidator: false,
onSuccess: { onSuccess: {
manualClose: false, manualClose: false,
redirecting: false, redirecting: false,