@infosoftas/s4-orders-js-sdk 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +8 -8
- package/.release-it.json +10 -10
- package/README.md +27 -19
- package/build/postcss-scope-tailwind.cjs +0 -0
- package/components.json +0 -0
- package/package.json +1 -1
- package/src/App.completion.test.tsx +46 -107
- package/src/App.css +0 -14
- package/src/App.paymentReturn.test.tsx +341 -0
- package/src/App.tsx +22 -53
- package/src/ErrorBoundary.tsx +41 -41
- package/src/api/InvoiceApi.ts +23 -23
- package/src/api/SubscribeApi.ts +18 -18
- package/src/bundle.ts +4 -4
- package/src/components/Alert/Alert.tsx +23 -23
- package/src/components/Button/Button.tsx +39 -39
- package/src/components/EHFForm/EHFForm.tsx +1 -4
- package/src/components/FormFields/AddressField.tsx +55 -55
- package/src/components/FormFields/EmailField.tsx +53 -53
- package/src/components/FormFields/FormFields.helper.ts +34 -34
- package/src/components/FormFields/FormFieldsMapper.ts +34 -34
- package/src/components/FormFields/InputField.tsx +52 -52
- package/src/components/FormFields/NameField.tsx +52 -52
- package/src/components/FormFields/PhoneField.tsx +53 -53
- package/src/components/FormFields/TermsCheckbox.tsx +70 -70
- package/src/components/FormFields/ToggleFiled.tsx +52 -52
- package/src/components/FormFields/fieldValidators.ts +71 -71
- package/src/components/OIOForm/OIOForm.tsx +1 -4
- package/src/components/OrderDenialModal/OrderDenialModal.tsx +102 -102
- package/src/components/OrderForm/ContactDetailsSection.tsx +71 -71
- package/src/components/OrderForm/OrderForm.tsx +1 -4
- package/src/components/OrderForm/PaymentMethodContent.tsx +62 -62
- package/src/components/OrderForm/PaymentMethodSection.tsx +79 -79
- package/src/components/OrderForm/SubmitConfirmationDialog.tsx +55 -55
- package/src/components/OrderForm/orderFormState.ts +171 -171
- package/src/components/OrderForm/useSlotElement.tsx +89 -89
- package/src/components/ui/alert-dialog.tsx +0 -0
- package/src/components/ui/alert.tsx +0 -0
- package/src/components/ui/button.tsx +0 -0
- package/src/components/ui/card.tsx +0 -0
- package/src/components/ui/checkbox.tsx +0 -0
- package/src/components/ui/field.tsx +0 -0
- package/src/components/ui/input.tsx +0 -0
- package/src/components/ui/label.tsx +0 -0
- package/src/components/ui/radio-group.tsx +0 -0
- package/src/components/ui/separator.tsx +0 -0
- package/src/components/ui/skeleton.tsx +0 -0
- package/src/components/ui/spinner.tsx +0 -0
- package/src/components/ui/switch.tsx +0 -0
- package/src/components/ui/textarea.tsx +0 -0
- package/src/constants/index.ts +13 -13
- package/src/custom.d.ts +0 -0
- package/src/enums/api.ts +5 -5
- package/src/enums/general.ts +4 -2
- package/src/hooks/useOrderForm.test.tsx +86 -0
- package/src/hooks/useOrderForm.tsx +2 -5
- package/src/hooks/usePaymentReturn.tsx +139 -0
- package/src/hooks/useQueryParams.ts +5 -5
- package/src/index.tsx +30 -30
- package/src/lib/utils.ts +1 -1
- package/src/types/api.ts +28 -28
- package/src/types/general.ts +1 -2
- package/src/utils/helper.ts +78 -78
- package/src/utils/order.helper.test.ts +94 -0
- package/src/utils/order.helper.ts +41 -23
- package/tsconfig.json +1 -0
- package/src/components/MainIframe/MainIframe.tsx +0 -42
- package/src/hooks/useIframe.tsx +0 -59
- package/src/hooks/useMessageEvent.tsx +0 -194
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import type { FC } from 'react';
|
|
2
|
-
|
|
3
|
-
import type { SubmitConfirmationType } from '../../types/general';
|
|
4
|
-
import {
|
|
5
|
-
AlertDialog,
|
|
6
|
-
AlertDialogAction,
|
|
7
|
-
AlertDialogCancel,
|
|
8
|
-
AlertDialogContent,
|
|
9
|
-
AlertDialogDescription,
|
|
10
|
-
AlertDialogFooter,
|
|
11
|
-
AlertDialogHeader,
|
|
12
|
-
AlertDialogTitle,
|
|
13
|
-
} from '../ui/alert-dialog';
|
|
14
|
-
|
|
15
|
-
type Props = {
|
|
16
|
-
isOpen: boolean;
|
|
17
|
-
confirmation: SubmitConfirmationType;
|
|
18
|
-
onConfirm: () => void;
|
|
19
|
-
onCancel: () => void;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const SubmitConfirmationDialog: FC<Props> = ({
|
|
23
|
-
isOpen,
|
|
24
|
-
confirmation,
|
|
25
|
-
onConfirm,
|
|
26
|
-
onCancel,
|
|
27
|
-
}) => (
|
|
28
|
-
<AlertDialog
|
|
29
|
-
open={isOpen}
|
|
30
|
-
onOpenChange={(open) => {
|
|
31
|
-
if (!open) onCancel();
|
|
32
|
-
}}
|
|
33
|
-
>
|
|
34
|
-
<AlertDialogContent data-testid="sdk-submit-confirmation-dialog">
|
|
35
|
-
<AlertDialogHeader>
|
|
36
|
-
<AlertDialogTitle>{confirmation.title}</AlertDialogTitle>
|
|
37
|
-
{confirmation.description && (
|
|
38
|
-
<AlertDialogDescription>
|
|
39
|
-
{confirmation.description}
|
|
40
|
-
</AlertDialogDescription>
|
|
41
|
-
)}
|
|
42
|
-
</AlertDialogHeader>
|
|
43
|
-
<AlertDialogFooter>
|
|
44
|
-
<AlertDialogCancel variant="ghost">
|
|
45
|
-
{confirmation.cancelText}
|
|
46
|
-
</AlertDialogCancel>
|
|
47
|
-
<AlertDialogAction onClick={onConfirm}>
|
|
48
|
-
{confirmation.confirmText}
|
|
49
|
-
</AlertDialogAction>
|
|
50
|
-
</AlertDialogFooter>
|
|
51
|
-
</AlertDialogContent>
|
|
52
|
-
</AlertDialog>
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
export default SubmitConfirmationDialog;
|
|
1
|
+
import type { FC } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { SubmitConfirmationType } from '../../types/general';
|
|
4
|
+
import {
|
|
5
|
+
AlertDialog,
|
|
6
|
+
AlertDialogAction,
|
|
7
|
+
AlertDialogCancel,
|
|
8
|
+
AlertDialogContent,
|
|
9
|
+
AlertDialogDescription,
|
|
10
|
+
AlertDialogFooter,
|
|
11
|
+
AlertDialogHeader,
|
|
12
|
+
AlertDialogTitle,
|
|
13
|
+
} from '../ui/alert-dialog';
|
|
14
|
+
|
|
15
|
+
type Props = {
|
|
16
|
+
isOpen: boolean;
|
|
17
|
+
confirmation: SubmitConfirmationType;
|
|
18
|
+
onConfirm: () => void;
|
|
19
|
+
onCancel: () => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const SubmitConfirmationDialog: FC<Props> = ({
|
|
23
|
+
isOpen,
|
|
24
|
+
confirmation,
|
|
25
|
+
onConfirm,
|
|
26
|
+
onCancel,
|
|
27
|
+
}) => (
|
|
28
|
+
<AlertDialog
|
|
29
|
+
open={isOpen}
|
|
30
|
+
onOpenChange={(open) => {
|
|
31
|
+
if (!open) onCancel();
|
|
32
|
+
}}
|
|
33
|
+
>
|
|
34
|
+
<AlertDialogContent data-testid="sdk-submit-confirmation-dialog">
|
|
35
|
+
<AlertDialogHeader>
|
|
36
|
+
<AlertDialogTitle>{confirmation.title}</AlertDialogTitle>
|
|
37
|
+
{confirmation.description && (
|
|
38
|
+
<AlertDialogDescription>
|
|
39
|
+
{confirmation.description}
|
|
40
|
+
</AlertDialogDescription>
|
|
41
|
+
)}
|
|
42
|
+
</AlertDialogHeader>
|
|
43
|
+
<AlertDialogFooter>
|
|
44
|
+
<AlertDialogCancel variant="ghost">
|
|
45
|
+
{confirmation.cancelText}
|
|
46
|
+
</AlertDialogCancel>
|
|
47
|
+
<AlertDialogAction onClick={onConfirm}>
|
|
48
|
+
{confirmation.confirmText}
|
|
49
|
+
</AlertDialogAction>
|
|
50
|
+
</AlertDialogFooter>
|
|
51
|
+
</AlertDialogContent>
|
|
52
|
+
</AlertDialog>
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
export default SubmitConfirmationDialog;
|
|
@@ -1,171 +1,171 @@
|
|
|
1
|
-
import { z, type ZodTypeAny } from 'zod';
|
|
2
|
-
|
|
3
|
-
import { PaymentMethodEnum } from '../../enums/general';
|
|
4
|
-
import {
|
|
5
|
-
PAYMENT_METHOD_DEFAULT,
|
|
6
|
-
INVOICE_ALLOWED_PAYMENT_METHODS,
|
|
7
|
-
} from '../../constants/index';
|
|
8
|
-
import formFieldsMapper from '../FormFields/FormFieldsMapper';
|
|
9
|
-
import { DEFAULT_ORDER_FORM_FIELDS } from '../FormFields/FormFields.helper';
|
|
10
|
-
import { buildFieldSchema } from '../FormFields/fieldValidators';
|
|
11
|
-
import { orderInvoiceContactFields } from '../../utils/order.helper';
|
|
12
|
-
import type { OrderFormInputsType } from '../../types/order';
|
|
13
|
-
import type {
|
|
14
|
-
PaymentMethodSettingsType,
|
|
15
|
-
PaymentMethodOptionType,
|
|
16
|
-
OrderFormFieldType,
|
|
17
|
-
} from '../../types/general';
|
|
18
|
-
|
|
19
|
-
export type InvoiceAddressSelection = {
|
|
20
|
-
enabled?: boolean;
|
|
21
|
-
label?: string;
|
|
22
|
-
fields?: OrderFormFieldType[];
|
|
23
|
-
paymentMethods?: PaymentMethodEnum[];
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type OrderFormConfig = {
|
|
27
|
-
paymentMethods: PaymentMethodOptionType[];
|
|
28
|
-
paymentMethodsOptions?: PaymentMethodSettingsType;
|
|
29
|
-
allowedPaymentMethods?: PaymentMethodEnum[];
|
|
30
|
-
invoiceAddressSelection?: InvoiceAddressSelection;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export type OrderFormValues = Pick<
|
|
34
|
-
OrderFormInputsType,
|
|
35
|
-
'paymentMethod' | 'invoiceAddressSelection'
|
|
36
|
-
>;
|
|
37
|
-
|
|
38
|
-
export type OrderFormDerived = {
|
|
39
|
-
paymentMethod: PaymentMethodEnum;
|
|
40
|
-
availablePaymentMethods: PaymentMethodEnum[];
|
|
41
|
-
orderFields: OrderFormFieldType[];
|
|
42
|
-
invoiceOrderFields: OrderFormFieldType[];
|
|
43
|
-
invoicePaymentMethods: PaymentMethodEnum[];
|
|
44
|
-
allowPaymentMethod: boolean;
|
|
45
|
-
showPaymentMethodSelection: boolean;
|
|
46
|
-
showOrderFields: boolean;
|
|
47
|
-
showInvoiceToggle: boolean;
|
|
48
|
-
showInvoiceFields: boolean;
|
|
49
|
-
showContactDetails: boolean;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export const deriveOrderFormState = (
|
|
53
|
-
values: OrderFormValues,
|
|
54
|
-
{
|
|
55
|
-
paymentMethods,
|
|
56
|
-
paymentMethodsOptions,
|
|
57
|
-
allowedPaymentMethods,
|
|
58
|
-
invoiceAddressSelection,
|
|
59
|
-
}: OrderFormConfig
|
|
60
|
-
): OrderFormDerived => {
|
|
61
|
-
const paymentMethod = (values.paymentMethod ||
|
|
62
|
-
PAYMENT_METHOD_DEFAULT) as PaymentMethodEnum;
|
|
63
|
-
|
|
64
|
-
const orderFields =
|
|
65
|
-
paymentMethodsOptions?.[paymentMethod]?.orderFormFields ??
|
|
66
|
-
DEFAULT_ORDER_FORM_FIELDS;
|
|
67
|
-
|
|
68
|
-
const invoiceOrderFields =
|
|
69
|
-
paymentMethodsOptions?.[paymentMethod]?.paymentInvoiceFields ||
|
|
70
|
-
invoiceAddressSelection?.fields ||
|
|
71
|
-
orderInvoiceContactFields;
|
|
72
|
-
|
|
73
|
-
const invoicePaymentMethods =
|
|
74
|
-
invoiceAddressSelection?.paymentMethods ||
|
|
75
|
-
INVOICE_ALLOWED_PAYMENT_METHODS;
|
|
76
|
-
|
|
77
|
-
const allowPaymentMethod =
|
|
78
|
-
!allowedPaymentMethods?.length ||
|
|
79
|
-
allowedPaymentMethods.includes(paymentMethod);
|
|
80
|
-
|
|
81
|
-
const showOrderFields = orderFields?.length > 0 && allowPaymentMethod;
|
|
82
|
-
|
|
83
|
-
const showInvoiceToggle = !!(
|
|
84
|
-
invoiceAddressSelection?.enabled &&
|
|
85
|
-
allowPaymentMethod &&
|
|
86
|
-
invoicePaymentMethods.includes(paymentMethod)
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
const showInvoiceFields = !!(
|
|
90
|
-
values.invoiceAddressSelection &&
|
|
91
|
-
invoiceOrderFields?.length > 0 &&
|
|
92
|
-
allowPaymentMethod
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
paymentMethod,
|
|
97
|
-
availablePaymentMethods: paymentMethods.map((m) => m.value),
|
|
98
|
-
orderFields,
|
|
99
|
-
invoiceOrderFields,
|
|
100
|
-
invoicePaymentMethods,
|
|
101
|
-
allowPaymentMethod,
|
|
102
|
-
showPaymentMethodSelection: paymentMethods.length > 1,
|
|
103
|
-
showOrderFields,
|
|
104
|
-
showInvoiceToggle,
|
|
105
|
-
showInvoiceFields,
|
|
106
|
-
showContactDetails:
|
|
107
|
-
showOrderFields || showInvoiceToggle || showInvoiceFields,
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export type OrderFormMessages = {
|
|
112
|
-
errorReqMsg?: string;
|
|
113
|
-
errorInvalidEmailMsg?: string;
|
|
114
|
-
errorInvalidPhoneMsg?: string;
|
|
115
|
-
errorTermsMsg?: string;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
export const buildOrderFormSchema = (
|
|
119
|
-
derived: OrderFormDerived,
|
|
120
|
-
requireTermsAcceptance: boolean | undefined,
|
|
121
|
-
{
|
|
122
|
-
errorReqMsg,
|
|
123
|
-
errorInvalidEmailMsg,
|
|
124
|
-
errorInvalidPhoneMsg,
|
|
125
|
-
errorTermsMsg,
|
|
126
|
-
}: OrderFormMessages
|
|
127
|
-
): z.ZodType<OrderFormInputsType, OrderFormInputsType> => {
|
|
128
|
-
const shape: Record<string, ZodTypeAny> = {};
|
|
129
|
-
const messages = {
|
|
130
|
-
errorReqMsg,
|
|
131
|
-
errorInvalidEmailMsg,
|
|
132
|
-
errorInvalidPhoneMsg,
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const addFields = (fields: OrderFormFieldType[]) => {
|
|
136
|
-
fields.forEach((field) => {
|
|
137
|
-
if (!formFieldsMapper[field.name]) return;
|
|
138
|
-
|
|
139
|
-
shape[field.name] = buildFieldSchema(
|
|
140
|
-
field.name,
|
|
141
|
-
field.required || false,
|
|
142
|
-
messages
|
|
143
|
-
);
|
|
144
|
-
});
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
if (derived.showOrderFields) addFields(derived.orderFields);
|
|
148
|
-
if (derived.showInvoiceFields) addFields(derived.invoiceOrderFields);
|
|
149
|
-
|
|
150
|
-
if (derived.availablePaymentMethods.length > 0) {
|
|
151
|
-
const available: string[] = derived.availablePaymentMethods;
|
|
152
|
-
shape.paymentMethod = z
|
|
153
|
-
.string()
|
|
154
|
-
.refine((value) => available.includes(value), {
|
|
155
|
-
message: errorReqMsg || 'This field is required!',
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (requireTermsAcceptance) {
|
|
160
|
-
shape.termsAccept = z.boolean().refine((value) => value === true, {
|
|
161
|
-
message:
|
|
162
|
-
errorTermsMsg ||
|
|
163
|
-
'You must accept the terms and conditions to proceed.',
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return z.object(shape).loose() as unknown as z.ZodType<
|
|
168
|
-
OrderFormInputsType,
|
|
169
|
-
OrderFormInputsType
|
|
170
|
-
>;
|
|
171
|
-
};
|
|
1
|
+
import { z, type ZodTypeAny } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { PaymentMethodEnum } from '../../enums/general';
|
|
4
|
+
import {
|
|
5
|
+
PAYMENT_METHOD_DEFAULT,
|
|
6
|
+
INVOICE_ALLOWED_PAYMENT_METHODS,
|
|
7
|
+
} from '../../constants/index';
|
|
8
|
+
import formFieldsMapper from '../FormFields/FormFieldsMapper';
|
|
9
|
+
import { DEFAULT_ORDER_FORM_FIELDS } from '../FormFields/FormFields.helper';
|
|
10
|
+
import { buildFieldSchema } from '../FormFields/fieldValidators';
|
|
11
|
+
import { orderInvoiceContactFields } from '../../utils/order.helper';
|
|
12
|
+
import type { OrderFormInputsType } from '../../types/order';
|
|
13
|
+
import type {
|
|
14
|
+
PaymentMethodSettingsType,
|
|
15
|
+
PaymentMethodOptionType,
|
|
16
|
+
OrderFormFieldType,
|
|
17
|
+
} from '../../types/general';
|
|
18
|
+
|
|
19
|
+
export type InvoiceAddressSelection = {
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
label?: string;
|
|
22
|
+
fields?: OrderFormFieldType[];
|
|
23
|
+
paymentMethods?: PaymentMethodEnum[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type OrderFormConfig = {
|
|
27
|
+
paymentMethods: PaymentMethodOptionType[];
|
|
28
|
+
paymentMethodsOptions?: PaymentMethodSettingsType;
|
|
29
|
+
allowedPaymentMethods?: PaymentMethodEnum[];
|
|
30
|
+
invoiceAddressSelection?: InvoiceAddressSelection;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type OrderFormValues = Pick<
|
|
34
|
+
OrderFormInputsType,
|
|
35
|
+
'paymentMethod' | 'invoiceAddressSelection'
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
export type OrderFormDerived = {
|
|
39
|
+
paymentMethod: PaymentMethodEnum;
|
|
40
|
+
availablePaymentMethods: PaymentMethodEnum[];
|
|
41
|
+
orderFields: OrderFormFieldType[];
|
|
42
|
+
invoiceOrderFields: OrderFormFieldType[];
|
|
43
|
+
invoicePaymentMethods: PaymentMethodEnum[];
|
|
44
|
+
allowPaymentMethod: boolean;
|
|
45
|
+
showPaymentMethodSelection: boolean;
|
|
46
|
+
showOrderFields: boolean;
|
|
47
|
+
showInvoiceToggle: boolean;
|
|
48
|
+
showInvoiceFields: boolean;
|
|
49
|
+
showContactDetails: boolean;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const deriveOrderFormState = (
|
|
53
|
+
values: OrderFormValues,
|
|
54
|
+
{
|
|
55
|
+
paymentMethods,
|
|
56
|
+
paymentMethodsOptions,
|
|
57
|
+
allowedPaymentMethods,
|
|
58
|
+
invoiceAddressSelection,
|
|
59
|
+
}: OrderFormConfig
|
|
60
|
+
): OrderFormDerived => {
|
|
61
|
+
const paymentMethod = (values.paymentMethod ||
|
|
62
|
+
PAYMENT_METHOD_DEFAULT) as PaymentMethodEnum;
|
|
63
|
+
|
|
64
|
+
const orderFields =
|
|
65
|
+
paymentMethodsOptions?.[paymentMethod]?.orderFormFields ??
|
|
66
|
+
DEFAULT_ORDER_FORM_FIELDS;
|
|
67
|
+
|
|
68
|
+
const invoiceOrderFields =
|
|
69
|
+
paymentMethodsOptions?.[paymentMethod]?.paymentInvoiceFields ||
|
|
70
|
+
invoiceAddressSelection?.fields ||
|
|
71
|
+
orderInvoiceContactFields;
|
|
72
|
+
|
|
73
|
+
const invoicePaymentMethods =
|
|
74
|
+
invoiceAddressSelection?.paymentMethods ||
|
|
75
|
+
INVOICE_ALLOWED_PAYMENT_METHODS;
|
|
76
|
+
|
|
77
|
+
const allowPaymentMethod =
|
|
78
|
+
!allowedPaymentMethods?.length ||
|
|
79
|
+
allowedPaymentMethods.includes(paymentMethod);
|
|
80
|
+
|
|
81
|
+
const showOrderFields = orderFields?.length > 0 && allowPaymentMethod;
|
|
82
|
+
|
|
83
|
+
const showInvoiceToggle = !!(
|
|
84
|
+
invoiceAddressSelection?.enabled &&
|
|
85
|
+
allowPaymentMethod &&
|
|
86
|
+
invoicePaymentMethods.includes(paymentMethod)
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const showInvoiceFields = !!(
|
|
90
|
+
values.invoiceAddressSelection &&
|
|
91
|
+
invoiceOrderFields?.length > 0 &&
|
|
92
|
+
allowPaymentMethod
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
paymentMethod,
|
|
97
|
+
availablePaymentMethods: paymentMethods.map((m) => m.value),
|
|
98
|
+
orderFields,
|
|
99
|
+
invoiceOrderFields,
|
|
100
|
+
invoicePaymentMethods,
|
|
101
|
+
allowPaymentMethod,
|
|
102
|
+
showPaymentMethodSelection: paymentMethods.length > 1,
|
|
103
|
+
showOrderFields,
|
|
104
|
+
showInvoiceToggle,
|
|
105
|
+
showInvoiceFields,
|
|
106
|
+
showContactDetails:
|
|
107
|
+
showOrderFields || showInvoiceToggle || showInvoiceFields,
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export type OrderFormMessages = {
|
|
112
|
+
errorReqMsg?: string;
|
|
113
|
+
errorInvalidEmailMsg?: string;
|
|
114
|
+
errorInvalidPhoneMsg?: string;
|
|
115
|
+
errorTermsMsg?: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const buildOrderFormSchema = (
|
|
119
|
+
derived: OrderFormDerived,
|
|
120
|
+
requireTermsAcceptance: boolean | undefined,
|
|
121
|
+
{
|
|
122
|
+
errorReqMsg,
|
|
123
|
+
errorInvalidEmailMsg,
|
|
124
|
+
errorInvalidPhoneMsg,
|
|
125
|
+
errorTermsMsg,
|
|
126
|
+
}: OrderFormMessages
|
|
127
|
+
): z.ZodType<OrderFormInputsType, OrderFormInputsType> => {
|
|
128
|
+
const shape: Record<string, ZodTypeAny> = {};
|
|
129
|
+
const messages = {
|
|
130
|
+
errorReqMsg,
|
|
131
|
+
errorInvalidEmailMsg,
|
|
132
|
+
errorInvalidPhoneMsg,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const addFields = (fields: OrderFormFieldType[]) => {
|
|
136
|
+
fields.forEach((field) => {
|
|
137
|
+
if (!formFieldsMapper[field.name]) return;
|
|
138
|
+
|
|
139
|
+
shape[field.name] = buildFieldSchema(
|
|
140
|
+
field.name,
|
|
141
|
+
field.required || false,
|
|
142
|
+
messages
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
if (derived.showOrderFields) addFields(derived.orderFields);
|
|
148
|
+
if (derived.showInvoiceFields) addFields(derived.invoiceOrderFields);
|
|
149
|
+
|
|
150
|
+
if (derived.availablePaymentMethods.length > 0) {
|
|
151
|
+
const available: string[] = derived.availablePaymentMethods;
|
|
152
|
+
shape.paymentMethod = z
|
|
153
|
+
.string()
|
|
154
|
+
.refine((value) => available.includes(value), {
|
|
155
|
+
message: errorReqMsg || 'This field is required!',
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (requireTermsAcceptance) {
|
|
160
|
+
shape.termsAccept = z.boolean().refine((value) => value === true, {
|
|
161
|
+
message:
|
|
162
|
+
errorTermsMsg ||
|
|
163
|
+
'You must accept the terms and conditions to proceed.',
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return z.object(shape).loose() as unknown as z.ZodType<
|
|
168
|
+
OrderFormInputsType,
|
|
169
|
+
OrderFormInputsType
|
|
170
|
+
>;
|
|
171
|
+
};
|
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type KeyboardEventHandler,
|
|
3
|
-
type ReactNode,
|
|
4
|
-
useLayoutEffect,
|
|
5
|
-
useState,
|
|
6
|
-
} from 'react';
|
|
7
|
-
import { createPortal } from 'react-dom';
|
|
8
|
-
|
|
9
|
-
export type SlotState = {
|
|
10
|
-
pending: boolean;
|
|
11
|
-
element: HTMLElement | null;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const NO_SLOT: SlotState = { pending: false, element: null };
|
|
15
|
-
|
|
16
|
-
const SLOT_WAIT_MS = 1000;
|
|
17
|
-
|
|
18
|
-
export const useSlotElement = (elementId?: string): SlotState => {
|
|
19
|
-
const [state, setState] = useState<SlotState>(() =>
|
|
20
|
-
elementId ? { pending: true, element: null } : NO_SLOT
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
useLayoutEffect(() => {
|
|
24
|
-
if (!elementId) {
|
|
25
|
-
setState(NO_SLOT);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const found = document.getElementById(elementId);
|
|
30
|
-
if (found) {
|
|
31
|
-
setState(
|
|
32
|
-
found.closest('form')
|
|
33
|
-
? NO_SLOT
|
|
34
|
-
: { pending: false, element: found }
|
|
35
|
-
);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
setState((s) =>
|
|
40
|
-
s.pending && !s.element ? s : { pending: true, element: null }
|
|
41
|
-
);
|
|
42
|
-
const observer = new MutationObserver(() => {
|
|
43
|
-
const target = document.getElementById(elementId);
|
|
44
|
-
if (target) {
|
|
45
|
-
observer.disconnect();
|
|
46
|
-
setState(
|
|
47
|
-
target.closest('form')
|
|
48
|
-
? NO_SLOT
|
|
49
|
-
: { pending: false, element: target }
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
observer.observe(document.documentElement, {
|
|
54
|
-
childList: true,
|
|
55
|
-
subtree: true,
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
const timer = window.setTimeout(() => {
|
|
59
|
-
observer.disconnect();
|
|
60
|
-
setState((s) => (s.element ? s : NO_SLOT));
|
|
61
|
-
}, SLOT_WAIT_MS);
|
|
62
|
-
|
|
63
|
-
return () => {
|
|
64
|
-
observer.disconnect();
|
|
65
|
-
window.clearTimeout(timer);
|
|
66
|
-
};
|
|
67
|
-
}, [elementId]);
|
|
68
|
-
|
|
69
|
-
return state;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export const renderInSlot = (
|
|
73
|
-
section: ReactNode,
|
|
74
|
-
slot: SlotState,
|
|
75
|
-
onKeyDown?: KeyboardEventHandler<HTMLDivElement>
|
|
76
|
-
) => {
|
|
77
|
-
if (!section || !slot.pending) {
|
|
78
|
-
return slot.element && section
|
|
79
|
-
? createPortal(
|
|
80
|
-
<div className="sdk-scope" onKeyDown={onKeyDown}>
|
|
81
|
-
{section}
|
|
82
|
-
</div>,
|
|
83
|
-
slot.element
|
|
84
|
-
)
|
|
85
|
-
: section;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return null;
|
|
89
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
type KeyboardEventHandler,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useState,
|
|
6
|
+
} from 'react';
|
|
7
|
+
import { createPortal } from 'react-dom';
|
|
8
|
+
|
|
9
|
+
export type SlotState = {
|
|
10
|
+
pending: boolean;
|
|
11
|
+
element: HTMLElement | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const NO_SLOT: SlotState = { pending: false, element: null };
|
|
15
|
+
|
|
16
|
+
const SLOT_WAIT_MS = 1000;
|
|
17
|
+
|
|
18
|
+
export const useSlotElement = (elementId?: string): SlotState => {
|
|
19
|
+
const [state, setState] = useState<SlotState>(() =>
|
|
20
|
+
elementId ? { pending: true, element: null } : NO_SLOT
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
useLayoutEffect(() => {
|
|
24
|
+
if (!elementId) {
|
|
25
|
+
setState(NO_SLOT);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const found = document.getElementById(elementId);
|
|
30
|
+
if (found) {
|
|
31
|
+
setState(
|
|
32
|
+
found.closest('form')
|
|
33
|
+
? NO_SLOT
|
|
34
|
+
: { pending: false, element: found }
|
|
35
|
+
);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setState((s) =>
|
|
40
|
+
s.pending && !s.element ? s : { pending: true, element: null }
|
|
41
|
+
);
|
|
42
|
+
const observer = new MutationObserver(() => {
|
|
43
|
+
const target = document.getElementById(elementId);
|
|
44
|
+
if (target) {
|
|
45
|
+
observer.disconnect();
|
|
46
|
+
setState(
|
|
47
|
+
target.closest('form')
|
|
48
|
+
? NO_SLOT
|
|
49
|
+
: { pending: false, element: target }
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
observer.observe(document.documentElement, {
|
|
54
|
+
childList: true,
|
|
55
|
+
subtree: true,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const timer = window.setTimeout(() => {
|
|
59
|
+
observer.disconnect();
|
|
60
|
+
setState((s) => (s.element ? s : NO_SLOT));
|
|
61
|
+
}, SLOT_WAIT_MS);
|
|
62
|
+
|
|
63
|
+
return () => {
|
|
64
|
+
observer.disconnect();
|
|
65
|
+
window.clearTimeout(timer);
|
|
66
|
+
};
|
|
67
|
+
}, [elementId]);
|
|
68
|
+
|
|
69
|
+
return state;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const renderInSlot = (
|
|
73
|
+
section: ReactNode,
|
|
74
|
+
slot: SlotState,
|
|
75
|
+
onKeyDown?: KeyboardEventHandler<HTMLDivElement>
|
|
76
|
+
) => {
|
|
77
|
+
if (!section || !slot.pending) {
|
|
78
|
+
return slot.element && section
|
|
79
|
+
? createPortal(
|
|
80
|
+
<div className="sdk-scope" onKeyDown={onKeyDown}>
|
|
81
|
+
{section}
|
|
82
|
+
</div>,
|
|
83
|
+
slot.element
|
|
84
|
+
)
|
|
85
|
+
: section;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return null;
|
|
89
|
+
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|