quickbooks-medusa-plugin 0.0.7 → 0.0.9

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.
@@ -1,1832 +1,5 @@
1
- import { jsx, jsxs } from "react/jsx-runtime";
2
- import { defineRouteConfig } from "@medusajs/admin-sdk";
3
- import { toast, Text, Container, Heading, Badge, Button, Label, Input, Switch } from "@medusajs/ui";
4
- import { CurrencyDollar } from "@medusajs/icons";
5
- import React, { useState, useEffect } from "react";
6
- var isCheckBoxInput = (element) => element.type === "checkbox";
7
- var isDateObject = (value) => value instanceof Date;
8
- var isNullOrUndefined = (value) => value == null;
9
- const isObjectType = (value) => typeof value === "object";
10
- var isObject = (value) => !isNullOrUndefined(value) && !Array.isArray(value) && isObjectType(value) && !isDateObject(value);
11
- var getEventValue = (event) => isObject(event) && event.target ? isCheckBoxInput(event.target) ? event.target.checked : event.target.value : event;
12
- var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
13
- var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
14
- var isPlainObject = (tempObject) => {
15
- const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
16
- return isObject(prototypeCopy) && prototypeCopy.hasOwnProperty("isPrototypeOf");
17
- };
18
- var isWeb = typeof window !== "undefined" && typeof window.HTMLElement !== "undefined" && typeof document !== "undefined";
19
- function cloneObject(data) {
20
- let copy;
21
- const isArray = Array.isArray(data);
22
- if (data instanceof Date) {
23
- copy = new Date(data);
24
- } else if (data instanceof Set) {
25
- copy = new Set(data);
26
- } else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) && (isArray || isObject(data))) {
27
- copy = isArray ? [] : {};
28
- if (!isArray && !isPlainObject(data)) {
29
- copy = data;
30
- } else {
31
- for (const key in data) {
32
- if (data.hasOwnProperty(key)) {
33
- copy[key] = cloneObject(data[key]);
34
- }
35
- }
36
- }
37
- } else {
38
- return data;
39
- }
40
- return copy;
41
- }
42
- var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
43
- var isUndefined = (val) => val === void 0;
44
- var get = (object, path, defaultValue) => {
45
- if (!path || !isObject(object)) {
46
- return defaultValue;
47
- }
48
- const result = compact(path.split(/[,[\].]+?/)).reduce((result2, key) => isNullOrUndefined(result2) ? result2 : result2[key], object);
49
- return isUndefined(result) || result === object ? isUndefined(object[path]) ? defaultValue : object[path] : result;
50
- };
51
- var isBoolean = (value) => typeof value === "boolean";
52
- const EVENTS = {
53
- BLUR: "blur",
54
- FOCUS_OUT: "focusout",
55
- CHANGE: "change"
56
- };
57
- const VALIDATION_MODE = {
58
- onBlur: "onBlur",
59
- onChange: "onChange",
60
- onSubmit: "onSubmit",
61
- onTouched: "onTouched",
62
- all: "all"
63
- };
64
- const INPUT_VALIDATION_RULES = {
65
- max: "max",
66
- min: "min",
67
- maxLength: "maxLength",
68
- minLength: "minLength",
69
- pattern: "pattern",
70
- required: "required",
71
- validate: "validate"
72
- };
73
- const HookFormContext = React.createContext(null);
74
- const useFormContext = () => React.useContext(HookFormContext);
75
- var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
76
- const result = {
77
- defaultValues: control._defaultValues
78
- };
79
- for (const key in formState) {
80
- Object.defineProperty(result, key, {
81
- get: () => {
82
- const _key = key;
83
- if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
84
- control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
85
- }
86
- localProxyFormState && (localProxyFormState[_key] = true);
87
- return formState[_key];
88
- }
89
- });
90
- }
91
- return result;
92
- };
93
- var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
94
- var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
95
- updateFormState(formStateData);
96
- const { name, ...formState } = formStateData;
97
- return isEmptyObject(formState) || Object.keys(formState).length >= Object.keys(_proxyFormState).length || Object.keys(formState).find((key) => _proxyFormState[key] === (!isRoot || VALIDATION_MODE.all));
98
- };
99
- var convertToArrayPayload = (value) => Array.isArray(value) ? value : [value];
100
- var shouldSubscribeByName = (name, signalName, exact) => !name || !signalName || name === signalName || convertToArrayPayload(name).some((currentName) => currentName && (exact ? currentName === signalName : currentName.startsWith(signalName) || signalName.startsWith(currentName)));
101
- function useSubscribe(props) {
102
- const _props = React.useRef(props);
103
- _props.current = props;
104
- React.useEffect(() => {
105
- const subscription = !props.disabled && _props.current.subject && _props.current.subject.subscribe({
106
- next: _props.current.next
107
- });
108
- return () => {
109
- subscription && subscription.unsubscribe();
110
- };
111
- }, [props.disabled]);
112
- }
113
- function useFormState(props) {
114
- const methods = useFormContext();
115
- const { control = methods.control, disabled, name, exact } = props;
116
- const [formState, updateFormState] = React.useState(control._formState);
117
- const _mounted = React.useRef(true);
118
- const _localProxyFormState = React.useRef({
119
- isDirty: false,
120
- isLoading: false,
121
- dirtyFields: false,
122
- touchedFields: false,
123
- isValidating: false,
124
- isValid: false,
125
- errors: false
126
- });
127
- const _name = React.useRef(name);
128
- _name.current = name;
129
- useSubscribe({
130
- disabled,
131
- next: (value) => _mounted.current && shouldSubscribeByName(_name.current, value.name, exact) && shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) && updateFormState({
132
- ...control._formState,
133
- ...value
134
- }),
135
- subject: control._subjects.state
136
- });
137
- React.useEffect(() => {
138
- _mounted.current = true;
139
- _localProxyFormState.current.isValid && control._updateValid(true);
140
- return () => {
141
- _mounted.current = false;
142
- };
143
- }, [control]);
144
- return getProxyFormState(formState, control, _localProxyFormState.current, false);
145
- }
146
- var isString = (value) => typeof value === "string";
147
- var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
148
- if (isString(names)) {
149
- isGlobal && _names.watch.add(names);
150
- return get(formValues, names, defaultValue);
151
- }
152
- if (Array.isArray(names)) {
153
- return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
154
- }
155
- isGlobal && (_names.watchAll = true);
156
- return formValues;
157
- };
158
- function useWatch(props) {
159
- const methods = useFormContext();
160
- const { control = methods.control, name, defaultValue, disabled, exact } = props;
161
- const _name = React.useRef(name);
162
- _name.current = name;
163
- useSubscribe({
164
- disabled,
165
- subject: control._subjects.values,
166
- next: (formState) => {
167
- if (shouldSubscribeByName(_name.current, formState.name, exact)) {
168
- updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
169
- }
170
- }
171
- });
172
- const [value, updateValue] = React.useState(control._getWatch(name, defaultValue));
173
- React.useEffect(() => control._removeUnmounted());
174
- return value;
175
- }
176
- var isKey = (value) => /^\w*$/.test(value);
177
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, "").split(/\.|\[/));
178
- var set = (object, path, value) => {
179
- let index = -1;
180
- const tempPath = isKey(path) ? [path] : stringToPath(path);
181
- const length = tempPath.length;
182
- const lastIndex = length - 1;
183
- while (++index < length) {
184
- const key = tempPath[index];
185
- let newValue = value;
186
- if (index !== lastIndex) {
187
- const objValue = object[key];
188
- newValue = isObject(objValue) || Array.isArray(objValue) ? objValue : !isNaN(+tempPath[index + 1]) ? [] : {};
189
- }
190
- object[key] = newValue;
191
- object = object[key];
192
- }
193
- return object;
194
- };
195
- function useController(props) {
196
- const methods = useFormContext();
197
- const { name, disabled, control = methods.control, shouldUnregister } = props;
198
- const isArrayField = isNameInFieldArray(control._names.array, name);
199
- const value = useWatch({
200
- control,
201
- name,
202
- defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
203
- exact: true
204
- });
205
- const formState = useFormState({
206
- control,
207
- name
208
- });
209
- const _registerProps = React.useRef(control.register(name, {
210
- ...props.rules,
211
- value,
212
- ...isBoolean(props.disabled) ? { disabled: props.disabled } : {}
213
- }));
214
- React.useEffect(() => {
215
- const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
216
- const updateMounted = (name2, value2) => {
217
- const field = get(control._fields, name2);
218
- if (field) {
219
- field._f.mount = value2;
220
- }
221
- };
222
- updateMounted(name, true);
223
- if (_shouldUnregisterField) {
224
- const value2 = cloneObject(get(control._options.defaultValues, name));
225
- set(control._defaultValues, name, value2);
226
- if (isUndefined(get(control._formValues, name))) {
227
- set(control._formValues, name, value2);
228
- }
229
- }
230
- return () => {
231
- (isArrayField ? _shouldUnregisterField && !control._state.action : _shouldUnregisterField) ? control.unregister(name) : updateMounted(name, false);
232
- };
233
- }, [name, control, isArrayField, shouldUnregister]);
234
- React.useEffect(() => {
235
- if (get(control._fields, name)) {
236
- control._updateDisabledField({
237
- disabled,
238
- fields: control._fields,
239
- name,
240
- value: get(control._fields, name)._f.value
241
- });
242
- }
243
- }, [disabled, name, control]);
244
- return {
245
- field: {
246
- name,
247
- value,
248
- ...isBoolean(disabled) || isBoolean(formState.disabled) ? { disabled: formState.disabled || disabled } : {},
249
- onChange: React.useCallback((event) => _registerProps.current.onChange({
250
- target: {
251
- value: getEventValue(event),
252
- name
253
- },
254
- type: EVENTS.CHANGE
255
- }), [name]),
256
- onBlur: React.useCallback(() => _registerProps.current.onBlur({
257
- target: {
258
- value: get(control._formValues, name),
259
- name
260
- },
261
- type: EVENTS.BLUR
262
- }), [name, control]),
263
- ref: (elm) => {
264
- const field = get(control._fields, name);
265
- if (field && elm) {
266
- field._f.ref = {
267
- focus: () => elm.focus(),
268
- select: () => elm.select(),
269
- setCustomValidity: (message) => elm.setCustomValidity(message),
270
- reportValidity: () => elm.reportValidity()
271
- };
272
- }
273
- }
274
- },
275
- formState,
276
- fieldState: Object.defineProperties({}, {
277
- invalid: {
278
- enumerable: true,
279
- get: () => !!get(formState.errors, name)
280
- },
281
- isDirty: {
282
- enumerable: true,
283
- get: () => !!get(formState.dirtyFields, name)
284
- },
285
- isTouched: {
286
- enumerable: true,
287
- get: () => !!get(formState.touchedFields, name)
288
- },
289
- error: {
290
- enumerable: true,
291
- get: () => get(formState.errors, name)
292
- }
293
- })
294
- };
295
- }
296
- const Controller = (props) => props.render(useController(props));
297
- var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria ? {
298
- ...errors[name],
299
- types: {
300
- ...errors[name] && errors[name].types ? errors[name].types : {},
301
- [type]: message || true
302
- }
303
- } : {};
304
- var getValidationModes = (mode) => ({
305
- isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
306
- isOnBlur: mode === VALIDATION_MODE.onBlur,
307
- isOnChange: mode === VALIDATION_MODE.onChange,
308
- isOnAll: mode === VALIDATION_MODE.all,
309
- isOnTouch: mode === VALIDATION_MODE.onTouched
310
- });
311
- var isWatched = (name, _names, isBlurEvent) => !isBlurEvent && (_names.watchAll || _names.watch.has(name) || [..._names.watch].some((watchName) => name.startsWith(watchName) && /^\.\w+/.test(name.slice(watchName.length))));
312
- const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
313
- for (const key of fieldsNames || Object.keys(fields)) {
314
- const field = get(fields, key);
315
- if (field) {
316
- const { _f, ...currentField } = field;
317
- if (_f) {
318
- if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
319
- break;
320
- } else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
321
- break;
322
- } else {
323
- iterateFieldsByAction(currentField, action);
324
- }
325
- } else if (isObject(currentField)) {
326
- iterateFieldsByAction(currentField, action);
327
- }
328
- }
329
- }
330
- };
331
- var updateFieldArrayRootError = (errors, error, name) => {
332
- const fieldArrayErrors = compact(get(errors, name));
333
- set(fieldArrayErrors, "root", error[name]);
334
- set(errors, name, fieldArrayErrors);
335
- return errors;
336
- };
337
- var isFileInput = (element) => element.type === "file";
338
- var isFunction = (value) => typeof value === "function";
339
- var isHTMLElement = (value) => {
340
- if (!isWeb) {
341
- return false;
342
- }
343
- const owner = value ? value.ownerDocument : 0;
344
- return value instanceof (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement);
345
- };
346
- var isMessage = (value) => isString(value);
347
- var isRadioInput = (element) => element.type === "radio";
348
- var isRegex = (value) => value instanceof RegExp;
349
- const defaultResult = {
350
- value: false,
351
- isValid: false
352
- };
353
- const validResult = { value: true, isValid: true };
354
- var getCheckboxValue = (options) => {
355
- if (Array.isArray(options)) {
356
- if (options.length > 1) {
357
- const values = options.filter((option) => option && option.checked && !option.disabled).map((option) => option.value);
358
- return { value: values, isValid: !!values.length };
359
- }
360
- return options[0].checked && !options[0].disabled ? (
361
- // @ts-expect-error expected to work in the browser
362
- options[0].attributes && !isUndefined(options[0].attributes.value) ? isUndefined(options[0].value) || options[0].value === "" ? validResult : { value: options[0].value, isValid: true } : validResult
363
- ) : defaultResult;
364
- }
365
- return defaultResult;
366
- };
367
- const defaultReturn = {
368
- isValid: false,
369
- value: null
370
- };
371
- var getRadioValue = (options) => Array.isArray(options) ? options.reduce((previous, option) => option && option.checked && !option.disabled ? {
372
- isValid: true,
373
- value: option.value
374
- } : previous, defaultReturn) : defaultReturn;
375
- function getValidateError(result, ref, type = "validate") {
376
- if (isMessage(result) || Array.isArray(result) && result.every(isMessage) || isBoolean(result) && !result) {
377
- return {
378
- type,
379
- message: isMessage(result) ? result : "",
380
- ref
381
- };
382
- }
383
- }
384
- var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData) ? validationData : {
385
- value: validationData,
386
- message: ""
387
- };
388
- var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
389
- const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled } = field._f;
390
- const inputValue = get(formValues, name);
391
- if (!mount || disabled) {
392
- return {};
393
- }
394
- const inputRef = refs ? refs[0] : ref;
395
- const setCustomValidity = (message) => {
396
- if (shouldUseNativeValidation && inputRef.reportValidity) {
397
- inputRef.setCustomValidity(isBoolean(message) ? "" : message || "");
398
- inputRef.reportValidity();
399
- }
400
- };
401
- const error = {};
402
- const isRadio = isRadioInput(ref);
403
- const isCheckBox = isCheckBoxInput(ref);
404
- const isRadioOrCheckbox2 = isRadio || isCheckBox;
405
- const isEmpty = (valueAsNumber || isFileInput(ref)) && isUndefined(ref.value) && isUndefined(inputValue) || isHTMLElement(ref) && ref.value === "" || inputValue === "" || Array.isArray(inputValue) && !inputValue.length;
406
- const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
407
- const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
408
- const message = exceedMax ? maxLengthMessage : minLengthMessage;
409
- error[name] = {
410
- type: exceedMax ? maxType : minType,
411
- message,
412
- ref,
413
- ...appendErrorsCurry(exceedMax ? maxType : minType, message)
414
- };
415
- };
416
- if (isFieldArray ? !Array.isArray(inputValue) || !inputValue.length : required && (!isRadioOrCheckbox2 && (isEmpty || isNullOrUndefined(inputValue)) || isBoolean(inputValue) && !inputValue || isCheckBox && !getCheckboxValue(refs).isValid || isRadio && !getRadioValue(refs).isValid)) {
417
- const { value, message } = isMessage(required) ? { value: !!required, message: required } : getValueAndMessage(required);
418
- if (value) {
419
- error[name] = {
420
- type: INPUT_VALIDATION_RULES.required,
421
- message,
422
- ref: inputRef,
423
- ...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message)
424
- };
425
- if (!validateAllFieldCriteria) {
426
- setCustomValidity(message);
427
- return error;
428
- }
429
- }
430
- }
431
- if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
432
- let exceedMax;
433
- let exceedMin;
434
- const maxOutput = getValueAndMessage(max);
435
- const minOutput = getValueAndMessage(min);
436
- if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
437
- const valueNumber = ref.valueAsNumber || (inputValue ? +inputValue : inputValue);
438
- if (!isNullOrUndefined(maxOutput.value)) {
439
- exceedMax = valueNumber > maxOutput.value;
440
- }
441
- if (!isNullOrUndefined(minOutput.value)) {
442
- exceedMin = valueNumber < minOutput.value;
443
- }
444
- } else {
445
- const valueDate = ref.valueAsDate || new Date(inputValue);
446
- const convertTimeToDate = (time) => /* @__PURE__ */ new Date((/* @__PURE__ */ new Date()).toDateString() + " " + time);
447
- const isTime = ref.type == "time";
448
- const isWeek = ref.type == "week";
449
- if (isString(maxOutput.value) && inputValue) {
450
- exceedMax = isTime ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value) : isWeek ? inputValue > maxOutput.value : valueDate > new Date(maxOutput.value);
451
- }
452
- if (isString(minOutput.value) && inputValue) {
453
- exceedMin = isTime ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value) : isWeek ? inputValue < minOutput.value : valueDate < new Date(minOutput.value);
454
- }
455
- }
456
- if (exceedMax || exceedMin) {
457
- getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
458
- if (!validateAllFieldCriteria) {
459
- setCustomValidity(error[name].message);
460
- return error;
461
- }
462
- }
463
- }
464
- if ((maxLength || minLength) && !isEmpty && (isString(inputValue) || isFieldArray && Array.isArray(inputValue))) {
465
- const maxLengthOutput = getValueAndMessage(maxLength);
466
- const minLengthOutput = getValueAndMessage(minLength);
467
- const exceedMax = !isNullOrUndefined(maxLengthOutput.value) && inputValue.length > +maxLengthOutput.value;
468
- const exceedMin = !isNullOrUndefined(minLengthOutput.value) && inputValue.length < +minLengthOutput.value;
469
- if (exceedMax || exceedMin) {
470
- getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
471
- if (!validateAllFieldCriteria) {
472
- setCustomValidity(error[name].message);
473
- return error;
474
- }
475
- }
476
- }
477
- if (pattern && !isEmpty && isString(inputValue)) {
478
- const { value: patternValue, message } = getValueAndMessage(pattern);
479
- if (isRegex(patternValue) && !inputValue.match(patternValue)) {
480
- error[name] = {
481
- type: INPUT_VALIDATION_RULES.pattern,
482
- message,
483
- ref,
484
- ...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message)
485
- };
486
- if (!validateAllFieldCriteria) {
487
- setCustomValidity(message);
488
- return error;
489
- }
490
- }
491
- }
492
- if (validate) {
493
- if (isFunction(validate)) {
494
- const result = await validate(inputValue, formValues);
495
- const validateError = getValidateError(result, inputRef);
496
- if (validateError) {
497
- error[name] = {
498
- ...validateError,
499
- ...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message)
500
- };
501
- if (!validateAllFieldCriteria) {
502
- setCustomValidity(validateError.message);
503
- return error;
504
- }
505
- }
506
- } else if (isObject(validate)) {
507
- let validationResult = {};
508
- for (const key in validate) {
509
- if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
510
- break;
511
- }
512
- const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
513
- if (validateError) {
514
- validationResult = {
515
- ...validateError,
516
- ...appendErrorsCurry(key, validateError.message)
517
- };
518
- setCustomValidity(validateError.message);
519
- if (validateAllFieldCriteria) {
520
- error[name] = validationResult;
521
- }
522
- }
523
- }
524
- if (!isEmptyObject(validationResult)) {
525
- error[name] = {
526
- ref: inputRef,
527
- ...validationResult
528
- };
529
- if (!validateAllFieldCriteria) {
530
- return error;
531
- }
532
- }
533
- }
534
- }
535
- setCustomValidity(true);
536
- return error;
537
- };
538
- function baseGet(object, updatePath) {
539
- const length = updatePath.slice(0, -1).length;
540
- let index = 0;
541
- while (index < length) {
542
- object = isUndefined(object) ? index++ : object[updatePath[index++]];
543
- }
544
- return object;
545
- }
546
- function isEmptyArray(obj) {
547
- for (const key in obj) {
548
- if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
549
- return false;
550
- }
551
- }
552
- return true;
553
- }
554
- function unset(object, path) {
555
- const paths = Array.isArray(path) ? path : isKey(path) ? [path] : stringToPath(path);
556
- const childObject = paths.length === 1 ? object : baseGet(object, paths);
557
- const index = paths.length - 1;
558
- const key = paths[index];
559
- if (childObject) {
560
- delete childObject[key];
561
- }
562
- if (index !== 0 && (isObject(childObject) && isEmptyObject(childObject) || Array.isArray(childObject) && isEmptyArray(childObject))) {
563
- unset(object, paths.slice(0, -1));
564
- }
565
- return object;
566
- }
567
- var createSubject = () => {
568
- let _observers = [];
569
- const next = (value) => {
570
- for (const observer of _observers) {
571
- observer.next && observer.next(value);
572
- }
573
- };
574
- const subscribe = (observer) => {
575
- _observers.push(observer);
576
- return {
577
- unsubscribe: () => {
578
- _observers = _observers.filter((o) => o !== observer);
579
- }
580
- };
581
- };
582
- const unsubscribe = () => {
583
- _observers = [];
584
- };
585
- return {
586
- get observers() {
587
- return _observers;
588
- },
589
- next,
590
- subscribe,
591
- unsubscribe
592
- };
593
- };
594
- var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
595
- function deepEqual(object1, object2) {
596
- if (isPrimitive(object1) || isPrimitive(object2)) {
597
- return object1 === object2;
598
- }
599
- if (isDateObject(object1) && isDateObject(object2)) {
600
- return object1.getTime() === object2.getTime();
601
- }
602
- const keys1 = Object.keys(object1);
603
- const keys2 = Object.keys(object2);
604
- if (keys1.length !== keys2.length) {
605
- return false;
606
- }
607
- for (const key of keys1) {
608
- const val1 = object1[key];
609
- if (!keys2.includes(key)) {
610
- return false;
611
- }
612
- if (key !== "ref") {
613
- const val2 = object2[key];
614
- if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2) : val1 !== val2) {
615
- return false;
616
- }
617
- }
618
- }
619
- return true;
620
- }
621
- var isMultipleSelect = (element) => element.type === `select-multiple`;
622
- var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
623
- var live = (ref) => isHTMLElement(ref) && ref.isConnected;
624
- var objectHasFunction = (data) => {
625
- for (const key in data) {
626
- if (isFunction(data[key])) {
627
- return true;
628
- }
629
- }
630
- return false;
631
- };
632
- function markFieldsDirty(data, fields = {}) {
633
- const isParentNodeArray = Array.isArray(data);
634
- if (isObject(data) || isParentNodeArray) {
635
- for (const key in data) {
636
- if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
637
- fields[key] = Array.isArray(data[key]) ? [] : {};
638
- markFieldsDirty(data[key], fields[key]);
639
- } else if (!isNullOrUndefined(data[key])) {
640
- fields[key] = true;
641
- }
642
- }
643
- }
644
- return fields;
645
- }
646
- function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
647
- const isParentNodeArray = Array.isArray(data);
648
- if (isObject(data) || isParentNodeArray) {
649
- for (const key in data) {
650
- if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
651
- if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
652
- dirtyFieldsFromValues[key] = Array.isArray(data[key]) ? markFieldsDirty(data[key], []) : { ...markFieldsDirty(data[key]) };
653
- } else {
654
- getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
655
- }
656
- } else {
657
- dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
658
- }
659
- }
660
- }
661
- return dirtyFieldsFromValues;
662
- }
663
- var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
664
- var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value) ? value : valueAsNumber ? value === "" ? NaN : value ? +value : value : valueAsDate && isString(value) ? new Date(value) : setValueAs ? setValueAs(value) : value;
665
- function getFieldValue(_f) {
666
- const ref = _f.ref;
667
- if (_f.refs ? _f.refs.every((ref2) => ref2.disabled) : ref.disabled) {
668
- return;
669
- }
670
- if (isFileInput(ref)) {
671
- return ref.files;
672
- }
673
- if (isRadioInput(ref)) {
674
- return getRadioValue(_f.refs).value;
675
- }
676
- if (isMultipleSelect(ref)) {
677
- return [...ref.selectedOptions].map(({ value }) => value);
678
- }
679
- if (isCheckBoxInput(ref)) {
680
- return getCheckboxValue(_f.refs).value;
681
- }
682
- return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
683
- }
684
- var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
685
- const fields = {};
686
- for (const name of fieldsNames) {
687
- const field = get(_fields, name);
688
- field && set(fields, name, field._f);
689
- }
690
- return {
691
- criteriaMode,
692
- names: [...fieldsNames],
693
- fields,
694
- shouldUseNativeValidation
695
- };
696
- };
697
- var getRuleValue = (rule) => isUndefined(rule) ? rule : isRegex(rule) ? rule.source : isObject(rule) ? isRegex(rule.value) ? rule.value.source : rule.value : rule;
698
- var hasValidation = (options) => options.mount && (options.required || options.min || options.max || options.maxLength || options.minLength || options.pattern || options.validate);
699
- function schemaErrorLookup(errors, _fields, name) {
700
- const error = get(errors, name);
701
- if (error || isKey(name)) {
702
- return {
703
- error,
704
- name
705
- };
706
- }
707
- const names = name.split(".");
708
- while (names.length) {
709
- const fieldName = names.join(".");
710
- const field = get(_fields, fieldName);
711
- const foundError = get(errors, fieldName);
712
- if (field && !Array.isArray(field) && name !== fieldName) {
713
- return { name };
714
- }
715
- if (foundError && foundError.type) {
716
- return {
717
- name: fieldName,
718
- error: foundError
719
- };
720
- }
721
- names.pop();
722
- }
723
- return {
724
- name
725
- };
726
- }
727
- var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
728
- if (mode.isOnAll) {
729
- return false;
730
- } else if (!isSubmitted && mode.isOnTouch) {
731
- return !(isTouched || isBlurEvent);
732
- } else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
733
- return !isBlurEvent;
734
- } else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
735
- return isBlurEvent;
736
- }
737
- return true;
738
- };
739
- var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
740
- const defaultOptions = {
741
- mode: VALIDATION_MODE.onSubmit,
742
- reValidateMode: VALIDATION_MODE.onChange,
743
- shouldFocusError: true
744
- };
745
- function createFormControl(props = {}, flushRootRender) {
746
- let _options = {
747
- ...defaultOptions,
748
- ...props
749
- };
750
- let _formState = {
751
- submitCount: 0,
752
- isDirty: false,
753
- isLoading: isFunction(_options.defaultValues),
754
- isValidating: false,
755
- isSubmitted: false,
756
- isSubmitting: false,
757
- isSubmitSuccessful: false,
758
- isValid: false,
759
- touchedFields: {},
760
- dirtyFields: {},
761
- errors: _options.errors || {},
762
- disabled: false
763
- };
764
- let _fields = {};
765
- let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values) ? cloneObject(_options.defaultValues || _options.values) || {} : {};
766
- let _formValues = _options.shouldUnregister ? {} : cloneObject(_defaultValues);
767
- let _state = {
768
- action: false,
769
- mount: false,
770
- watch: false
771
- };
772
- let _names = {
773
- mount: /* @__PURE__ */ new Set(),
774
- unMount: /* @__PURE__ */ new Set(),
775
- array: /* @__PURE__ */ new Set(),
776
- watch: /* @__PURE__ */ new Set()
777
- };
778
- let delayErrorCallback;
779
- let timer = 0;
780
- const _proxyFormState = {
781
- isDirty: false,
782
- dirtyFields: false,
783
- touchedFields: false,
784
- isValidating: false,
785
- isValid: false,
786
- errors: false
787
- };
788
- const _subjects = {
789
- values: createSubject(),
790
- array: createSubject(),
791
- state: createSubject()
792
- };
793
- const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
794
- const validationModeBeforeSubmit = getValidationModes(_options.mode);
795
- const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
796
- const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
797
- const debounce = (callback) => (wait) => {
798
- clearTimeout(timer);
799
- timer = setTimeout(callback, wait);
800
- };
801
- const _updateValid = async (shouldUpdateValid) => {
802
- if (_proxyFormState.isValid || shouldUpdateValid) {
803
- const isValid = _options.resolver ? isEmptyObject((await _executeSchema()).errors) : await executeBuiltInValidation(_fields, true);
804
- if (isValid !== _formState.isValid) {
805
- _subjects.state.next({
806
- isValid
807
- });
808
- }
809
- }
810
- };
811
- const _updateIsValidating = (value) => _proxyFormState.isValidating && _subjects.state.next({
812
- isValidating: value
813
- });
814
- const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
815
- if (args && method) {
816
- _state.action = true;
817
- if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
818
- const fieldValues = method(get(_fields, name), args.argA, args.argB);
819
- shouldSetValues && set(_fields, name, fieldValues);
820
- }
821
- if (shouldUpdateFieldsAndState && Array.isArray(get(_formState.errors, name))) {
822
- const errors = method(get(_formState.errors, name), args.argA, args.argB);
823
- shouldSetValues && set(_formState.errors, name, errors);
824
- unsetEmptyArray(_formState.errors, name);
825
- }
826
- if (_proxyFormState.touchedFields && shouldUpdateFieldsAndState && Array.isArray(get(_formState.touchedFields, name))) {
827
- const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
828
- shouldSetValues && set(_formState.touchedFields, name, touchedFields);
829
- }
830
- if (_proxyFormState.dirtyFields) {
831
- _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
832
- }
833
- _subjects.state.next({
834
- name,
835
- isDirty: _getDirty(name, values),
836
- dirtyFields: _formState.dirtyFields,
837
- errors: _formState.errors,
838
- isValid: _formState.isValid
839
- });
840
- } else {
841
- set(_formValues, name, values);
842
- }
843
- };
844
- const updateErrors = (name, error) => {
845
- set(_formState.errors, name, error);
846
- _subjects.state.next({
847
- errors: _formState.errors
848
- });
849
- };
850
- const _setErrors = (errors) => {
851
- _formState.errors = errors;
852
- _subjects.state.next({
853
- errors: _formState.errors,
854
- isValid: false
855
- });
856
- };
857
- const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
858
- const field = get(_fields, name);
859
- if (field) {
860
- const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
861
- isUndefined(defaultValue) || ref && ref.defaultChecked || shouldSkipSetValueAs ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f)) : setFieldValue(name, defaultValue);
862
- _state.mount && _updateValid();
863
- }
864
- };
865
- const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
866
- let shouldUpdateField = false;
867
- let isPreviousDirty = false;
868
- const output = {
869
- name
870
- };
871
- const disabledField = get(_fields, name) && get(_fields, name)._f.disabled;
872
- if (!isBlurEvent || shouldDirty) {
873
- if (_proxyFormState.isDirty) {
874
- isPreviousDirty = _formState.isDirty;
875
- _formState.isDirty = output.isDirty = _getDirty();
876
- shouldUpdateField = isPreviousDirty !== output.isDirty;
877
- }
878
- const isCurrentFieldPristine = disabledField || deepEqual(get(_defaultValues, name), fieldValue);
879
- isPreviousDirty = !disabledField && get(_formState.dirtyFields, name);
880
- isCurrentFieldPristine || disabledField ? unset(_formState.dirtyFields, name) : set(_formState.dirtyFields, name, true);
881
- output.dirtyFields = _formState.dirtyFields;
882
- shouldUpdateField = shouldUpdateField || _proxyFormState.dirtyFields && isPreviousDirty !== !isCurrentFieldPristine;
883
- }
884
- if (isBlurEvent) {
885
- const isPreviousFieldTouched = get(_formState.touchedFields, name);
886
- if (!isPreviousFieldTouched) {
887
- set(_formState.touchedFields, name, isBlurEvent);
888
- output.touchedFields = _formState.touchedFields;
889
- shouldUpdateField = shouldUpdateField || _proxyFormState.touchedFields && isPreviousFieldTouched !== isBlurEvent;
890
- }
891
- }
892
- shouldUpdateField && shouldRender && _subjects.state.next(output);
893
- return shouldUpdateField ? output : {};
894
- };
895
- const shouldRenderByError = (name, isValid, error, fieldState) => {
896
- const previousFieldError = get(_formState.errors, name);
897
- const shouldUpdateValid = _proxyFormState.isValid && isBoolean(isValid) && _formState.isValid !== isValid;
898
- if (props.delayError && error) {
899
- delayErrorCallback = debounce(() => updateErrors(name, error));
900
- delayErrorCallback(props.delayError);
901
- } else {
902
- clearTimeout(timer);
903
- delayErrorCallback = null;
904
- error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
905
- }
906
- if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) || !isEmptyObject(fieldState) || shouldUpdateValid) {
907
- const updatedFormState = {
908
- ...fieldState,
909
- ...shouldUpdateValid && isBoolean(isValid) ? { isValid } : {},
910
- errors: _formState.errors,
911
- name
912
- };
913
- _formState = {
914
- ..._formState,
915
- ...updatedFormState
916
- };
917
- _subjects.state.next(updatedFormState);
918
- }
919
- _updateIsValidating(false);
920
- };
921
- const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
922
- const executeSchemaAndUpdateState = async (names) => {
923
- const { errors } = await _executeSchema(names);
924
- if (names) {
925
- for (const name of names) {
926
- const error = get(errors, name);
927
- error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
928
- }
929
- } else {
930
- _formState.errors = errors;
931
- }
932
- return errors;
933
- };
934
- const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
935
- valid: true
936
- }) => {
937
- for (const name in fields) {
938
- const field = fields[name];
939
- if (field) {
940
- const { _f, ...fieldValue } = field;
941
- if (_f) {
942
- const isFieldArrayRoot = _names.array.has(_f.name);
943
- const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
944
- if (fieldError[_f.name]) {
945
- context.valid = false;
946
- if (shouldOnlyCheckValid) {
947
- break;
948
- }
949
- }
950
- !shouldOnlyCheckValid && (get(fieldError, _f.name) ? isFieldArrayRoot ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name) : set(_formState.errors, _f.name, fieldError[_f.name]) : unset(_formState.errors, _f.name));
951
- }
952
- fieldValue && await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context);
953
- }
954
- }
955
- return context.valid;
956
- };
957
- const _removeUnmounted = () => {
958
- for (const name of _names.unMount) {
959
- const field = get(_fields, name);
960
- field && (field._f.refs ? field._f.refs.every((ref) => !live(ref)) : !live(field._f.ref)) && unregister(name);
961
- }
962
- _names.unMount = /* @__PURE__ */ new Set();
963
- };
964
- const _getDirty = (name, data) => (name && data && set(_formValues, name, data), !deepEqual(getValues(), _defaultValues));
965
- const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
966
- ..._state.mount ? _formValues : isUndefined(defaultValue) ? _defaultValues : isString(names) ? { [names]: defaultValue } : defaultValue
967
- }, isGlobal, defaultValue);
968
- const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
969
- const setFieldValue = (name, value, options = {}) => {
970
- const field = get(_fields, name);
971
- let fieldValue = value;
972
- if (field) {
973
- const fieldReference = field._f;
974
- if (fieldReference) {
975
- !fieldReference.disabled && set(_formValues, name, getFieldValueAs(value, fieldReference));
976
- fieldValue = isHTMLElement(fieldReference.ref) && isNullOrUndefined(value) ? "" : value;
977
- if (isMultipleSelect(fieldReference.ref)) {
978
- [...fieldReference.ref.options].forEach((optionRef) => optionRef.selected = fieldValue.includes(optionRef.value));
979
- } else if (fieldReference.refs) {
980
- if (isCheckBoxInput(fieldReference.ref)) {
981
- fieldReference.refs.length > 1 ? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) && (checkboxRef.checked = Array.isArray(fieldValue) ? !!fieldValue.find((data) => data === checkboxRef.value) : fieldValue === checkboxRef.value)) : fieldReference.refs[0] && (fieldReference.refs[0].checked = !!fieldValue);
982
- } else {
983
- fieldReference.refs.forEach((radioRef) => radioRef.checked = radioRef.value === fieldValue);
984
- }
985
- } else if (isFileInput(fieldReference.ref)) {
986
- fieldReference.ref.value = "";
987
- } else {
988
- fieldReference.ref.value = fieldValue;
989
- if (!fieldReference.ref.type) {
990
- _subjects.values.next({
991
- name,
992
- values: { ..._formValues }
993
- });
994
- }
995
- }
996
- }
997
- }
998
- (options.shouldDirty || options.shouldTouch) && updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
999
- options.shouldValidate && trigger(name);
1000
- };
1001
- const setValues = (name, value, options) => {
1002
- for (const fieldKey in value) {
1003
- const fieldValue = value[fieldKey];
1004
- const fieldName = `${name}.${fieldKey}`;
1005
- const field = get(_fields, fieldName);
1006
- (_names.array.has(name) || !isPrimitive(fieldValue) || field && !field._f) && !isDateObject(fieldValue) ? setValues(fieldName, fieldValue, options) : setFieldValue(fieldName, fieldValue, options);
1007
- }
1008
- };
1009
- const setValue = (name, value, options = {}) => {
1010
- const field = get(_fields, name);
1011
- const isFieldArray = _names.array.has(name);
1012
- const cloneValue = cloneObject(value);
1013
- set(_formValues, name, cloneValue);
1014
- if (isFieldArray) {
1015
- _subjects.array.next({
1016
- name,
1017
- values: { ..._formValues }
1018
- });
1019
- if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) && options.shouldDirty) {
1020
- _subjects.state.next({
1021
- name,
1022
- dirtyFields: getDirtyFields(_defaultValues, _formValues),
1023
- isDirty: _getDirty(name, cloneValue)
1024
- });
1025
- }
1026
- } else {
1027
- field && !field._f && !isNullOrUndefined(cloneValue) ? setValues(name, cloneValue, options) : setFieldValue(name, cloneValue, options);
1028
- }
1029
- isWatched(name, _names) && _subjects.state.next({ ..._formState });
1030
- _subjects.values.next({
1031
- name,
1032
- values: { ..._formValues }
1033
- });
1034
- !_state.mount && flushRootRender();
1035
- };
1036
- const onChange = async (event) => {
1037
- const target = event.target;
1038
- let name = target.name;
1039
- let isFieldValueUpdated = true;
1040
- const field = get(_fields, name);
1041
- const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
1042
- const _updateIsFieldValueUpdated = (fieldValue) => {
1043
- isFieldValueUpdated = Number.isNaN(fieldValue) || fieldValue === get(_formValues, name, fieldValue);
1044
- };
1045
- if (field) {
1046
- let error;
1047
- let isValid;
1048
- const fieldValue = getCurrentFieldValue();
1049
- const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
1050
- const shouldSkipValidation = !hasValidation(field._f) && !_options.resolver && !get(_formState.errors, name) && !field._f.deps || skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
1051
- const watched = isWatched(name, _names, isBlurEvent);
1052
- set(_formValues, name, fieldValue);
1053
- if (isBlurEvent) {
1054
- field._f.onBlur && field._f.onBlur(event);
1055
- delayErrorCallback && delayErrorCallback(0);
1056
- } else if (field._f.onChange) {
1057
- field._f.onChange(event);
1058
- }
1059
- const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
1060
- const shouldRender = !isEmptyObject(fieldState) || watched;
1061
- !isBlurEvent && _subjects.values.next({
1062
- name,
1063
- type: event.type,
1064
- values: { ..._formValues }
1065
- });
1066
- if (shouldSkipValidation) {
1067
- _proxyFormState.isValid && _updateValid();
1068
- return shouldRender && _subjects.state.next({ name, ...watched ? {} : fieldState });
1069
- }
1070
- !isBlurEvent && watched && _subjects.state.next({ ..._formState });
1071
- _updateIsValidating(true);
1072
- if (_options.resolver) {
1073
- const { errors } = await _executeSchema([name]);
1074
- _updateIsFieldValueUpdated(fieldValue);
1075
- if (isFieldValueUpdated) {
1076
- const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
1077
- const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
1078
- error = errorLookupResult.error;
1079
- name = errorLookupResult.name;
1080
- isValid = isEmptyObject(errors);
1081
- }
1082
- } else {
1083
- error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1084
- _updateIsFieldValueUpdated(fieldValue);
1085
- if (isFieldValueUpdated) {
1086
- if (error) {
1087
- isValid = false;
1088
- } else if (_proxyFormState.isValid) {
1089
- isValid = await executeBuiltInValidation(_fields, true);
1090
- }
1091
- }
1092
- }
1093
- if (isFieldValueUpdated) {
1094
- field._f.deps && trigger(field._f.deps);
1095
- shouldRenderByError(name, isValid, error, fieldState);
1096
- }
1097
- }
1098
- };
1099
- const _focusInput = (ref, key) => {
1100
- if (get(_formState.errors, key) && ref.focus) {
1101
- ref.focus();
1102
- return 1;
1103
- }
1104
- return;
1105
- };
1106
- const trigger = async (name, options = {}) => {
1107
- let isValid;
1108
- let validationResult;
1109
- const fieldNames = convertToArrayPayload(name);
1110
- _updateIsValidating(true);
1111
- if (_options.resolver) {
1112
- const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
1113
- isValid = isEmptyObject(errors);
1114
- validationResult = name ? !fieldNames.some((name2) => get(errors, name2)) : isValid;
1115
- } else if (name) {
1116
- validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
1117
- const field = get(_fields, fieldName);
1118
- return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
1119
- }))).every(Boolean);
1120
- !(!validationResult && !_formState.isValid) && _updateValid();
1121
- } else {
1122
- validationResult = isValid = await executeBuiltInValidation(_fields);
1123
- }
1124
- _subjects.state.next({
1125
- ...!isString(name) || _proxyFormState.isValid && isValid !== _formState.isValid ? {} : { name },
1126
- ..._options.resolver || !name ? { isValid } : {},
1127
- errors: _formState.errors,
1128
- isValidating: false
1129
- });
1130
- options.shouldFocus && !validationResult && iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
1131
- return validationResult;
1132
- };
1133
- const getValues = (fieldNames) => {
1134
- const values = {
1135
- ..._defaultValues,
1136
- ..._state.mount ? _formValues : {}
1137
- };
1138
- return isUndefined(fieldNames) ? values : isString(fieldNames) ? get(values, fieldNames) : fieldNames.map((name) => get(values, name));
1139
- };
1140
- const getFieldState = (name, formState) => ({
1141
- invalid: !!get((formState || _formState).errors, name),
1142
- isDirty: !!get((formState || _formState).dirtyFields, name),
1143
- isTouched: !!get((formState || _formState).touchedFields, name),
1144
- error: get((formState || _formState).errors, name)
1145
- });
1146
- const clearErrors = (name) => {
1147
- name && convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
1148
- _subjects.state.next({
1149
- errors: name ? _formState.errors : {}
1150
- });
1151
- };
1152
- const setError = (name, error, options) => {
1153
- const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
1154
- set(_formState.errors, name, {
1155
- ...error,
1156
- ref
1157
- });
1158
- _subjects.state.next({
1159
- name,
1160
- errors: _formState.errors,
1161
- isValid: false
1162
- });
1163
- options && options.shouldFocus && ref && ref.focus && ref.focus();
1164
- };
1165
- const watch = (name, defaultValue) => isFunction(name) ? _subjects.values.subscribe({
1166
- next: (payload) => name(_getWatch(void 0, defaultValue), payload)
1167
- }) : _getWatch(name, defaultValue, true);
1168
- const unregister = (name, options = {}) => {
1169
- for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
1170
- _names.mount.delete(fieldName);
1171
- _names.array.delete(fieldName);
1172
- if (!options.keepValue) {
1173
- unset(_fields, fieldName);
1174
- unset(_formValues, fieldName);
1175
- }
1176
- !options.keepError && unset(_formState.errors, fieldName);
1177
- !options.keepDirty && unset(_formState.dirtyFields, fieldName);
1178
- !options.keepTouched && unset(_formState.touchedFields, fieldName);
1179
- !_options.shouldUnregister && !options.keepDefaultValue && unset(_defaultValues, fieldName);
1180
- }
1181
- _subjects.values.next({
1182
- values: { ..._formValues }
1183
- });
1184
- _subjects.state.next({
1185
- ..._formState,
1186
- ...!options.keepDirty ? {} : { isDirty: _getDirty() }
1187
- });
1188
- !options.keepIsValid && _updateValid();
1189
- };
1190
- const _updateDisabledField = ({ disabled, name, field, fields, value }) => {
1191
- if (isBoolean(disabled)) {
1192
- const inputValue = disabled ? void 0 : isUndefined(value) ? getFieldValue(field ? field._f : get(fields, name)._f) : value;
1193
- set(_formValues, name, inputValue);
1194
- updateTouchAndDirty(name, inputValue, false, false, true);
1195
- }
1196
- };
1197
- const register = (name, options = {}) => {
1198
- let field = get(_fields, name);
1199
- const disabledIsDefined = isBoolean(options.disabled);
1200
- set(_fields, name, {
1201
- ...field || {},
1202
- _f: {
1203
- ...field && field._f ? field._f : { ref: { name } },
1204
- name,
1205
- mount: true,
1206
- ...options
1207
- }
1208
- });
1209
- _names.mount.add(name);
1210
- if (field) {
1211
- _updateDisabledField({
1212
- field,
1213
- disabled: options.disabled,
1214
- name
1215
- });
1216
- } else {
1217
- updateValidAndValue(name, true, options.value);
1218
- }
1219
- return {
1220
- ...disabledIsDefined ? { disabled: options.disabled } : {},
1221
- ..._options.progressive ? {
1222
- required: !!options.required,
1223
- min: getRuleValue(options.min),
1224
- max: getRuleValue(options.max),
1225
- minLength: getRuleValue(options.minLength),
1226
- maxLength: getRuleValue(options.maxLength),
1227
- pattern: getRuleValue(options.pattern)
1228
- } : {},
1229
- name,
1230
- onChange,
1231
- onBlur: onChange,
1232
- ref: (ref) => {
1233
- if (ref) {
1234
- register(name, options);
1235
- field = get(_fields, name);
1236
- const fieldRef = isUndefined(ref.value) ? ref.querySelectorAll ? ref.querySelectorAll("input,select,textarea")[0] || ref : ref : ref;
1237
- const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
1238
- const refs = field._f.refs || [];
1239
- if (radioOrCheckbox ? refs.find((option) => option === fieldRef) : fieldRef === field._f.ref) {
1240
- return;
1241
- }
1242
- set(_fields, name, {
1243
- _f: {
1244
- ...field._f,
1245
- ...radioOrCheckbox ? {
1246
- refs: [
1247
- ...refs.filter(live),
1248
- fieldRef,
1249
- ...Array.isArray(get(_defaultValues, name)) ? [{}] : []
1250
- ],
1251
- ref: { type: fieldRef.type, name }
1252
- } : { ref: fieldRef }
1253
- }
1254
- });
1255
- updateValidAndValue(name, false, void 0, fieldRef);
1256
- } else {
1257
- field = get(_fields, name, {});
1258
- if (field._f) {
1259
- field._f.mount = false;
1260
- }
1261
- (_options.shouldUnregister || options.shouldUnregister) && !(isNameInFieldArray(_names.array, name) && _state.action) && _names.unMount.add(name);
1262
- }
1263
- }
1264
- };
1265
- };
1266
- const _focusError = () => _options.shouldFocusError && iterateFieldsByAction(_fields, _focusInput, _names.mount);
1267
- const _disableForm = (disabled) => {
1268
- if (isBoolean(disabled)) {
1269
- _subjects.state.next({ disabled });
1270
- iterateFieldsByAction(_fields, (ref, name) => {
1271
- let requiredDisabledState = disabled;
1272
- const currentField = get(_fields, name);
1273
- if (currentField && isBoolean(currentField._f.disabled)) {
1274
- requiredDisabledState || (requiredDisabledState = currentField._f.disabled);
1275
- }
1276
- ref.disabled = requiredDisabledState;
1277
- }, 0, false);
1278
- }
1279
- };
1280
- const handleSubmit = (onValid, onInvalid) => async (e) => {
1281
- if (e) {
1282
- e.preventDefault && e.preventDefault();
1283
- e.persist && e.persist();
1284
- }
1285
- let fieldValues = cloneObject(_formValues);
1286
- _subjects.state.next({
1287
- isSubmitting: true
1288
- });
1289
- if (_options.resolver) {
1290
- const { errors, values } = await _executeSchema();
1291
- _formState.errors = errors;
1292
- fieldValues = values;
1293
- } else {
1294
- await executeBuiltInValidation(_fields);
1295
- }
1296
- unset(_formState.errors, "root");
1297
- if (isEmptyObject(_formState.errors)) {
1298
- _subjects.state.next({
1299
- errors: {}
1300
- });
1301
- await onValid(fieldValues, e);
1302
- } else {
1303
- if (onInvalid) {
1304
- await onInvalid({ ..._formState.errors }, e);
1305
- }
1306
- _focusError();
1307
- setTimeout(_focusError);
1308
- }
1309
- _subjects.state.next({
1310
- isSubmitted: true,
1311
- isSubmitting: false,
1312
- isSubmitSuccessful: isEmptyObject(_formState.errors),
1313
- submitCount: _formState.submitCount + 1,
1314
- errors: _formState.errors
1315
- });
1316
- };
1317
- const resetField = (name, options = {}) => {
1318
- if (get(_fields, name)) {
1319
- if (isUndefined(options.defaultValue)) {
1320
- setValue(name, get(_defaultValues, name));
1321
- } else {
1322
- setValue(name, options.defaultValue);
1323
- set(_defaultValues, name, options.defaultValue);
1324
- }
1325
- if (!options.keepTouched) {
1326
- unset(_formState.touchedFields, name);
1327
- }
1328
- if (!options.keepDirty) {
1329
- unset(_formState.dirtyFields, name);
1330
- _formState.isDirty = options.defaultValue ? _getDirty(name, get(_defaultValues, name)) : _getDirty();
1331
- }
1332
- if (!options.keepError) {
1333
- unset(_formState.errors, name);
1334
- _proxyFormState.isValid && _updateValid();
1335
- }
1336
- _subjects.state.next({ ..._formState });
1337
- }
1338
- };
1339
- const _reset = (formValues, keepStateOptions = {}) => {
1340
- const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
1341
- const cloneUpdatedValues = cloneObject(updatedValues);
1342
- const values = formValues && !isEmptyObject(formValues) ? cloneUpdatedValues : _defaultValues;
1343
- if (!keepStateOptions.keepDefaultValues) {
1344
- _defaultValues = updatedValues;
1345
- }
1346
- if (!keepStateOptions.keepValues) {
1347
- if (keepStateOptions.keepDirtyValues || shouldCaptureDirtyFields) {
1348
- for (const fieldName of _names.mount) {
1349
- get(_formState.dirtyFields, fieldName) ? set(values, fieldName, get(_formValues, fieldName)) : setValue(fieldName, get(values, fieldName));
1350
- }
1351
- } else {
1352
- if (isWeb && isUndefined(formValues)) {
1353
- for (const name of _names.mount) {
1354
- const field = get(_fields, name);
1355
- if (field && field._f) {
1356
- const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
1357
- if (isHTMLElement(fieldReference)) {
1358
- const form = fieldReference.closest("form");
1359
- if (form) {
1360
- form.reset();
1361
- break;
1362
- }
1363
- }
1364
- }
1365
- }
1366
- }
1367
- _fields = {};
1368
- }
1369
- _formValues = props.shouldUnregister ? keepStateOptions.keepDefaultValues ? cloneObject(_defaultValues) : {} : cloneObject(values);
1370
- _subjects.array.next({
1371
- values: { ...values }
1372
- });
1373
- _subjects.values.next({
1374
- values: { ...values }
1375
- });
1376
- }
1377
- _names = {
1378
- mount: /* @__PURE__ */ new Set(),
1379
- unMount: /* @__PURE__ */ new Set(),
1380
- array: /* @__PURE__ */ new Set(),
1381
- watch: /* @__PURE__ */ new Set(),
1382
- watchAll: false,
1383
- focus: ""
1384
- };
1385
- !_state.mount && flushRootRender();
1386
- _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
1387
- _state.watch = !!props.shouldUnregister;
1388
- _subjects.state.next({
1389
- submitCount: keepStateOptions.keepSubmitCount ? _formState.submitCount : 0,
1390
- isDirty: keepStateOptions.keepDirty ? _formState.isDirty : !!(keepStateOptions.keepDefaultValues && !deepEqual(formValues, _defaultValues)),
1391
- isSubmitted: keepStateOptions.keepIsSubmitted ? _formState.isSubmitted : false,
1392
- dirtyFields: keepStateOptions.keepDirtyValues ? _formState.dirtyFields : keepStateOptions.keepDefaultValues && formValues ? getDirtyFields(_defaultValues, formValues) : {},
1393
- touchedFields: keepStateOptions.keepTouched ? _formState.touchedFields : {},
1394
- errors: keepStateOptions.keepErrors ? _formState.errors : {},
1395
- isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful ? _formState.isSubmitSuccessful : false,
1396
- isSubmitting: false
1397
- });
1398
- };
1399
- const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues) ? formValues(_formValues) : formValues, keepStateOptions);
1400
- const setFocus = (name, options = {}) => {
1401
- const field = get(_fields, name);
1402
- const fieldReference = field && field._f;
1403
- if (fieldReference) {
1404
- const fieldRef = fieldReference.refs ? fieldReference.refs[0] : fieldReference.ref;
1405
- if (fieldRef.focus) {
1406
- fieldRef.focus();
1407
- options.shouldSelect && fieldRef.select();
1408
- }
1409
- }
1410
- };
1411
- const _updateFormState = (updatedFormState) => {
1412
- _formState = {
1413
- ..._formState,
1414
- ...updatedFormState
1415
- };
1416
- };
1417
- const _resetDefaultValues = () => isFunction(_options.defaultValues) && _options.defaultValues().then((values) => {
1418
- reset(values, _options.resetOptions);
1419
- _subjects.state.next({
1420
- isLoading: false
1421
- });
1422
- });
1423
- return {
1424
- control: {
1425
- register,
1426
- unregister,
1427
- getFieldState,
1428
- handleSubmit,
1429
- setError,
1430
- _executeSchema,
1431
- _getWatch,
1432
- _getDirty,
1433
- _updateValid,
1434
- _removeUnmounted,
1435
- _updateFieldArray,
1436
- _updateDisabledField,
1437
- _getFieldArray,
1438
- _reset,
1439
- _resetDefaultValues,
1440
- _updateFormState,
1441
- _disableForm,
1442
- _subjects,
1443
- _proxyFormState,
1444
- _setErrors,
1445
- get _fields() {
1446
- return _fields;
1447
- },
1448
- get _formValues() {
1449
- return _formValues;
1450
- },
1451
- get _state() {
1452
- return _state;
1453
- },
1454
- set _state(value) {
1455
- _state = value;
1456
- },
1457
- get _defaultValues() {
1458
- return _defaultValues;
1459
- },
1460
- get _names() {
1461
- return _names;
1462
- },
1463
- set _names(value) {
1464
- _names = value;
1465
- },
1466
- get _formState() {
1467
- return _formState;
1468
- },
1469
- set _formState(value) {
1470
- _formState = value;
1471
- },
1472
- get _options() {
1473
- return _options;
1474
- },
1475
- set _options(value) {
1476
- _options = {
1477
- ..._options,
1478
- ...value
1479
- };
1480
- }
1481
- },
1482
- trigger,
1483
- register,
1484
- handleSubmit,
1485
- watch,
1486
- setValue,
1487
- getValues,
1488
- reset,
1489
- resetField,
1490
- clearErrors,
1491
- unregister,
1492
- setError,
1493
- setFocus,
1494
- getFieldState
1495
- };
1496
- }
1497
- function useForm(props = {}) {
1498
- const _formControl = React.useRef();
1499
- const _values = React.useRef();
1500
- const [formState, updateFormState] = React.useState({
1501
- isDirty: false,
1502
- isValidating: false,
1503
- isLoading: isFunction(props.defaultValues),
1504
- isSubmitted: false,
1505
- isSubmitting: false,
1506
- isSubmitSuccessful: false,
1507
- isValid: false,
1508
- submitCount: 0,
1509
- dirtyFields: {},
1510
- touchedFields: {},
1511
- errors: props.errors || {},
1512
- disabled: false,
1513
- defaultValues: isFunction(props.defaultValues) ? void 0 : props.defaultValues
1514
- });
1515
- if (!_formControl.current) {
1516
- _formControl.current = {
1517
- ...createFormControl(props, () => updateFormState((formState2) => ({ ...formState2 }))),
1518
- formState
1519
- };
1520
- }
1521
- const control = _formControl.current.control;
1522
- control._options = props;
1523
- useSubscribe({
1524
- subject: control._subjects.state,
1525
- next: (value) => {
1526
- if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
1527
- updateFormState({ ...control._formState });
1528
- }
1529
- }
1530
- });
1531
- React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
1532
- React.useEffect(() => {
1533
- if (control._proxyFormState.isDirty) {
1534
- const isDirty = control._getDirty();
1535
- if (isDirty !== formState.isDirty) {
1536
- control._subjects.state.next({
1537
- isDirty
1538
- });
1539
- }
1540
- }
1541
- }, [control, formState.isDirty]);
1542
- React.useEffect(() => {
1543
- if (props.values && !deepEqual(props.values, _values.current)) {
1544
- control._reset(props.values, control._options.resetOptions);
1545
- _values.current = props.values;
1546
- updateFormState((state) => ({ ...state }));
1547
- } else {
1548
- control._resetDefaultValues();
1549
- }
1550
- }, [props.values, control]);
1551
- React.useEffect(() => {
1552
- if (props.errors) {
1553
- control._setErrors(props.errors);
1554
- }
1555
- }, [props.errors, control]);
1556
- React.useEffect(() => {
1557
- if (!control._state.mount) {
1558
- control._updateValid();
1559
- control._state.mount = true;
1560
- }
1561
- if (control._state.watch) {
1562
- control._state.watch = false;
1563
- control._subjects.state.next({ ...control._formState });
1564
- }
1565
- control._removeUnmounted();
1566
- });
1567
- _formControl.current.formState = getProxyFormState(formState, control);
1568
- return _formControl.current;
1569
- }
1570
- const QuickBooksSettingsPage = () => {
1571
- const [_, setSettings] = useState(null);
1572
- const [connection, setConnection] = useState(null);
1573
- const [isLoading, setIsLoading] = useState(true);
1574
- const [isSaving, setIsSaving] = useState(false);
1575
- const { control, handleSubmit, reset } = useForm({
1576
- defaultValues: {
1577
- auto_sync_customers: false,
1578
- auto_sync_products: false,
1579
- auto_sync_orders: false,
1580
- reverse_sync_customers: false,
1581
- reverse_sync_products: false,
1582
- reverse_sync_orders: false
1583
- }
1584
- });
1585
- useEffect(() => {
1586
- fetchData();
1587
- }, []);
1588
- const fetchData = async () => {
1589
- try {
1590
- setIsLoading(true);
1591
- const connRes = await fetch("/admin/quickbooks/connection");
1592
- const connData = await connRes.json();
1593
- setConnection(connData);
1594
- const setRes = await fetch("/admin/quickbooks/settings");
1595
- const setData = await setRes.json();
1596
- if (setData.settings) {
1597
- setSettings(setData.settings);
1598
- reset(setData.settings);
1599
- }
1600
- } catch (err) {
1601
- toast.error("Failed to load QuickBooks data");
1602
- } finally {
1603
- setIsLoading(false);
1604
- }
1605
- };
1606
- const onSubmit = async (data) => {
1607
- try {
1608
- setIsSaving(true);
1609
- const res = await fetch("/admin/quickbooks/settings", {
1610
- method: "PUT",
1611
- headers: { "Content-Type": "application/json" },
1612
- body: JSON.stringify(data)
1613
- });
1614
- if (res.ok) {
1615
- toast.success("Settings saved successfully");
1616
- const updated = await res.json();
1617
- setSettings(updated.settings);
1618
- reset(updated.settings);
1619
- } else {
1620
- throw new Error("Failed to save");
1621
- }
1622
- } catch (err) {
1623
- toast.error("Failed to save settings");
1624
- } finally {
1625
- setIsSaving(false);
1626
- }
1627
- };
1628
- const handleConnect = async () => {
1629
- try {
1630
- const res = await fetch("/admin/quickbooks/connect");
1631
- const data = await res.json();
1632
- if (data.url) {
1633
- window.location.href = data.url;
1634
- } else {
1635
- toast.error("Failed to get authorization URL");
1636
- }
1637
- } catch (err) {
1638
- toast.error("Connection error");
1639
- }
1640
- };
1641
- const handleDisconnect = async () => {
1642
- if (!confirm("Are you sure you want to disconnect?")) return;
1643
- try {
1644
- const res = await fetch("/admin/quickbooks/disconnect", {
1645
- method: "POST"
1646
- });
1647
- if (res.ok) {
1648
- toast.success("Disconnected from QuickBooks");
1649
- fetchData();
1650
- } else {
1651
- toast.error("Failed to disconnect");
1652
- }
1653
- } catch (err) {
1654
- toast.error("Disconnection error");
1655
- }
1656
- };
1657
- if (isLoading) return /* @__PURE__ */ jsx(Text, { children: "Loading..." });
1658
- return /* @__PURE__ */ jsxs(Container, { className: "p-8 flex flex-col gap-y-8", children: [
1659
- /* @__PURE__ */ jsxs("div", { children: [
1660
- /* @__PURE__ */ jsx(Heading, { level: "h1", children: "QuickBooks Integration" }),
1661
- /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", children: "Manage your connection and synchronization settings." })
1662
- ] }),
1663
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-4 p-4 border rounded-lg bg-ui-bg-base", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
1664
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-1", children: [
1665
- /* @__PURE__ */ jsx(Heading, { level: "h2", children: "Connection Status" }),
1666
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
1667
- /* @__PURE__ */ jsx(Text, { children: "Status:" }),
1668
- (connection == null ? void 0 : connection.is_connected) ? /* @__PURE__ */ jsx(Badge, { color: "green", children: "Connected" }) : /* @__PURE__ */ jsx(Badge, { color: "red", children: "Disconnected" })
1669
- ] }),
1670
- (connection == null ? void 0 : connection.is_connected) && /* @__PURE__ */ jsxs(Text, { className: "text-ui-fg-subtle text-small", children: [
1671
- "Connected to: ",
1672
- connection.company_name,
1673
- " (Realm ID: ",
1674
- connection.realm_id,
1675
- ")"
1676
- ] })
1677
- ] }),
1678
- /* @__PURE__ */ jsx("div", { children: (connection == null ? void 0 : connection.is_connected) ? /* @__PURE__ */ jsx(Button, { variant: "danger", onClick: handleDisconnect, children: "Disconnect" }) : /* @__PURE__ */ jsx(Button, { onClick: handleConnect, children: "Connect to QuickBooks" }) })
1679
- ] }) }),
1680
- /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit(onSubmit), className: "flex flex-col gap-y-6", children: [
1681
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
1682
- /* @__PURE__ */ jsx(Heading, { level: "h2", children: "Account Mappings" }),
1683
- /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", children: "Enter the QuickBooks Account IDs (Numeric) for transactions." }),
1684
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 md:grid-cols-3 gap-4", children: [
1685
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
1686
- /* @__PURE__ */ jsx(Label, { children: "Income Account ID" }),
1687
- /* @__PURE__ */ jsx(
1688
- Controller,
1689
- {
1690
- control,
1691
- name: "income_account_id",
1692
- render: ({ field }) => /* @__PURE__ */ jsx(Input, { ...field, placeholder: "e.g. 1" })
1693
- }
1694
- )
1695
- ] }),
1696
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
1697
- /* @__PURE__ */ jsx(Label, { children: "COGS Account ID" }),
1698
- /* @__PURE__ */ jsx(
1699
- Controller,
1700
- {
1701
- control,
1702
- name: "cogs_account_id",
1703
- render: ({ field }) => /* @__PURE__ */ jsx(Input, { ...field, placeholder: "e.g. 50" })
1704
- }
1705
- )
1706
- ] }),
1707
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
1708
- /* @__PURE__ */ jsx(Label, { children: "Asset Account ID" }),
1709
- /* @__PURE__ */ jsx(
1710
- Controller,
1711
- {
1712
- control,
1713
- name: "asset_account_id",
1714
- render: ({ field }) => /* @__PURE__ */ jsx(Input, { ...field, placeholder: "e.g. 80" })
1715
- }
1716
- )
1717
- ] })
1718
- ] })
1719
- ] }),
1720
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
1721
- /* @__PURE__ */ jsx(Heading, { level: "h2", children: "Synchronization Settings" }),
1722
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-8", children: [
1723
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
1724
- /* @__PURE__ */ jsx(Text, { weight: "plus", children: "Auto-Sync (Medusa to QuickBooks)" }),
1725
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
1726
- /* @__PURE__ */ jsx(Label, { children: "Sync Customers" }),
1727
- /* @__PURE__ */ jsx(
1728
- Controller,
1729
- {
1730
- control,
1731
- name: "auto_sync_customers",
1732
- render: ({ field: { value, onChange } }) => /* @__PURE__ */ jsx(Switch, { checked: value, onCheckedChange: onChange })
1733
- }
1734
- )
1735
- ] }),
1736
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
1737
- /* @__PURE__ */ jsx(Label, { children: "Sync Products" }),
1738
- /* @__PURE__ */ jsx(
1739
- Controller,
1740
- {
1741
- control,
1742
- name: "auto_sync_products",
1743
- render: ({ field: { value, onChange } }) => /* @__PURE__ */ jsx(Switch, { checked: value, onCheckedChange: onChange })
1744
- }
1745
- )
1746
- ] }),
1747
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
1748
- /* @__PURE__ */ jsx(Label, { children: "Sync Orders" }),
1749
- /* @__PURE__ */ jsx(
1750
- Controller,
1751
- {
1752
- control,
1753
- name: "auto_sync_orders",
1754
- render: ({ field: { value, onChange } }) => /* @__PURE__ */ jsx(Switch, { checked: value, onCheckedChange: onChange })
1755
- }
1756
- )
1757
- ] })
1758
- ] }),
1759
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
1760
- /* @__PURE__ */ jsx(Text, { weight: "plus", children: "Reverse Sync (QuickBooks to Medusa)" }),
1761
- /* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle text-small", children: "Requires Webhooks to be configured in QuickBooks." }),
1762
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
1763
- /* @__PURE__ */ jsx(Label, { children: "Sync Customers (Recieve Updates)" }),
1764
- /* @__PURE__ */ jsx(
1765
- Controller,
1766
- {
1767
- control,
1768
- name: "reverse_sync_customers",
1769
- render: ({ field: { value, onChange } }) => /* @__PURE__ */ jsx(Switch, { checked: value, onCheckedChange: onChange })
1770
- }
1771
- )
1772
- ] }),
1773
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
1774
- /* @__PURE__ */ jsx(Label, { children: "Sync Products (Receive Updates)" }),
1775
- /* @__PURE__ */ jsx(
1776
- Controller,
1777
- {
1778
- control,
1779
- name: "reverse_sync_products",
1780
- render: ({ field: { value, onChange } }) => /* @__PURE__ */ jsx(Switch, { checked: value, onCheckedChange: onChange })
1781
- }
1782
- )
1783
- ] })
1784
- ] })
1785
- ] })
1786
- ] }),
1787
- /* @__PURE__ */ jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsx(Button, { type: "submit", isLoading: isSaving, children: "Save Settings" }) })
1788
- ] })
1789
- ] });
1790
- };
1791
- const config = defineRouteConfig({
1792
- label: "QuickBooks",
1793
- icon: CurrencyDollar
1794
- });
1795
- const i18nTranslations0 = {};
1796
- const widgetModule = { widgets: [] };
1797
- const routeModule = {
1798
- routes: [
1799
- {
1800
- Component: QuickBooksSettingsPage,
1801
- path: "/settings/quickbooks"
1802
- }
1803
- ]
1804
- };
1805
- const menuItemModule = {
1806
- menuItems: [
1807
- {
1808
- label: config.label,
1809
- icon: config.icon,
1810
- path: "/settings/quickbooks",
1811
- nested: void 0,
1812
- rank: void 0,
1813
- translationNs: void 0
1814
- }
1815
- ]
1816
- };
1817
- const formModule = { customFields: {} };
1818
- const displayModule = {
1819
- displays: {}
1820
- };
1821
- const i18nModule = { resources: i18nTranslations0 };
1822
- const plugin = {
1823
- widgetModule,
1824
- routeModule,
1825
- menuItemModule,
1826
- formModule,
1827
- displayModule,
1828
- i18nModule
1829
- };
1830
- export {
1831
- plugin as default
1832
- };
1
+ // Medusa Admin Extension Entry Point
2
+ // Must export 'widgets' and 'default' to be valid for the dashboard loader.
3
+ export const widgets = [];
4
+ const plugin = { widgets };
5
+ export default plugin;