quickbooks-medusa-plugin 0.0.6 → 0.0.8

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