@infosoftas/s4-orders-js-sdk 0.1.21-3 → 0.2.0
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 +565 -471
- package/package.json +62 -58
- package/src/App.completion.test.tsx +738 -0
- package/src/App.tsx +753 -639
- package/src/ErrorBoundary.tsx +41 -41
- package/src/api/InvoiceApi.ts +23 -23
- package/src/api/OrdersApi.ts +74 -59
- 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 +253 -253
- 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/MainIframe/MainIframe.tsx +42 -42
- package/src/components/OIOForm/OIOForm.tsx +307 -307
- package/src/components/OrderDenialModal/OrderDenialModal.tsx +102 -102
- package/src/components/OrderForm/ContactDetailsSection.tsx +71 -71
- package/src/components/OrderForm/OrderForm.tsx +420 -420
- 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/constants/index.ts +13 -13
- package/src/enums/api.ts +5 -5
- package/src/enums/general.ts +40 -40
- package/src/hooks/useIframe.tsx +59 -59
- package/src/hooks/useMessageEvent.tsx +194 -194
- package/src/hooks/useOrderForm.tsx +403 -403
- 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 +154 -136
- package/src/types/order.ts +91 -70
- package/src/utils/fetcher.ts +131 -113
- package/src/utils/helper.ts +78 -78
- package/src/utils/order.helper.ts +272 -272
- package/src/utils/waitForOrderCompletion.ts +125 -0
- package/tsconfig.json +38 -38
|
@@ -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
|
+
};
|
package/src/constants/index.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { PaymentMethodEnum } from '../enums/general';
|
|
2
|
-
|
|
3
|
-
export const BASE_API = '/';
|
|
4
|
-
|
|
5
|
-
export const WRONG_MSG = 'Something went wrong!';
|
|
6
|
-
|
|
7
|
-
export const PAYMENT_METHOD_DEFAULT: PaymentMethodEnum =
|
|
8
|
-
PaymentMethodEnum.Vipps;
|
|
9
|
-
|
|
10
|
-
export const INVOICE_ALLOWED_PAYMENT_METHODS = [
|
|
11
|
-
PaymentMethodEnum.Email,
|
|
12
|
-
PaymentMethodEnum.Invoice,
|
|
13
|
-
];
|
|
1
|
+
import { PaymentMethodEnum } from '../enums/general';
|
|
2
|
+
|
|
3
|
+
export const BASE_API = '/';
|
|
4
|
+
|
|
5
|
+
export const WRONG_MSG = 'Something went wrong!';
|
|
6
|
+
|
|
7
|
+
export const PAYMENT_METHOD_DEFAULT: PaymentMethodEnum =
|
|
8
|
+
PaymentMethodEnum.Vipps;
|
|
9
|
+
|
|
10
|
+
export const INVOICE_ALLOWED_PAYMENT_METHODS = [
|
|
11
|
+
PaymentMethodEnum.Email,
|
|
12
|
+
PaymentMethodEnum.Invoice,
|
|
13
|
+
];
|
package/src/enums/api.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export enum HttpStatusCode {
|
|
2
|
-
UNAUTHORIZED = 401,
|
|
3
|
-
FORBIDDEN = 403,
|
|
4
|
-
LIMIT = 429,
|
|
5
|
-
}
|
|
1
|
+
export enum HttpStatusCode {
|
|
2
|
+
UNAUTHORIZED = 401,
|
|
3
|
+
FORBIDDEN = 403,
|
|
4
|
+
LIMIT = 429,
|
|
5
|
+
}
|
package/src/enums/general.ts
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
export enum PaymentMethodEnum {
|
|
2
|
-
Vipps = 'Vipps',
|
|
3
|
-
MobilePay = 'MobilePay',
|
|
4
|
-
SwedbankPay = 'SwedbankPay',
|
|
5
|
-
Mollie = 'Mollie',
|
|
6
|
-
Invoice = 'Invoice',
|
|
7
|
-
Email = 'Email',
|
|
8
|
-
EHF = 'EHF',
|
|
9
|
-
OIO = 'OIO',
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export enum MessageEventTypeEnum {
|
|
13
|
-
ORDER_FLOW_COMPLETE = 'orderFlowComplete',
|
|
14
|
-
ORDER_FLOW_CANCEL = 'orderFlowCancel',
|
|
15
|
-
ORDER_UPDATE_INFO = 'orderUpdateInfo',
|
|
16
|
-
VIPPS_COMPLETE = 'completeOrder',
|
|
17
|
-
CANCEL = 'cancel',
|
|
18
|
-
COMPLETE = 'complete',
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export enum InvoiceLookupNetworkEnum {
|
|
22
|
-
EHF = '0192',
|
|
23
|
-
OIO_GLN = '0088',
|
|
24
|
-
OIO_Danish_CVR = '0096',
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export enum FormTypeEnum {
|
|
28
|
-
ORDER = 'order',
|
|
29
|
-
EHF = 'EHF',
|
|
30
|
-
OIO = 'OIO',
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export enum UserActionEnum {
|
|
34
|
-
SELECT_FORM = 'selectForm',
|
|
35
|
-
SELECT_PAYMENT_METHOD = 'selectPaymentMethod',
|
|
36
|
-
SELECT_FALLBACK_OFFER = 'selectFallbackOffer',
|
|
37
|
-
RETURN_TO_MAIN = 'returnToMain',
|
|
38
|
-
TOGGLE_INVOICE_ADDRESS = 'toggleInvoiceAddress',
|
|
39
|
-
SEARCH_ORGANIZATION_NUMBER = 'searchOrganizationNumber',
|
|
40
|
-
}
|
|
1
|
+
export enum PaymentMethodEnum {
|
|
2
|
+
Vipps = 'Vipps',
|
|
3
|
+
MobilePay = 'MobilePay',
|
|
4
|
+
SwedbankPay = 'SwedbankPay',
|
|
5
|
+
Mollie = 'Mollie',
|
|
6
|
+
Invoice = 'Invoice',
|
|
7
|
+
Email = 'Email',
|
|
8
|
+
EHF = 'EHF',
|
|
9
|
+
OIO = 'OIO',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export enum MessageEventTypeEnum {
|
|
13
|
+
ORDER_FLOW_COMPLETE = 'orderFlowComplete',
|
|
14
|
+
ORDER_FLOW_CANCEL = 'orderFlowCancel',
|
|
15
|
+
ORDER_UPDATE_INFO = 'orderUpdateInfo',
|
|
16
|
+
VIPPS_COMPLETE = 'completeOrder',
|
|
17
|
+
CANCEL = 'cancel',
|
|
18
|
+
COMPLETE = 'complete',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export enum InvoiceLookupNetworkEnum {
|
|
22
|
+
EHF = '0192',
|
|
23
|
+
OIO_GLN = '0088',
|
|
24
|
+
OIO_Danish_CVR = '0096',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export enum FormTypeEnum {
|
|
28
|
+
ORDER = 'order',
|
|
29
|
+
EHF = 'EHF',
|
|
30
|
+
OIO = 'OIO',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export enum UserActionEnum {
|
|
34
|
+
SELECT_FORM = 'selectForm',
|
|
35
|
+
SELECT_PAYMENT_METHOD = 'selectPaymentMethod',
|
|
36
|
+
SELECT_FALLBACK_OFFER = 'selectFallbackOffer',
|
|
37
|
+
RETURN_TO_MAIN = 'returnToMain',
|
|
38
|
+
TOGGLE_INVOICE_ADDRESS = 'toggleInvoiceAddress',
|
|
39
|
+
SEARCH_ORGANIZATION_NUMBER = 'searchOrganizationNumber',
|
|
40
|
+
}
|