@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.
Files changed (51) hide show
  1. package/.editorconfig +8 -8
  2. package/.release-it.json +10 -10
  3. package/README.md +565 -471
  4. package/package.json +62 -58
  5. package/src/App.completion.test.tsx +738 -0
  6. package/src/App.tsx +753 -639
  7. package/src/ErrorBoundary.tsx +41 -41
  8. package/src/api/InvoiceApi.ts +23 -23
  9. package/src/api/OrdersApi.ts +74 -59
  10. package/src/api/SubscribeApi.ts +18 -18
  11. package/src/bundle.ts +4 -4
  12. package/src/components/Alert/Alert.tsx +23 -23
  13. package/src/components/Button/Button.tsx +39 -39
  14. package/src/components/EHFForm/EHFForm.tsx +253 -253
  15. package/src/components/FormFields/AddressField.tsx +55 -55
  16. package/src/components/FormFields/EmailField.tsx +53 -53
  17. package/src/components/FormFields/FormFields.helper.ts +34 -34
  18. package/src/components/FormFields/FormFieldsMapper.ts +34 -34
  19. package/src/components/FormFields/InputField.tsx +52 -52
  20. package/src/components/FormFields/NameField.tsx +52 -52
  21. package/src/components/FormFields/PhoneField.tsx +53 -53
  22. package/src/components/FormFields/TermsCheckbox.tsx +70 -70
  23. package/src/components/FormFields/ToggleFiled.tsx +52 -52
  24. package/src/components/FormFields/fieldValidators.ts +71 -71
  25. package/src/components/MainIframe/MainIframe.tsx +42 -42
  26. package/src/components/OIOForm/OIOForm.tsx +307 -307
  27. package/src/components/OrderDenialModal/OrderDenialModal.tsx +102 -102
  28. package/src/components/OrderForm/ContactDetailsSection.tsx +71 -71
  29. package/src/components/OrderForm/OrderForm.tsx +420 -420
  30. package/src/components/OrderForm/PaymentMethodContent.tsx +62 -62
  31. package/src/components/OrderForm/PaymentMethodSection.tsx +79 -79
  32. package/src/components/OrderForm/SubmitConfirmationDialog.tsx +55 -55
  33. package/src/components/OrderForm/orderFormState.ts +171 -171
  34. package/src/components/OrderForm/useSlotElement.tsx +89 -89
  35. package/src/constants/index.ts +13 -13
  36. package/src/enums/api.ts +5 -5
  37. package/src/enums/general.ts +40 -40
  38. package/src/hooks/useIframe.tsx +59 -59
  39. package/src/hooks/useMessageEvent.tsx +194 -194
  40. package/src/hooks/useOrderForm.tsx +403 -403
  41. package/src/hooks/useQueryParams.ts +5 -5
  42. package/src/index.tsx +30 -30
  43. package/src/lib/utils.ts +1 -1
  44. package/src/types/api.ts +28 -28
  45. package/src/types/general.ts +154 -136
  46. package/src/types/order.ts +91 -70
  47. package/src/utils/fetcher.ts +131 -113
  48. package/src/utils/helper.ts +78 -78
  49. package/src/utils/order.helper.ts +272 -272
  50. package/src/utils/waitForOrderCompletion.ts +125 -0
  51. package/tsconfig.json +38 -38
@@ -1,307 +1,307 @@
1
- import { type FC, useId, useMemo } from 'react';
2
- import { useForm, type SubmitHandler, Controller } from 'react-hook-form';
3
-
4
- import Alert from '../Alert/Alert';
5
- import Button from '../Button/Button';
6
- import OrderDenialModal from '../OrderDenialModal/OrderDenialModal';
7
- import { DEFAULT_ORDER_FORM_FIELDS } from '../FormFields/FormFields.helper';
8
-
9
- import { PaymentMethodEnum, UserActionEnum } from '../../enums/general';
10
- import type {
11
- OrderFormFieldType,
12
- PaymentMethodSettingsType,
13
- ContactRequestType,
14
- OrderDenialFallbackOfferType,
15
- } from '../../types/general';
16
- import useOrderForm from '../../hooks/useOrderForm';
17
- import { orderInvoiceContactFields } from '../../utils/order.helper';
18
- import type { OrderFormInputsType, OrderInfoType } from '../../types/order';
19
- import z from 'zod';
20
- import { zodResolver } from '@hookform/resolvers/zod';
21
- import { Field, FieldGroup, FieldLabel, FieldError } from '../ui/field';
22
- import { Input } from '../ui/input';
23
-
24
- type Props = {
25
- callback: (url: string | null, orderInfo?: OrderInfoType | null) => void;
26
- onBack: () => void;
27
- submitStartCallback?: (id: string) => void;
28
- userActionCallback?: (
29
- action: UserActionEnum,
30
- args: object | null | undefined
31
- ) => void;
32
- setContactCallback?: (contactInfo: ContactRequestType) => void;
33
- className?: string;
34
- backButtonText?: string;
35
- verifyButtonText?: string;
36
- orderDenialCloseButtonText?: string;
37
- orderDenialContinueButtonText?: string;
38
- organizationNumberLabel?: string;
39
- organizationNumber?: string;
40
- glnLabel?: string;
41
- templatePackageId: string;
42
- subscriberId?: string;
43
- userId?: string;
44
- identityProviderId?: string;
45
- organizationId: string;
46
- redirectUrl?: string;
47
- showIframe?: boolean;
48
- language: string;
49
- merchantAgreementUrl: string;
50
- orderValues?: OrderFormInputsType;
51
- paymentMethodsOptions?: PaymentMethodSettingsType;
52
- invoiceAddressSelection?: {
53
- enabled?: boolean;
54
- label?: string;
55
- fields?: OrderFormFieldType[];
56
- paymentMethods?: PaymentMethodEnum[];
57
- };
58
- invoiceLookupNotFoundText?: string;
59
- errorReqMsg?: string;
60
- errorValidationTitleMsg?: string;
61
- errorValidationDenialOrderBlockingMsg?: string;
62
- errorValidationBlockingOffersMsg?: string;
63
- orderDenialOfferBaseText?: string;
64
- orderDenialOfferWithFallbackText?: string;
65
- orderDenialAmountText?: string;
66
- fetchDenialFallbackOffer?: (
67
- organizationId: string
68
- ) => Promise<OrderDenialFallbackOfferType | undefined>;
69
- };
70
-
71
- type OIOFormInputsType = {
72
- cvr: string;
73
- gln: string;
74
- };
75
-
76
- const initialData = {
77
- cvr: '',
78
- gln: '',
79
- };
80
-
81
- const buildFormSchema = (errorReqMsg?: string) =>
82
- z
83
- .object({
84
- cvr: z.string().trim(),
85
- gln: z.string().trim(),
86
- })
87
- .superRefine((data, ctx) => {
88
- if (!data.cvr && !data.gln) {
89
- const message = errorReqMsg || 'This field is required!';
90
- ctx.addIssue({
91
- code: z.ZodIssueCode.custom,
92
- message,
93
- path: ['cvr'],
94
- });
95
- ctx.addIssue({
96
- code: z.ZodIssueCode.custom,
97
- message,
98
- path: ['gln'],
99
- });
100
- }
101
- });
102
-
103
- const OIOForm: FC<Props> = ({
104
- callback,
105
- onBack,
106
- submitStartCallback,
107
- userActionCallback,
108
- setContactCallback,
109
- className = '',
110
- backButtonText = '',
111
- verifyButtonText = '',
112
- orderDenialCloseButtonText = 'Close',
113
- orderDenialContinueButtonText = 'Continue',
114
- organizationNumberLabel = '',
115
- organizationNumber = '',
116
- glnLabel = '',
117
- templatePackageId,
118
- subscriberId,
119
- userId,
120
- identityProviderId,
121
- organizationId,
122
- redirectUrl,
123
- showIframe,
124
- language,
125
- merchantAgreementUrl,
126
- orderValues,
127
- paymentMethodsOptions,
128
- invoiceAddressSelection,
129
- invoiceLookupNotFoundText,
130
- errorReqMsg,
131
- errorValidationTitleMsg,
132
- errorValidationDenialOrderBlockingMsg,
133
- errorValidationBlockingOffersMsg,
134
- orderDenialOfferBaseText,
135
- orderDenialOfferWithFallbackText,
136
- orderDenialAmountText,
137
- fetchDenialFallbackOffer,
138
- }) => {
139
- const cvrId = useId();
140
- const glnId = useId();
141
-
142
- const formSchema = useMemo(
143
- () => buildFormSchema(errorReqMsg),
144
- [errorReqMsg]
145
- );
146
-
147
- const form = useForm<OIOFormInputsType>({
148
- resolver: zodResolver(formSchema),
149
- defaultValues: {
150
- ...initialData,
151
- cvr: organizationNumber,
152
- },
153
- });
154
-
155
- const { handleSubmit } = form;
156
-
157
- const invoiceOrderFields =
158
- invoiceAddressSelection?.fields || orderInvoiceContactFields;
159
-
160
- const orderFields =
161
- paymentMethodsOptions?.[orderValues?.paymentMethod as PaymentMethodEnum]
162
- ?.orderFormFields ?? DEFAULT_ORDER_FORM_FIELDS;
163
-
164
- const {
165
- orderSubmit,
166
- continueWithFallbackOffer,
167
- loading,
168
- apiErrorMsg,
169
- errorsMsg,
170
- orderDenialType,
171
- orderDenialMessage,
172
- orderDenialFallbackOffer,
173
- dismissOrderDenial,
174
- } = useOrderForm({
175
- callback,
176
- submitStartCallback,
177
- userActionCallback,
178
- setContactCallback,
179
- organizationId,
180
- subscriberId,
181
- userId,
182
- identityProviderId,
183
- orderFields,
184
- redirectUrl,
185
- showIframe,
186
- language,
187
- merchantAgreementUrl,
188
- paymentMethodsOptions,
189
- templatePackageId,
190
- invoiceAddressToggle: orderValues?.invoiceAddressSelection,
191
- invoiceOrderFields,
192
- invoiceLookupNotFoundText,
193
- errorValidationTitleMsg,
194
- errorValidationDenialOrderBlockingMsg,
195
- errorValidationBlockingOffersMsg,
196
- orderDenialOfferBaseText,
197
- orderDenialOfferWithFallbackText,
198
- orderDenialAmountText,
199
- fetchDenialFallbackOffer,
200
- });
201
-
202
- const onSubmit: SubmitHandler<OIOFormInputsType> = async (
203
- data
204
- ): Promise<void> => {
205
- await orderSubmit({ ...orderValues, gln: data.gln, cvr: data.cvr });
206
- };
207
-
208
- const handleBack = () => {
209
- userActionCallback?.(UserActionEnum.RETURN_TO_MAIN, null);
210
- onBack?.();
211
- };
212
-
213
- return (
214
- <form
215
- className={`${className}`}
216
- noValidate
217
- onSubmit={handleSubmit(onSubmit)}
218
- data-testid="oio-form-id"
219
- >
220
- <FieldGroup>
221
- <Controller
222
- name="cvr"
223
- control={form.control}
224
- render={({ field, fieldState }) => (
225
- <Field data-invalid={fieldState.invalid}>
226
- <FieldLabel htmlFor={cvrId}>
227
- {organizationNumberLabel}
228
- </FieldLabel>
229
- <Input
230
- {...field}
231
- id={cvrId}
232
- aria-invalid={fieldState.invalid}
233
- autoComplete="off"
234
- />
235
- {fieldState.error && (
236
- <FieldError>
237
- {fieldState.error.message}
238
- </FieldError>
239
- )}
240
- </Field>
241
- )}
242
- />
243
-
244
- <Controller
245
- name="gln"
246
- control={form.control}
247
- render={({ field, fieldState }) => (
248
- <Field data-invalid={fieldState.invalid}>
249
- <FieldLabel htmlFor={glnId}>{glnLabel}</FieldLabel>
250
- <Input
251
- {...field}
252
- id={glnId}
253
- aria-invalid={fieldState.invalid}
254
- autoComplete="off"
255
- />
256
- {fieldState.error && (
257
- <FieldError>
258
- {fieldState.error.message}
259
- </FieldError>
260
- )}
261
- </Field>
262
- )}
263
- />
264
- </FieldGroup>
265
- <div className="flex justify-center flex-wrap gap-2">
266
- <Button
267
- type="button"
268
- btnType="default"
269
- disable={loading}
270
- buttonText={backButtonText}
271
- onClick={handleBack}
272
- />
273
-
274
- <Button
275
- type="submit"
276
- loading={loading}
277
- buttonText={verifyButtonText}
278
- />
279
- </div>
280
- {apiErrorMsg && <Alert className="mt-2" msg={apiErrorMsg} />}
281
- {errorsMsg?.length > 0 &&
282
- errorsMsg.map((i, index) => (
283
- <Alert key={`${i}-${index}`} className="mt-2" msg={i} />
284
- ))}
285
- <OrderDenialModal
286
- isOpen={!!orderDenialType}
287
- message={orderDenialMessage}
288
- closeButtonText={orderDenialCloseButtonText}
289
- continueButtonText={orderDenialContinueButtonText}
290
- canContinue={
291
- orderDenialType === 'offer' &&
292
- !!orderDenialFallbackOffer?.templatePackageId
293
- }
294
- offer={
295
- orderDenialType === 'offer' &&
296
- !!orderDenialFallbackOffer?.templatePackageId
297
- ? orderDenialFallbackOffer
298
- : undefined
299
- }
300
- onClose={dismissOrderDenial}
301
- onContinue={continueWithFallbackOffer}
302
- />
303
- </form>
304
- );
305
- };
306
-
307
- export default OIOForm;
1
+ import { type FC, useId, useMemo } from 'react';
2
+ import { useForm, type SubmitHandler, Controller } from 'react-hook-form';
3
+
4
+ import Alert from '../Alert/Alert';
5
+ import Button from '../Button/Button';
6
+ import OrderDenialModal from '../OrderDenialModal/OrderDenialModal';
7
+ import { DEFAULT_ORDER_FORM_FIELDS } from '../FormFields/FormFields.helper';
8
+
9
+ import { PaymentMethodEnum, UserActionEnum } from '../../enums/general';
10
+ import type {
11
+ OrderFormFieldType,
12
+ PaymentMethodSettingsType,
13
+ ContactRequestType,
14
+ OrderDenialFallbackOfferType,
15
+ } from '../../types/general';
16
+ import useOrderForm from '../../hooks/useOrderForm';
17
+ import { orderInvoiceContactFields } from '../../utils/order.helper';
18
+ import type { OrderFormInputsType, OrderInfoType } from '../../types/order';
19
+ import z from 'zod';
20
+ import { zodResolver } from '@hookform/resolvers/zod';
21
+ import { Field, FieldGroup, FieldLabel, FieldError } from '../ui/field';
22
+ import { Input } from '../ui/input';
23
+
24
+ type Props = {
25
+ callback: (url: string | null, orderInfo?: OrderInfoType | null) => void;
26
+ onBack: () => void;
27
+ submitStartCallback?: (id: string) => void;
28
+ userActionCallback?: (
29
+ action: UserActionEnum,
30
+ args: object | null | undefined
31
+ ) => void;
32
+ setContactCallback?: (contactInfo: ContactRequestType) => void;
33
+ className?: string;
34
+ backButtonText?: string;
35
+ verifyButtonText?: string;
36
+ orderDenialCloseButtonText?: string;
37
+ orderDenialContinueButtonText?: string;
38
+ organizationNumberLabel?: string;
39
+ organizationNumber?: string;
40
+ glnLabel?: string;
41
+ templatePackageId: string;
42
+ subscriberId?: string;
43
+ userId?: string;
44
+ identityProviderId?: string;
45
+ organizationId: string;
46
+ redirectUrl?: string;
47
+ showIframe?: boolean;
48
+ language: string;
49
+ merchantAgreementUrl: string;
50
+ orderValues?: OrderFormInputsType;
51
+ paymentMethodsOptions?: PaymentMethodSettingsType;
52
+ invoiceAddressSelection?: {
53
+ enabled?: boolean;
54
+ label?: string;
55
+ fields?: OrderFormFieldType[];
56
+ paymentMethods?: PaymentMethodEnum[];
57
+ };
58
+ invoiceLookupNotFoundText?: string;
59
+ errorReqMsg?: string;
60
+ errorValidationTitleMsg?: string;
61
+ errorValidationDenialOrderBlockingMsg?: string;
62
+ errorValidationBlockingOffersMsg?: string;
63
+ orderDenialOfferBaseText?: string;
64
+ orderDenialOfferWithFallbackText?: string;
65
+ orderDenialAmountText?: string;
66
+ fetchDenialFallbackOffer?: (
67
+ organizationId: string
68
+ ) => Promise<OrderDenialFallbackOfferType | undefined>;
69
+ };
70
+
71
+ type OIOFormInputsType = {
72
+ cvr: string;
73
+ gln: string;
74
+ };
75
+
76
+ const initialData = {
77
+ cvr: '',
78
+ gln: '',
79
+ };
80
+
81
+ const buildFormSchema = (errorReqMsg?: string) =>
82
+ z
83
+ .object({
84
+ cvr: z.string().trim(),
85
+ gln: z.string().trim(),
86
+ })
87
+ .superRefine((data, ctx) => {
88
+ if (!data.cvr && !data.gln) {
89
+ const message = errorReqMsg || 'This field is required!';
90
+ ctx.addIssue({
91
+ code: z.ZodIssueCode.custom,
92
+ message,
93
+ path: ['cvr'],
94
+ });
95
+ ctx.addIssue({
96
+ code: z.ZodIssueCode.custom,
97
+ message,
98
+ path: ['gln'],
99
+ });
100
+ }
101
+ });
102
+
103
+ const OIOForm: FC<Props> = ({
104
+ callback,
105
+ onBack,
106
+ submitStartCallback,
107
+ userActionCallback,
108
+ setContactCallback,
109
+ className = '',
110
+ backButtonText = '',
111
+ verifyButtonText = '',
112
+ orderDenialCloseButtonText = 'Close',
113
+ orderDenialContinueButtonText = 'Continue',
114
+ organizationNumberLabel = '',
115
+ organizationNumber = '',
116
+ glnLabel = '',
117
+ templatePackageId,
118
+ subscriberId,
119
+ userId,
120
+ identityProviderId,
121
+ organizationId,
122
+ redirectUrl,
123
+ showIframe,
124
+ language,
125
+ merchantAgreementUrl,
126
+ orderValues,
127
+ paymentMethodsOptions,
128
+ invoiceAddressSelection,
129
+ invoiceLookupNotFoundText,
130
+ errorReqMsg,
131
+ errorValidationTitleMsg,
132
+ errorValidationDenialOrderBlockingMsg,
133
+ errorValidationBlockingOffersMsg,
134
+ orderDenialOfferBaseText,
135
+ orderDenialOfferWithFallbackText,
136
+ orderDenialAmountText,
137
+ fetchDenialFallbackOffer,
138
+ }) => {
139
+ const cvrId = useId();
140
+ const glnId = useId();
141
+
142
+ const formSchema = useMemo(
143
+ () => buildFormSchema(errorReqMsg),
144
+ [errorReqMsg]
145
+ );
146
+
147
+ const form = useForm<OIOFormInputsType>({
148
+ resolver: zodResolver(formSchema),
149
+ defaultValues: {
150
+ ...initialData,
151
+ cvr: organizationNumber,
152
+ },
153
+ });
154
+
155
+ const { handleSubmit } = form;
156
+
157
+ const invoiceOrderFields =
158
+ invoiceAddressSelection?.fields || orderInvoiceContactFields;
159
+
160
+ const orderFields =
161
+ paymentMethodsOptions?.[orderValues?.paymentMethod as PaymentMethodEnum]
162
+ ?.orderFormFields ?? DEFAULT_ORDER_FORM_FIELDS;
163
+
164
+ const {
165
+ orderSubmit,
166
+ continueWithFallbackOffer,
167
+ loading,
168
+ apiErrorMsg,
169
+ errorsMsg,
170
+ orderDenialType,
171
+ orderDenialMessage,
172
+ orderDenialFallbackOffer,
173
+ dismissOrderDenial,
174
+ } = useOrderForm({
175
+ callback,
176
+ submitStartCallback,
177
+ userActionCallback,
178
+ setContactCallback,
179
+ organizationId,
180
+ subscriberId,
181
+ userId,
182
+ identityProviderId,
183
+ orderFields,
184
+ redirectUrl,
185
+ showIframe,
186
+ language,
187
+ merchantAgreementUrl,
188
+ paymentMethodsOptions,
189
+ templatePackageId,
190
+ invoiceAddressToggle: orderValues?.invoiceAddressSelection,
191
+ invoiceOrderFields,
192
+ invoiceLookupNotFoundText,
193
+ errorValidationTitleMsg,
194
+ errorValidationDenialOrderBlockingMsg,
195
+ errorValidationBlockingOffersMsg,
196
+ orderDenialOfferBaseText,
197
+ orderDenialOfferWithFallbackText,
198
+ orderDenialAmountText,
199
+ fetchDenialFallbackOffer,
200
+ });
201
+
202
+ const onSubmit: SubmitHandler<OIOFormInputsType> = async (
203
+ data
204
+ ): Promise<void> => {
205
+ await orderSubmit({ ...orderValues, gln: data.gln, cvr: data.cvr });
206
+ };
207
+
208
+ const handleBack = () => {
209
+ userActionCallback?.(UserActionEnum.RETURN_TO_MAIN, null);
210
+ onBack?.();
211
+ };
212
+
213
+ return (
214
+ <form
215
+ className={`${className}`}
216
+ noValidate
217
+ onSubmit={handleSubmit(onSubmit)}
218
+ data-testid="oio-form-id"
219
+ >
220
+ <FieldGroup>
221
+ <Controller
222
+ name="cvr"
223
+ control={form.control}
224
+ render={({ field, fieldState }) => (
225
+ <Field data-invalid={fieldState.invalid}>
226
+ <FieldLabel htmlFor={cvrId}>
227
+ {organizationNumberLabel}
228
+ </FieldLabel>
229
+ <Input
230
+ {...field}
231
+ id={cvrId}
232
+ aria-invalid={fieldState.invalid}
233
+ autoComplete="off"
234
+ />
235
+ {fieldState.error && (
236
+ <FieldError>
237
+ {fieldState.error.message}
238
+ </FieldError>
239
+ )}
240
+ </Field>
241
+ )}
242
+ />
243
+
244
+ <Controller
245
+ name="gln"
246
+ control={form.control}
247
+ render={({ field, fieldState }) => (
248
+ <Field data-invalid={fieldState.invalid}>
249
+ <FieldLabel htmlFor={glnId}>{glnLabel}</FieldLabel>
250
+ <Input
251
+ {...field}
252
+ id={glnId}
253
+ aria-invalid={fieldState.invalid}
254
+ autoComplete="off"
255
+ />
256
+ {fieldState.error && (
257
+ <FieldError>
258
+ {fieldState.error.message}
259
+ </FieldError>
260
+ )}
261
+ </Field>
262
+ )}
263
+ />
264
+ </FieldGroup>
265
+ <div className="flex justify-center flex-wrap gap-2">
266
+ <Button
267
+ type="button"
268
+ btnType="default"
269
+ disable={loading}
270
+ buttonText={backButtonText}
271
+ onClick={handleBack}
272
+ />
273
+
274
+ <Button
275
+ type="submit"
276
+ loading={loading}
277
+ buttonText={verifyButtonText}
278
+ />
279
+ </div>
280
+ {apiErrorMsg && <Alert className="mt-2" msg={apiErrorMsg} />}
281
+ {errorsMsg?.length > 0 &&
282
+ errorsMsg.map((i, index) => (
283
+ <Alert key={`${i}-${index}`} className="mt-2" msg={i} />
284
+ ))}
285
+ <OrderDenialModal
286
+ isOpen={!!orderDenialType}
287
+ message={orderDenialMessage}
288
+ closeButtonText={orderDenialCloseButtonText}
289
+ continueButtonText={orderDenialContinueButtonText}
290
+ canContinue={
291
+ orderDenialType === 'offer' &&
292
+ !!orderDenialFallbackOffer?.templatePackageId
293
+ }
294
+ offer={
295
+ orderDenialType === 'offer' &&
296
+ !!orderDenialFallbackOffer?.templatePackageId
297
+ ? orderDenialFallbackOffer
298
+ : undefined
299
+ }
300
+ onClose={dismissOrderDenial}
301
+ onContinue={continueWithFallbackOffer}
302
+ />
303
+ </form>
304
+ );
305
+ };
306
+
307
+ export default OIOForm;