forked from impress-org/givewp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
310 lines (263 loc) · 7.02 KB
/
types.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import type {FC} from 'react';
import {FormSettings} from "@givewp/form-builder/types";
/**
* Used for a single currency. The amount is an integer in the smallest unit of the currency.
*/
export interface Currency {
/**
* Amount as an integer in the smallest unit of the currency.
*/
amount: bigint;
/**
* 3-Character Currency code (e.g. USD, EUR, GBP, etc.)
*/
currency: string;
}
/**
* @unreleased
*/
export type CurrencySwitcherSetting = {
id: string;
exchangeRate: number;
gateways: string[];
exchangeRateFractionDigits: number;
};
export interface FormData {
honorific?: string;
firstName: string;
lastName?: string;
email: string;
amount: number;
company?: string;
}
export interface FormServerExports {
registeredGateways: RegisteredGateway[];
form: Form;
attributes: object;
donateUrl: string;
validateUrl: string;
authUrl: string;
inlineRedirectRoutes: string[];
}
export interface ReceiptDetail {
label: string;
value: any;
}
export interface DonationConfirmationReceiptServerExports {
receipt: {
settings: {
heading: string;
description: string;
currency: string;
donorDashboardUrl: string;
};
donorDetails: ReceiptDetail[];
donationDetails: ReceiptDetail[];
subscriptionDetails: ReceiptDetail[];
additionalDetails: ReceiptDetail[];
};
}
export interface GatewaySettings {
label: string;
}
export interface RegisteredGateway {
/**
* The gateway ID. Must be the same as the back-end
*/
id: string;
/**
* The gateway label
*/
label?: string;
/**
* Settings for the gateway as sent from the back-end
*/
settings?: GatewaySettings;
/**
* Whether the gateway supports recurring donations
*/
supportsSubscriptions?: boolean;
}
export interface Gateway extends RegisteredGateway {
/**
* Initialize function for the gateway. The settings are passed to the gateway
* from the server. This is called once before the form is rendered.
*/
initialize?(): void;
/**
* The component to render when the gateway is selected
*/
Fields: FC;
/**
* A hook before the form is submitted.
*/
beforeCreatePayment?(values: FormData): Promise<object> | Error;
/**
* A hook after the form is submitted.
*/
afterCreatePayment?(response: FormResponse): Promise<void> | Error;
}
export interface BasicCondition {
type: 'basic';
logicalOperator: 'and' | 'or';
field: string;
comparisonOperator: '==' | '!=' | '>' | '<' | '>=' | '<=' | 'contains' | 'not_contains';
value: any;
}
export interface NestedCondition {
type: 'nested';
boolean: 'and' | 'or';
conditions: FieldCondition[];
}
export type FieldCondition = BasicCondition | NestedCondition;
export interface Node {
name: string;
type: string;
nodeType: string;
visibilityConditions: FieldCondition[];
}
export interface Field extends Node {
nodeType: 'field';
label: string;
placeholder: string | null;
validationRules: {
[key: string]: any;
};
readOnly: boolean;
defaultValue: any;
fieldError: string | null;
}
export interface Group extends Node {
nodeType: 'group';
nodes: Node[];
/**
* Recursively walk the group and its children for all nodes.
*
* @since 0.1.0
*/
walkNodes(callback: (node: Node) => void, filter?: (node: Node) => boolean): void;
/**
* Recursively map the group and its children for all nodes.
*
* @since 0.1.0
*/
mapNodes(callback: (node: Node) => void, filter?: (node: Node) => boolean): Node[];
/**
* Recursively walk the group and its children for all nodes and reduce to a single value.
*
* @since 0.1.0
*/
reduceNodes(
callback: (accumulator: any, node: Node) => any,
initialValue: any,
filter?: (node: Node) => boolean
): any;
}
export interface Element extends Node {
nodeType: 'element';
}
export interface Section extends Group {
label: string;
description: string;
}
export interface Form extends Group {
currency: string;
settings: FormSettings;
stats: {
totalRevenue: number;
totalRevenueFormatted: string;
totalCountValue: number;
totalCountLabel: string;
};
goal: {
type: string;
label: string;
enabled: boolean;
show: boolean;
typeIsCount: boolean;
typeIsMoney: boolean;
currentAmount: number;
targetAmount: number;
progressPercentage: number;
isAchieved: boolean;
};
design?: {
id: string;
name: string;
isMultiStep: boolean;
};
nodes: Section[];
currencySwitcherSettings?: CurrencySwitcherSetting[];
currencySwitcherMessage?: string;
}
export function isField(node: Node): node is Field {
return node.nodeType === 'field';
}
export function isElement(node: Node): node is Element {
return node.nodeType === 'element';
}
export function isGroup(node: Node): node is Group {
return node.nodeType === 'group';
}
export function isSection(node: Node): node is Section {
return node.nodeType === 'section';
}
export interface SelectOption {
label: string;
value: string;
disabled?: boolean;
}
export interface RadioOption {
label: string;
value: string;
}
interface FormResponse {
type: string;
data: any;
}
type FormResponseValidationError = {
[key: string]: string;
};
type FormResponseGatewayError = {
gateway_error: string;
};
interface FormResponseValidationErrors extends FormResponse {
type: 'validation_error';
data: {
errors: {
errors: FormResponseValidationError[];
};
};
}
interface FormResponseGatewayErrors extends FormResponse {
type: 'gateway_error';
data: {
errors: {
errors: FormResponseGatewayError[];
};
};
}
interface FormResponseRedirect extends FormResponse {
type: 'redirect';
data: {
redirectUrl: string;
};
}
export function isFormResponseRedirect(response: FormResponse): response is FormResponseRedirect {
return (response as FormResponseRedirect).type === 'redirect';
}
export function isFormResponseValidationError(response: FormResponse): response is FormResponseValidationErrors {
return (
(response as FormResponseValidationErrors).type === 'validation_error' ||
(response as FormResponseValidationErrors).data?.errors != undefined
);
}
export function isFormResponseGatewayError(response: FormResponse): response is FormResponseGatewayErrors {
return (response as FormResponseGatewayErrors).type === 'gateway_error';
}
export function isResponseRedirected(response: Response): response is Response {
return (response as Response).redirected === true;
}
export function isDonationTypeSubscription(donationType: string): boolean {
return donationType === 'subscription';
}