forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
152 lines (138 loc) · 5.05 KB
/
index.ts
File metadata and controls
152 lines (138 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { useConfigurableAction } from "@/providers/configurableActionsDispatcher";
import { IShaFormInstance } from "../store/interfaces";
import { SheshaActionOwners } from "@/providers/configurableActionsDispatcher/models";
import { hasPreviousActionError } from "@/interfaces/configurableAction";
import { ISubmitActionArguments } from "../models";
import { useRef } from "react";
export type UseShaFormActionsArgs = {
name: string;
isActionsOwner: boolean;
shaForm: IShaFormInstance;
};
export const useShaFormActions = ({ name, isActionsOwner, shaForm }: UseShaFormActionsArgs) => {
const actionsOwnerUid = isActionsOwner ? SheshaActionOwners.Form : null;
const actionDependencies = [actionsOwnerUid];
const prevFormData = useRef(null);
useConfigurableAction(
{
name: 'Start Edit',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: () => {
prevFormData.current = shaForm.formData;
shaForm.setFormMode('edit');
return Promise.resolve();
},
},
actionDependencies
);
useConfigurableAction(
{
name: 'Cancel Edit',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: () => {
shaForm.resetFields();
shaForm.setFormData({ values: prevFormData?.current, mergeValues: true });
shaForm.setFormMode('readonly');
return Promise.resolve();
},
},
actionDependencies
);
useConfigurableAction(
{
name: 'Submit',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: async (args: ISubmitActionArguments, actionContext) => {
var formInstance = (actionContext?.form?.formInstance ?? shaForm.antdForm);
var fieldsToValidate = actionContext?.fieldsToValidate ?? null;
if (args?.validateFields === true || fieldsToValidate?.length > 0) {
await formInstance.validateFields(fieldsToValidate);
}
formInstance.submit();
return Promise.resolve();
},
},
actionDependencies
);
useConfigurableAction(
{
name: 'Reset',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: () => {
shaForm.resetFields();
return Promise.resolve();
},
},
actionDependencies
);
useConfigurableAction(
{
name: 'Refresh',
description: 'Refresh the form data by fetching it from the back-end',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: () => {
return shaForm.fetchData();
},
},
actionDependencies
);
useConfigurableAction(
{
name: 'Validate',
description: 'Validate the form data and show validation errors if any',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: async (_, actionContext) => {
var formInstance = actionContext?.form?.formInstance ?? shaForm.antdForm;
var fieldsToValidate = actionContext?.fieldsToValidate ?? null;
await formInstance.validateFields(fieldsToValidate);
return Promise.resolve();
},
},
actionDependencies
);
useConfigurableAction<{ data: object }>(
{
name: 'Set validation errors',
description: 'Errors are displayed on the Validation Errors component attached to the form',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: async (_args, actionContext) => {
if (hasPreviousActionError(actionContext)) {
const error = actionContext.actionError instanceof Error
? { message: actionContext.actionError.message }
: actionContext.actionError;
shaForm.setValidationErrors(error);
}
return Promise.resolve();
},
},
actionDependencies
);
useConfigurableAction(
{
name: 'Reset validation errors',
description: 'Clear errors displayed on the Validation Errors component attached to the form',
owner: name,
ownerUid: actionsOwnerUid,
hasArguments: false,
executer: async () => {
shaForm.setValidationErrors(undefined);
return Promise.resolve();
},
},
actionDependencies
);
};