@vulcanjs/react-ui 0.6.6-y.11 → 0.6.6-y.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -597,12 +597,12 @@ init_esm_shims();
597
597
 
598
598
  // components/form/core/Form/Form.tsx
599
599
  init_esm_shims();
600
- import React12, { useEffect as useEffect2, useRef as useRef2, useState as useState2 } from "react";
600
+ import React5, { useEffect as useEffect2, useRef as useRef2, useState } from "react";
601
601
  import { runCallbacks, getErrors } from "@vulcanjs/core";
602
602
  import { useIntlContext as useIntlContext2 } from "@vulcanjs/react-i18n";
603
603
  import { removeProperty } from "@vulcanjs/utils";
604
604
  import cloneDeep from "lodash/cloneDeep.js";
605
- import compact3 from "lodash/compact.js";
605
+ import compact2 from "lodash/compact.js";
606
606
  import get2 from "lodash/get.js";
607
607
  import isObject from "lodash/isObject.js";
608
608
  import mapValues from "lodash/mapValues.js";
@@ -1152,12 +1152,10 @@ var useWarnOnUnsaved = /* @__PURE__ */ __name(({ isChanged, warnUnsavedChanges }
1152
1152
  });
1153
1153
  }, "useWarnOnUnsaved");
1154
1154
 
1155
- // components/VulcanComponents/index.ts
1156
- init_esm_shims();
1157
-
1158
- // components/VulcanComponents/Provider.tsx
1155
+ // components/VulcanComponents/Consumer.tsx
1159
1156
  init_esm_shims();
1160
- import React5 from "react";
1157
+ import { deprecate } from "@vulcanjs/utils";
1158
+ import React4, { useContext as useContext2 } from "react";
1161
1159
 
1162
1160
  // components/VulcanComponents/Context.ts
1163
1161
  init_esm_shims();
@@ -1184,9 +1182,6 @@ var VulcanComponentsContext = React3.createContext(new Proxy({
1184
1182
  VulcanComponentsContext.displayName = "VulcanComponentsContext";
1185
1183
 
1186
1184
  // components/VulcanComponents/Consumer.tsx
1187
- init_esm_shims();
1188
- import { deprecate } from "@vulcanjs/utils";
1189
- import React4, { useContext as useContext2 } from "react";
1190
1185
  function _extends() {
1191
1186
  _extends = Object.assign || function(target) {
1192
1187
  for (var i = 1; i < arguments.length; i++) {
@@ -1215,8 +1210,7 @@ var withVulcanComponents = /* @__PURE__ */ __name((C) => (props) => {
1215
1210
  }, props));
1216
1211
  }, "withVulcanComponents");
1217
1212
 
1218
- // components/VulcanComponents/Provider.tsx
1219
- import { debugVulcan } from "@vulcanjs/utils";
1213
+ // components/form/core/Form/Form.tsx
1220
1214
  function _extends2() {
1221
1215
  _extends2 = Object.assign || function(target) {
1222
1216
  for (var i = 1; i < arguments.length; i++) {
@@ -1232,180 +1226,549 @@ function _extends2() {
1232
1226
  return _extends2.apply(this, arguments);
1233
1227
  }
1234
1228
  __name(_extends2, "_extends");
1235
- var debugComponents = debugVulcan("components");
1236
- var VulcanComponentsProvider = /* @__PURE__ */ __name((_a) => {
1237
- var _b = _a, { value } = _b, props = __objRest(_b, ["value"]);
1238
- const currentComponents = useVulcanComponents();
1239
- debugComponents("Current components __not_initialized?", currentComponents.__not_initialized);
1240
- const mergedComponents = __spreadValues(__spreadValues({}, (currentComponents == null ? void 0 : currentComponents.__not_initialized) ? {} : currentComponents || {}), value || {});
1241
- debugComponents("Merged components", mergedComponents);
1242
- Object.keys(mergedComponents).forEach((componentName) => {
1243
- if (mergedComponents[componentName]) {
1244
- mergedComponents[componentName].displayName = "Vulcan." + componentName;
1245
- } else {
1246
- console.warn(`Encountered an undefined component: ${componentName}.
1247
- The component may not be registered, or import failed.
1248
- For instance due to an infinite import loop when importing
1249
- "useVulcanComponents" from index instead of Consumer.`);
1229
+ var compactParent = /* @__PURE__ */ __name((object, path) => {
1230
+ const parentPath = getParentPath(path);
1231
+ const compactIfArray = /* @__PURE__ */ __name((x) => Array.isArray(x) ? compact2(x) : x, "compactIfArray");
1232
+ update(object, parentPath, compactIfArray);
1233
+ }, "compactParent");
1234
+ var getDefaultValues = /* @__PURE__ */ __name((convertedSchema) => {
1235
+ return pickBy(mapValues(convertedSchema, (field) => field.defaultValue), (value) => value);
1236
+ }, "getDefaultValues");
1237
+ var compactObject = /* @__PURE__ */ __name((o) => omitBy(o, (f) => f === null || f === void 0), "compactObject");
1238
+ var getInitialStateFromProps = /* @__PURE__ */ __name((nextProps) => {
1239
+ const schema = nextProps.schema || nextProps.model.schema;
1240
+ const convertedSchema = convertSchema(schema);
1241
+ const formType = nextProps.document ? "edit" : "new";
1242
+ const defaultValues = formType === "new" ? getDefaultValues(convertedSchema) : {};
1243
+ const initialDocument = merge2({}, defaultValues, nextProps.prefilledProps, compactObject(nextProps.document));
1244
+ Object.keys(convertedSchema).forEach((key) => {
1245
+ let minCount = convertedSchema[key].minCount;
1246
+ if (minCount) {
1247
+ initialDocument[key] = initialDocument[key] || [];
1248
+ while (initialDocument[key].length < minCount)
1249
+ initialDocument[key].push({});
1250
1250
  }
1251
1251
  });
1252
- return /* @__PURE__ */ React5.createElement(VulcanComponentsContext.Provider, _extends2({
1253
- value: mergedComponents
1254
- }, props));
1255
- }, "VulcanComponentsProvider");
1256
-
1257
- // components/VulcanComponents/typings.ts
1258
- init_esm_shims();
1259
- import React6 from "react";
1260
-
1261
- // components/VulcanComponents/defaultVulcanComponents/index.ts
1262
- init_esm_shims();
1263
-
1264
- // components/core/LoadingButton.tsx
1265
- init_esm_shims();
1266
- import React7 from "react";
1267
- function _extends3() {
1268
- _extends3 = Object.assign || function(target) {
1269
- for (var i = 1; i < arguments.length; i++) {
1270
- var source = arguments[i];
1271
- for (var key in source) {
1272
- if (Object.prototype.hasOwnProperty.call(source, key)) {
1273
- target[key] = source[key];
1274
- }
1275
- }
1276
- }
1277
- return target;
1252
+ removeProperty(initialDocument, "__typename");
1253
+ return {
1254
+ disabled: nextProps.disabled,
1255
+ errors: [],
1256
+ deletedValues: [],
1257
+ currentValues: {},
1258
+ originalSchema: convertSchema(schema, {
1259
+ removeArrays: false
1260
+ }),
1261
+ schema: convertedSchema,
1262
+ flatSchema: convertSchema(schema, {
1263
+ flatten: true
1264
+ }),
1265
+ initialDocument,
1266
+ currentDocument: initialDocument
1278
1267
  };
1279
- return _extends3.apply(this, arguments);
1280
- }
1281
- __name(_extends3, "_extends");
1282
- var LoadingButton = /* @__PURE__ */ __name((_a) => {
1283
- var _b = _a, { loading, label, onClick, children, className = "" } = _b, rest = __objRest(_b, ["loading", "label", "onClick", "children", "className"]);
1284
- const Components2 = useVulcanComponents();
1285
- const wrapperStyle = {
1286
- position: "relative"
1268
+ }, "getInitialStateFromProps");
1269
+ var getChildrenProps = /* @__PURE__ */ __name((props, state, options, callbacks) => {
1270
+ const { currentUser, repeatErrors, submitLabel, cancelLabel, revertLabel, cancelCallback, revertCallback, id, model, prefilledProps, itemProperties, contextName, showRemove, showDelete } = props;
1271
+ const { disabled, currentDocument } = state;
1272
+ const { formType } = options;
1273
+ const { deleteDocument } = callbacks;
1274
+ const commonProps = {
1275
+ document: currentDocument,
1276
+ formType,
1277
+ currentUser,
1278
+ disabled,
1279
+ prefilledProps,
1280
+ itemProperties,
1281
+ contextName
1287
1282
  };
1288
- const labelStyle = loading ? {
1289
- opacity: 0.5
1290
- } : {};
1291
- const loadingStyle = loading ? {
1292
- position: "absolute",
1293
- top: 0,
1294
- bottom: 0,
1295
- left: 0,
1296
- right: 0,
1297
- display: "flex",
1298
- justifyContent: "center",
1299
- alignItems: "center"
1300
- } : {
1301
- display: "none"
1283
+ const docClassName = `document-${formType}`;
1284
+ const modelName = model.name.toLowerCase();
1285
+ const formProps = {
1286
+ className: `${docClassName} ${docClassName}-${modelName}`,
1287
+ id
1288
+ };
1289
+ const formGroupProps = /* @__PURE__ */ __name((group) => __spreadValues(__spreadProps(__spreadValues({
1290
+ key: group.name
1291
+ }, group), {
1292
+ group: omit(group, [
1293
+ "fields"
1294
+ ])
1295
+ }), commonProps), "formGroupProps");
1296
+ const formSubmitProps = {
1297
+ model,
1298
+ currentUser,
1299
+ submitLabel,
1300
+ cancelLabel,
1301
+ revertLabel,
1302
+ cancelCallback,
1303
+ revertCallback,
1304
+ document: currentDocument,
1305
+ deleteDocument: formType === "edit" && showRemove && showDelete && deleteDocument || null
1306
+ };
1307
+ const formLayoutProps = {
1308
+ formProps,
1309
+ repeatErrors,
1310
+ submitProps: formSubmitProps,
1311
+ commonProps
1302
1312
  };
1303
- return /* @__PURE__ */ React7.createElement(Components2.Button, _extends3({
1304
- className: `loading-button ${loading ? "loading-button-loading" : "loading-button-notloading"} ${className}`,
1305
- onClick
1306
- }, rest), /* @__PURE__ */ React7.createElement("span", {
1307
- style: wrapperStyle
1308
- }, /* @__PURE__ */ React7.createElement("span", {
1309
- style: labelStyle
1310
- }, label || children), /* @__PURE__ */ React7.createElement("span", {
1311
- style: loadingStyle
1312
- }, /* @__PURE__ */ React7.createElement(Components2.Loading, null))));
1313
- }, "LoadingButton");
1314
-
1315
- // components/core/MutationButton.tsx
1316
- init_esm_shims();
1317
- import React8, { useState } from "react";
1318
- import { useMutation } from "@apollo/client";
1319
-
1320
- // ../../node_modules/graphql-tag/lib/index.js
1321
- init_esm_shims();
1322
-
1323
- // ../../node_modules/tslib/modules/index.js
1324
- init_esm_shims();
1325
- var import_tslib = __toESM(require_tslib(), 1);
1326
- var { __extends, __assign, __rest, __decorate, __param, __metadata, __awaiter, __generator, __exportStar, __createBinding, __values, __read, __spread, __spreadArrays, __spreadArray, __await, __asyncGenerator, __asyncDelegator, __asyncValues, __makeTemplateObject, __importStar, __importDefault, __classPrivateFieldGet, __classPrivateFieldSet, __classPrivateFieldIn } = import_tslib.default;
1327
-
1328
- // ../../node_modules/graphql/index.mjs
1329
- init_esm_shims();
1330
-
1331
- // ../../node_modules/graphql/language/parser.mjs
1332
- init_esm_shims();
1333
-
1334
- // ../../node_modules/graphql/error/syntaxError.mjs
1335
- init_esm_shims();
1336
-
1337
- // ../../node_modules/graphql/error/GraphQLError.mjs
1338
- init_esm_shims();
1339
-
1340
- // ../../node_modules/graphql/jsutils/isObjectLike.mjs
1341
- init_esm_shims();
1342
- function _typeof(obj) {
1343
- "@babel/helpers - typeof";
1344
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
1345
- _typeof = /* @__PURE__ */ __name(function _typeof5(obj2) {
1346
- return typeof obj2;
1347
- }, "_typeof");
1348
- } else {
1349
- _typeof = /* @__PURE__ */ __name(function _typeof5(obj2) {
1350
- return obj2 && typeof Symbol === "function" && obj2.constructor === Symbol && obj2 !== Symbol.prototype ? "symbol" : typeof obj2;
1351
- }, "_typeof");
1352
- }
1353
- return _typeof(obj);
1354
- }
1355
- __name(_typeof, "_typeof");
1356
- function isObjectLike(value) {
1357
- return _typeof(value) == "object" && value !== null;
1358
- }
1359
- __name(isObjectLike, "isObjectLike");
1360
-
1361
- // ../../node_modules/graphql/polyfills/symbols.mjs
1362
- init_esm_shims();
1363
- var SYMBOL_ITERATOR = typeof Symbol === "function" && Symbol.iterator != null ? Symbol.iterator : "@@iterator";
1364
- var SYMBOL_ASYNC_ITERATOR = typeof Symbol === "function" && Symbol.asyncIterator != null ? Symbol.asyncIterator : "@@asyncIterator";
1365
- var SYMBOL_TO_STRING_TAG = typeof Symbol === "function" && Symbol.toStringTag != null ? Symbol.toStringTag : "@@toStringTag";
1366
-
1367
- // ../../node_modules/graphql/language/location.mjs
1368
- init_esm_shims();
1369
- function getLocation(source, position) {
1370
- var lineRegexp = /\r\n|[\n\r]/g;
1371
- var line = 1;
1372
- var column = position + 1;
1373
- var match;
1374
- while ((match = lineRegexp.exec(source.body)) && match.index < position) {
1375
- line += 1;
1376
- column = position + 1 - (match.index + match[0].length);
1377
- }
1378
1313
  return {
1379
- line,
1380
- column
1314
+ commonProps,
1315
+ formSubmitProps,
1316
+ formGroupProps,
1317
+ formLayoutProps
1381
1318
  };
1382
- }
1383
- __name(getLocation, "getLocation");
1384
-
1385
- // ../../node_modules/graphql/language/printLocation.mjs
1386
- init_esm_shims();
1387
- function printLocation(location) {
1388
- return printSourceLocation(location.source, getLocation(location.source, location.start));
1389
- }
1390
- __name(printLocation, "printLocation");
1391
- function printSourceLocation(source, sourceLocation) {
1392
- var firstLineColumnOffset = source.locationOffset.column - 1;
1393
- var body = whitespace(firstLineColumnOffset) + source.body;
1394
- var lineIndex = sourceLocation.line - 1;
1395
- var lineOffset = source.locationOffset.line - 1;
1396
- var lineNum = sourceLocation.line + lineOffset;
1397
- var columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
1398
- var columnNum = sourceLocation.column + columnOffset;
1399
- var locationStr = "".concat(source.name, ":").concat(lineNum, ":").concat(columnNum, "\n");
1400
- var lines = body.split(/\r\n|[\n\r]/g);
1401
- var locationLine = lines[lineIndex];
1402
- if (locationLine.length > 120) {
1403
- var subLineIndex = Math.floor(columnNum / 80);
1404
- var subLineColumnNum = columnNum % 80;
1405
- var subLines = [];
1406
- for (var i = 0; i < locationLine.length; i += 80) {
1407
- subLines.push(locationLine.slice(i, i + 80));
1408
- }
1319
+ }, "getChildrenProps");
1320
+ var FormWarnUnsaved = /* @__PURE__ */ __name(({ isChanged, warnUnsavedChanges, children }) => {
1321
+ useWarnOnUnsaved({
1322
+ isChanged,
1323
+ warnUnsavedChanges
1324
+ });
1325
+ return /* @__PURE__ */ React5.createElement(React5.Fragment, null, children);
1326
+ }, "FormWarnUnsaved");
1327
+ var getData = /* @__PURE__ */ __name((customArgs, props, state, { submitFormCallbacks, form }) => {
1328
+ const { currentDocument } = state;
1329
+ const { model, prefilledProps } = props;
1330
+ let data = prefilledProps || {};
1331
+ data = omitBy(data, (value, key) => key.endsWith(".$"));
1332
+ const args = __spreadValues({
1333
+ schema: model.schema,
1334
+ excludeRemovedFields: false,
1335
+ excludeHiddenFields: false,
1336
+ replaceIntlFields: true,
1337
+ addExtraFields: false
1338
+ }, customArgs);
1339
+ const fields = getFieldNames(props, currentDocument, args);
1340
+ data = __spreadValues(__spreadValues({}, data), pick2(currentDocument, ...fields));
1341
+ state.deletedValues.forEach((path) => {
1342
+ if (path.includes(".")) {
1343
+ compactParent(data, path);
1344
+ }
1345
+ });
1346
+ data = runCallbacks({
1347
+ callbacks: submitFormCallbacks,
1348
+ iterator: data,
1349
+ args: [
1350
+ {}
1351
+ ]
1352
+ });
1353
+ return data;
1354
+ }, "getData");
1355
+ var Form = /* @__PURE__ */ __name((props) => {
1356
+ const { initCallback, createDocument, updateDocument, deleteDocument } = props;
1357
+ const initialState = getInitialStateFromProps(props);
1358
+ const { schema, originalSchema, flatSchema, initialDocument } = initialState;
1359
+ const isFirstRender = useRef2(true);
1360
+ useEffect2(() => {
1361
+ if (isFirstRender.current) {
1362
+ isFirstRender.current = false;
1363
+ return;
1364
+ }
1365
+ if (initCallback)
1366
+ initCallback(initialState.currentDocument);
1367
+ }, [
1368
+ initCallback
1369
+ ]);
1370
+ const defaultProps = {
1371
+ layout: "horizontal",
1372
+ prefilledProps: {},
1373
+ repeatErrors: false,
1374
+ showRemove: true,
1375
+ showDelete: true
1376
+ };
1377
+ const allProps = __spreadValues(__spreadValues({}, defaultProps), props);
1378
+ const defaultValues = {};
1379
+ const submitFormCallbacks = [];
1380
+ const successFormCallbacks = [];
1381
+ const failureFormCallbacks = [];
1382
+ const intl = useIntlContext2();
1383
+ const [errors, setErrors] = useState([]);
1384
+ const throwError = /* @__PURE__ */ __name((error) => {
1385
+ let formErrors = getErrors(error);
1386
+ console.log(formErrors);
1387
+ setErrors((prevErrors) => [
1388
+ ...prevErrors,
1389
+ ...formErrors
1390
+ ]);
1391
+ }, "throwError");
1392
+ const clearFieldErrors = /* @__PURE__ */ __name((path) => {
1393
+ setErrors((prevErrors) => prevErrors.filter((error) => error.path !== path));
1394
+ }, "clearFieldErrors");
1395
+ const [deletedValues, setDeletedValues] = useState([]);
1396
+ const addToDeletedValues = /* @__PURE__ */ __name((name) => {
1397
+ setDeletedValues((prevDeletedValues) => [
1398
+ ...prevDeletedValues,
1399
+ name
1400
+ ]);
1401
+ }, "addToDeletedValues");
1402
+ const [callbacks, setCallbacks] = useState({
1403
+ submitFormCallbacks: [],
1404
+ successFormCallbacks: [],
1405
+ failureFormCallbacks: []
1406
+ });
1407
+ const addToSubmitForm = /* @__PURE__ */ __name((callback) => {
1408
+ setCallbacks((cbs) => __spreadValues({}, cbs));
1409
+ }, "addToSubmitForm");
1410
+ const addToSuccessForm = /* @__PURE__ */ __name((callback) => {
1411
+ setCallbacks((cbs) => __spreadProps(__spreadValues({}, cbs), {
1412
+ successFormCallbacks: [
1413
+ ...cbs.successFormCallbacks,
1414
+ callback
1415
+ ]
1416
+ }));
1417
+ }, "addToSuccessForm");
1418
+ const addToFailureForm = /* @__PURE__ */ __name((callback) => {
1419
+ setCallbacks((cbs) => __spreadProps(__spreadValues({}, cbs), {
1420
+ failureFormCallbacks: [
1421
+ ...cbs.failureFormCallbacks,
1422
+ callback
1423
+ ]
1424
+ }));
1425
+ }, "addToFailureForm");
1426
+ const clearFormCallbacks = /* @__PURE__ */ __name(() => {
1427
+ setCallbacks({
1428
+ submitFormCallbacks: [],
1429
+ successFormCallbacks: [],
1430
+ failureFormCallbacks: []
1431
+ });
1432
+ }, "clearFormCallbacks");
1433
+ const [currentValues, setCurrentValues] = useState({});
1434
+ const submitFormContext = /* @__PURE__ */ __name((formType) => (event) => {
1435
+ submitForm(formType)(event);
1436
+ }, "submitFormContext");
1437
+ const [currentDocument, setCurrentDocument] = useState(initialDocument);
1438
+ const updateCurrentValues = /* @__PURE__ */ __name((newValues, options = {}) => {
1439
+ const { mode = "overwrite" } = options;
1440
+ const { changeCallback } = props;
1441
+ const newState = {
1442
+ currentValues: cloneDeep(currentValues),
1443
+ currentDocument: cloneDeep(currentDocument),
1444
+ deletedValues: cloneDeep(deletedValues)
1445
+ };
1446
+ Object.keys(newValues).forEach((key) => {
1447
+ const path = key;
1448
+ let value = newValues[key];
1449
+ if (isEmptyValue(value)) {
1450
+ unset(newState.currentValues, path);
1451
+ set2(newState.currentDocument, path, null);
1452
+ newState.deletedValues = [
1453
+ ...newState.deletedValues,
1454
+ path
1455
+ ];
1456
+ } else {
1457
+ set2(newState.currentValues, path, value);
1458
+ if (mode === "merge" && (Array.isArray(value) || isObject(value))) {
1459
+ const oldValue = get2(newState.currentDocument, path);
1460
+ set2(newState.currentDocument, path, merge2(oldValue, value));
1461
+ } else {
1462
+ set2(newState.currentDocument, path, value);
1463
+ }
1464
+ newState.deletedValues = without(newState.deletedValues, path);
1465
+ }
1466
+ });
1467
+ if (changeCallback)
1468
+ changeCallback(newState.currentDocument);
1469
+ setCurrentValues(newState.currentValues);
1470
+ setCurrentDocument(newState.currentDocument);
1471
+ setDeletedValues(newState.deletedValues);
1472
+ }, "updateCurrentValues");
1473
+ const refetchForm = /* @__PURE__ */ __name(() => {
1474
+ if (props.refetch) {
1475
+ props.refetch();
1476
+ }
1477
+ }, "refetchForm");
1478
+ const [disabled, setDisabled] = useState(false);
1479
+ const [success, setSuccess] = useState(false);
1480
+ const clearForm = /* @__PURE__ */ __name((options = {}) => {
1481
+ const { document: optionsDocument } = options;
1482
+ const document = optionsDocument ? merge2({}, props.prefilledProps, optionsDocument) : null;
1483
+ setErrors([]);
1484
+ setCurrentValues({});
1485
+ setDeletedValues([]);
1486
+ setCurrentDocument(document || initialDocument);
1487
+ setDisabled(false);
1488
+ }, "clearForm");
1489
+ const newMutationSuccessCallback = /* @__PURE__ */ __name(function(result) {
1490
+ mutationSuccessCallback(result, "new");
1491
+ }, "newMutationSuccessCallback");
1492
+ const editMutationSuccessCallback = /* @__PURE__ */ __name(function(result) {
1493
+ mutationSuccessCallback(result, "edit");
1494
+ }, "editMutationSuccessCallback");
1495
+ const formRef = useRef2(null);
1496
+ const mutationSuccessCallback = /* @__PURE__ */ __name(function(result, mutationType) {
1497
+ setDisabled(false);
1498
+ setSuccess(true);
1499
+ if (mutationType === "new")
1500
+ refetchForm();
1501
+ let { document } = result;
1502
+ if (formRef.current) {
1503
+ clearForm({
1504
+ document: mutationType === "edit" ? document : void 0
1505
+ });
1506
+ }
1507
+ document = runCallbacks({
1508
+ callbacks: successFormCallbacks,
1509
+ iterator: document,
1510
+ args: [
1511
+ {
1512
+ form: formRef.current
1513
+ }
1514
+ ]
1515
+ });
1516
+ if (props.successCallback)
1517
+ props.successCallback(document, {
1518
+ form: this
1519
+ });
1520
+ }, "mutationSuccessCallback");
1521
+ const mutationErrorCallback = /* @__PURE__ */ __name((document, error) => {
1522
+ setDisabled(false);
1523
+ console.error("// graphQL Error");
1524
+ console.error(error);
1525
+ runCallbacks({
1526
+ callbacks: failureFormCallbacks,
1527
+ iterator: error,
1528
+ args: [
1529
+ {
1530
+ error,
1531
+ form: formRef.current
1532
+ }
1533
+ ]
1534
+ });
1535
+ if (!isEmpty(error)) {
1536
+ throwError(error);
1537
+ }
1538
+ if (props.errorCallback)
1539
+ props.errorCallback(document, error, {
1540
+ form: void 0
1541
+ });
1542
+ }, "mutationErrorCallback");
1543
+ const submitForm = /* @__PURE__ */ __name((formType) => async (event) => {
1544
+ var _a, _b;
1545
+ event && event.preventDefault();
1546
+ event && event.stopPropagation();
1547
+ const { contextName } = props;
1548
+ if (disabled) {
1549
+ return;
1550
+ }
1551
+ setErrors([]);
1552
+ setDisabled(true);
1553
+ let data = getData({
1554
+ replaceIntlFields: true,
1555
+ addExtraFields: false,
1556
+ mutableFields
1557
+ }, props, {
1558
+ currentDocument,
1559
+ deletedValues
1560
+ }, {
1561
+ form: formRef.current,
1562
+ submitFormCallbacks
1563
+ });
1564
+ if (props.submitCallback) {
1565
+ data = props.submitCallback(data) || data;
1566
+ }
1567
+ if (formType === "new") {
1568
+ try {
1569
+ const result = await createDocument({
1570
+ input: {
1571
+ data,
1572
+ contextName
1573
+ }
1574
+ });
1575
+ if ((_a = result.errors) == null ? void 0 : _a.length) {
1576
+ mutationErrorCallback(document1, result.errors[0]);
1577
+ } else {
1578
+ newMutationSuccessCallback(result);
1579
+ }
1580
+ } catch (error) {
1581
+ mutationErrorCallback(document1, error);
1582
+ }
1583
+ } else {
1584
+ try {
1585
+ const documentId = currentDocument._id;
1586
+ const result = await updateDocument({
1587
+ input: {
1588
+ id: documentId,
1589
+ data,
1590
+ contextName
1591
+ }
1592
+ });
1593
+ if ((_b = result.errors) == null ? void 0 : _b.length) {
1594
+ mutationErrorCallback(document1, result.errors[0]);
1595
+ } else {
1596
+ editMutationSuccessCallback(result);
1597
+ }
1598
+ } catch (error) {
1599
+ mutationErrorCallback(document1, error);
1600
+ }
1601
+ }
1602
+ }, "submitForm");
1603
+ const deleteDocumentWithConfirm = /* @__PURE__ */ __name(() => {
1604
+ const document = currentDocument;
1605
+ const documentId = props.document._id;
1606
+ const documentTitle = document.title || document.name || "";
1607
+ const deleteDocumentConfirm = intl.formatMessage({
1608
+ id: "forms.delete_confirm"
1609
+ }, {
1610
+ title: documentTitle
1611
+ });
1612
+ if (window.confirm(deleteDocumentConfirm)) {
1613
+ deleteDocument({
1614
+ input: {
1615
+ id: documentId
1616
+ }
1617
+ }).then((mutationResult) => {
1618
+ if (props.removeSuccessCallback)
1619
+ props.removeSuccessCallback({
1620
+ documentId,
1621
+ documentTitle
1622
+ });
1623
+ refetchForm();
1624
+ }).catch((error) => {
1625
+ console.log(error);
1626
+ });
1627
+ }
1628
+ }, "deleteDocumentWithConfirm");
1629
+ const { successComponent, document: document1, currentUser, model, warnUnsavedChanges } = props;
1630
+ const FormComponents = useVulcanComponents();
1631
+ const formType1 = document1 ? "edit" : "new";
1632
+ const mutableFields = formType1 === "edit" ? getEditableFields(schema, currentUser, initialDocument) : getInsertableFields(schema, currentUser);
1633
+ const { formLayoutProps, formGroupProps } = getChildrenProps(props, {
1634
+ disabled,
1635
+ currentDocument
1636
+ }, {
1637
+ formType: formType1
1638
+ }, {
1639
+ deleteDocument: deleteDocumentWithConfirm
1640
+ });
1641
+ const isChanged = isNotSameDocument(initialDocument, currentDocument);
1642
+ return success && successComponent ? successComponent : /* @__PURE__ */ React5.createElement(FormWarnUnsaved, {
1643
+ isChanged,
1644
+ warnUnsavedChanges
1645
+ }, /* @__PURE__ */ React5.createElement(FormContext.Provider, {
1646
+ value: {
1647
+ throwError,
1648
+ clearForm,
1649
+ refetchForm,
1650
+ isChanged,
1651
+ submitForm: submitFormContext(formType1),
1652
+ addToDeletedValues,
1653
+ updateCurrentValues,
1654
+ getDocument: () => currentDocument,
1655
+ getLabel: (fieldName, fieldLocale) => getLabel(model, flatSchema, intl, fieldName, fieldLocale),
1656
+ initialDocument,
1657
+ addToSubmitForm,
1658
+ addToSuccessForm,
1659
+ addToFailureForm,
1660
+ clearFormCallbacks,
1661
+ errors,
1662
+ currentValues,
1663
+ deletedValues,
1664
+ clearFieldErrors
1665
+ }
1666
+ }, /* @__PURE__ */ React5.createElement(FormComponents.FormLayout, _extends2({}, formLayoutProps), getFieldGroups(props, {
1667
+ currentDocument,
1668
+ schema,
1669
+ flatSchema,
1670
+ originalSchema
1671
+ }, intl, mutableFields, intl.formatMessage).map((group, i) => /* @__PURE__ */ React5.createElement(FormComponents.FormGroup, _extends2({
1672
+ key: i
1673
+ }, formGroupProps(group)))))));
1674
+ }, "Form");
1675
+
1676
+ // components/form/core/Form/typings.ts
1677
+ init_esm_shims();
1678
+
1679
+ // components/form/core/FormContainer.tsx
1680
+ init_esm_shims();
1681
+ import React8 from "react";
1682
+
1683
+ // ../../node_modules/graphql-tag/lib/index.js
1684
+ init_esm_shims();
1685
+
1686
+ // ../../node_modules/tslib/modules/index.js
1687
+ init_esm_shims();
1688
+ var import_tslib = __toESM(require_tslib(), 1);
1689
+ var { __extends, __assign, __rest, __decorate, __param, __metadata, __awaiter, __generator, __exportStar, __createBinding, __values, __read, __spread, __spreadArrays, __spreadArray, __await, __asyncGenerator, __asyncDelegator, __asyncValues, __makeTemplateObject, __importStar, __importDefault, __classPrivateFieldGet, __classPrivateFieldSet, __classPrivateFieldIn } = import_tslib.default;
1690
+
1691
+ // ../../node_modules/graphql/index.mjs
1692
+ init_esm_shims();
1693
+
1694
+ // ../../node_modules/graphql/language/parser.mjs
1695
+ init_esm_shims();
1696
+
1697
+ // ../../node_modules/graphql/error/syntaxError.mjs
1698
+ init_esm_shims();
1699
+
1700
+ // ../../node_modules/graphql/error/GraphQLError.mjs
1701
+ init_esm_shims();
1702
+
1703
+ // ../../node_modules/graphql/jsutils/isObjectLike.mjs
1704
+ init_esm_shims();
1705
+ function _typeof(obj) {
1706
+ "@babel/helpers - typeof";
1707
+ if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
1708
+ _typeof = /* @__PURE__ */ __name(function _typeof5(obj2) {
1709
+ return typeof obj2;
1710
+ }, "_typeof");
1711
+ } else {
1712
+ _typeof = /* @__PURE__ */ __name(function _typeof5(obj2) {
1713
+ return obj2 && typeof Symbol === "function" && obj2.constructor === Symbol && obj2 !== Symbol.prototype ? "symbol" : typeof obj2;
1714
+ }, "_typeof");
1715
+ }
1716
+ return _typeof(obj);
1717
+ }
1718
+ __name(_typeof, "_typeof");
1719
+ function isObjectLike(value) {
1720
+ return _typeof(value) == "object" && value !== null;
1721
+ }
1722
+ __name(isObjectLike, "isObjectLike");
1723
+
1724
+ // ../../node_modules/graphql/polyfills/symbols.mjs
1725
+ init_esm_shims();
1726
+ var SYMBOL_ITERATOR = typeof Symbol === "function" && Symbol.iterator != null ? Symbol.iterator : "@@iterator";
1727
+ var SYMBOL_ASYNC_ITERATOR = typeof Symbol === "function" && Symbol.asyncIterator != null ? Symbol.asyncIterator : "@@asyncIterator";
1728
+ var SYMBOL_TO_STRING_TAG = typeof Symbol === "function" && Symbol.toStringTag != null ? Symbol.toStringTag : "@@toStringTag";
1729
+
1730
+ // ../../node_modules/graphql/language/location.mjs
1731
+ init_esm_shims();
1732
+ function getLocation(source, position) {
1733
+ var lineRegexp = /\r\n|[\n\r]/g;
1734
+ var line = 1;
1735
+ var column = position + 1;
1736
+ var match;
1737
+ while ((match = lineRegexp.exec(source.body)) && match.index < position) {
1738
+ line += 1;
1739
+ column = position + 1 - (match.index + match[0].length);
1740
+ }
1741
+ return {
1742
+ line,
1743
+ column
1744
+ };
1745
+ }
1746
+ __name(getLocation, "getLocation");
1747
+
1748
+ // ../../node_modules/graphql/language/printLocation.mjs
1749
+ init_esm_shims();
1750
+ function printLocation(location) {
1751
+ return printSourceLocation(location.source, getLocation(location.source, location.start));
1752
+ }
1753
+ __name(printLocation, "printLocation");
1754
+ function printSourceLocation(source, sourceLocation) {
1755
+ var firstLineColumnOffset = source.locationOffset.column - 1;
1756
+ var body = whitespace(firstLineColumnOffset) + source.body;
1757
+ var lineIndex = sourceLocation.line - 1;
1758
+ var lineOffset = source.locationOffset.line - 1;
1759
+ var lineNum = sourceLocation.line + lineOffset;
1760
+ var columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
1761
+ var columnNum = sourceLocation.column + columnOffset;
1762
+ var locationStr = "".concat(source.name, ":").concat(lineNum, ":").concat(columnNum, "\n");
1763
+ var lines = body.split(/\r\n|[\n\r]/g);
1764
+ var locationLine = lines[lineIndex];
1765
+ if (locationLine.length > 120) {
1766
+ var subLineIndex = Math.floor(columnNum / 80);
1767
+ var subLineColumnNum = columnNum % 80;
1768
+ var subLines = [];
1769
+ for (var i = 0; i < locationLine.length; i += 80) {
1770
+ subLines.push(locationLine.slice(i, i + 80));
1771
+ }
1409
1772
  return locationStr + printPrefixedLines([["".concat(lineNum), subLines[0]]].concat(subLines.slice(1, subLineIndex + 1).map(function(subLine) {
1410
1773
  return ["", subLine];
1411
1774
  }), [[" ", whitespace(subLineColumnNum - 1) + "^"], ["", subLines[subLineIndex + 1]]]));
@@ -3547,150 +3910,68 @@ function parseDocument(source) {
3547
3910
  var cacheKey = normalize(source);
3548
3911
  if (!docCache.has(cacheKey)) {
3549
3912
  var parsed = parse(source, {
3550
- experimentalFragmentVariables,
3551
- allowLegacyFragmentVariables: experimentalFragmentVariables
3552
- });
3553
- if (!parsed || parsed.kind !== "Document") {
3554
- throw new Error("Not a valid GraphQL document.");
3555
- }
3556
- docCache.set(cacheKey, stripLoc(processFragments(parsed)));
3557
- }
3558
- return docCache.get(cacheKey);
3559
- }
3560
- __name(parseDocument, "parseDocument");
3561
- function gql(literals) {
3562
- var args = [];
3563
- for (var _i = 1; _i < arguments.length; _i++) {
3564
- args[_i - 1] = arguments[_i];
3565
- }
3566
- if (typeof literals === "string") {
3567
- literals = [
3568
- literals
3569
- ];
3570
- }
3571
- var result = literals[0];
3572
- args.forEach(function(arg, i) {
3573
- if (arg && arg.kind === "Document") {
3574
- result += arg.loc.source.body;
3575
- } else {
3576
- result += arg;
3577
- }
3578
- result += literals[i + 1];
3579
- });
3580
- return parseDocument(result);
3581
- }
3582
- __name(gql, "gql");
3583
- function resetCaches() {
3584
- docCache.clear();
3585
- fragmentSourceMap.clear();
3586
- }
3587
- __name(resetCaches, "resetCaches");
3588
- function disableFragmentWarnings() {
3589
- printFragmentWarnings = false;
3590
- }
3591
- __name(disableFragmentWarnings, "disableFragmentWarnings");
3592
- function enableExperimentalFragmentVariables() {
3593
- experimentalFragmentVariables = true;
3594
- }
3595
- __name(enableExperimentalFragmentVariables, "enableExperimentalFragmentVariables");
3596
- function disableExperimentalFragmentVariables() {
3597
- experimentalFragmentVariables = false;
3598
- }
3599
- __name(disableExperimentalFragmentVariables, "disableExperimentalFragmentVariables");
3600
- var extras = {
3601
- gql,
3602
- resetCaches,
3603
- disableFragmentWarnings,
3604
- enableExperimentalFragmentVariables,
3605
- disableExperimentalFragmentVariables
3606
- };
3607
- (function(gql_1) {
3608
- gql_1.gql = extras.gql, gql_1.resetCaches = extras.resetCaches, gql_1.disableFragmentWarnings = extras.disableFragmentWarnings, gql_1.enableExperimentalFragmentVariables = extras.enableExperimentalFragmentVariables, gql_1.disableExperimentalFragmentVariables = extras.disableExperimentalFragmentVariables;
3609
- })(gql || (gql = {}));
3610
- gql["default"] = gql;
3611
- var lib_default = gql;
3612
-
3613
- // components/core/MutationButton.tsx
3614
- function _extends4() {
3615
- _extends4 = Object.assign || function(target) {
3616
- for (var i = 1; i < arguments.length; i++) {
3617
- var source = arguments[i];
3618
- for (var key in source) {
3619
- if (Object.prototype.hasOwnProperty.call(source, key)) {
3620
- target[key] = source[key];
3621
- }
3622
- }
3623
- }
3624
- return target;
3625
- };
3626
- return _extends4.apply(this, arguments);
3627
- }
3628
- __name(_extends4, "_extends");
3629
- var MutationButton = /* @__PURE__ */ __name((props) => {
3630
- const Components2 = useVulcanComponents();
3631
- const [loading, setLoading] = useState(false);
3632
- const [error1, setError] = useState();
3633
- const {
3634
- mutation,
3635
- loadingButtonProps = {},
3636
- label
3637
- } = props;
3638
- let { mutationArguments } = props;
3639
- const mutationAsNode = typeof mutation === "string" ? lib_default`
3640
- ${mutation}
3641
- ` : mutation;
3642
- const [mutationFunc] = useMutation(mutationAsNode);
3643
- const handleClick = /* @__PURE__ */ __name(async (e) => {
3644
- e.preventDefault();
3645
- setLoading(true);
3646
- setError(void 0);
3647
- const {
3648
- submitCallback,
3649
- successCallback,
3650
- errorCallback
3651
- } = props;
3652
- try {
3653
- if (submitCallback) {
3654
- const callbackReturn = await submitCallback(mutationArguments);
3655
- if (callbackReturn && callbackReturn.mutationArguments) {
3656
- mutationArguments = callbackReturn.mutationArguments;
3657
- }
3658
- }
3659
- const result = await mutationFunc({
3660
- variables: mutationArguments
3661
- });
3662
- if (successCallback) {
3663
- await successCallback(result);
3664
- }
3665
- } catch (error) {
3666
- setError(error);
3667
- if (errorCallback) {
3668
- await errorCallback(error);
3669
- }
3670
- } finally {
3671
- setLoading(false);
3672
- }
3673
- }, "handleClick");
3674
- const loadingButton = /* @__PURE__ */ React8.createElement(Components2.LoadingButton, _extends4({
3675
- loading,
3676
- onClick: handleClick,
3677
- label
3678
- }, loadingButtonProps));
3679
- if (error1) {
3680
- return /* @__PURE__ */ React8.createElement(Components2.TooltipTrigger, {
3681
- trigger: loadingButton,
3682
- defaultShow: true
3683
- }, error1.message.replace("GraphQL error: ", ""));
3913
+ experimentalFragmentVariables,
3914
+ allowLegacyFragmentVariables: experimentalFragmentVariables
3915
+ });
3916
+ if (!parsed || parsed.kind !== "Document") {
3917
+ throw new Error("Not a valid GraphQL document.");
3918
+ }
3919
+ docCache.set(cacheKey, stripLoc(processFragments(parsed)));
3684
3920
  }
3685
- return loadingButton;
3686
- }, "MutationButton");
3687
-
3688
- // components/form/core/index.ts
3689
- init_esm_shims();
3690
-
3691
- // components/form/core/FormContainer.tsx
3692
- init_esm_shims();
3693
- import React11 from "react";
3921
+ return docCache.get(cacheKey);
3922
+ }
3923
+ __name(parseDocument, "parseDocument");
3924
+ function gql(literals) {
3925
+ var args = [];
3926
+ for (var _i = 1; _i < arguments.length; _i++) {
3927
+ args[_i - 1] = arguments[_i];
3928
+ }
3929
+ if (typeof literals === "string") {
3930
+ literals = [
3931
+ literals
3932
+ ];
3933
+ }
3934
+ var result = literals[0];
3935
+ args.forEach(function(arg, i) {
3936
+ if (arg && arg.kind === "Document") {
3937
+ result += arg.loc.source.body;
3938
+ } else {
3939
+ result += arg;
3940
+ }
3941
+ result += literals[i + 1];
3942
+ });
3943
+ return parseDocument(result);
3944
+ }
3945
+ __name(gql, "gql");
3946
+ function resetCaches() {
3947
+ docCache.clear();
3948
+ fragmentSourceMap.clear();
3949
+ }
3950
+ __name(resetCaches, "resetCaches");
3951
+ function disableFragmentWarnings() {
3952
+ printFragmentWarnings = false;
3953
+ }
3954
+ __name(disableFragmentWarnings, "disableFragmentWarnings");
3955
+ function enableExperimentalFragmentVariables() {
3956
+ experimentalFragmentVariables = true;
3957
+ }
3958
+ __name(enableExperimentalFragmentVariables, "enableExperimentalFragmentVariables");
3959
+ function disableExperimentalFragmentVariables() {
3960
+ experimentalFragmentVariables = false;
3961
+ }
3962
+ __name(disableExperimentalFragmentVariables, "disableExperimentalFragmentVariables");
3963
+ var extras = {
3964
+ gql,
3965
+ resetCaches,
3966
+ disableFragmentWarnings,
3967
+ enableExperimentalFragmentVariables,
3968
+ disableExperimentalFragmentVariables
3969
+ };
3970
+ (function(gql_1) {
3971
+ gql_1.gql = extras.gql, gql_1.resetCaches = extras.resetCaches, gql_1.disableFragmentWarnings = extras.disableFragmentWarnings, gql_1.enableExperimentalFragmentVariables = extras.enableExperimentalFragmentVariables, gql_1.disableExperimentalFragmentVariables = extras.disableExperimentalFragmentVariables;
3972
+ })(gql || (gql = {}));
3973
+ gql["default"] = gql;
3974
+ var lib_default = gql;
3694
3975
 
3695
3976
  // components/form/utils/formFragments.ts
3696
3977
  init_esm_shims();
@@ -3699,7 +3980,7 @@ import _intersection from "lodash/intersection.js";
3699
3980
  import { getCreateableFields, getUpdateableFields, getFragmentFieldNames } from "@vulcanjs/schema";
3700
3981
  import { getFieldFragment } from "@vulcanjs/graphql";
3701
3982
  import { capitalize as capitalize2 } from "@vulcanjs/utils";
3702
- import compact2 from "lodash/compact.js";
3983
+ import compact3 from "lodash/compact.js";
3703
3984
  var intlSuffix = "_intl";
3704
3985
  var getFragmentName = /* @__PURE__ */ __name((formType, multiTypeName, fragmentType) => [
3705
3986
  multiTypeName,
@@ -3815,7 +4096,7 @@ var getFormFragments = /* @__PURE__ */ __name(({ formType = "new", model, fields
3815
4096
  throw new Error(`Model ${model.name} with fields ${mutationFieldNames} yield an empty mutation fragment.`);
3816
4097
  }
3817
4098
  const generatedMutationFragment = lib_default(mutationFragmentText);
3818
- const extraQueries = compact2(getQueryFieldNames({
4099
+ const extraQueries = compact3(getQueryFieldNames({
3819
4100
  schema,
3820
4101
  options: {
3821
4102
  formType
@@ -3838,7 +4119,7 @@ var formFragments_default = getFormFragments;
3838
4119
  import { getFragmentName as getFragmentName2 } from "@vulcanjs/graphql";
3839
4120
  import { capitalize as capitalize3 } from "@vulcanjs/utils";
3840
4121
  import { useSingle, useCreate, useUpdate, useDelete } from "@vulcanjs/react-hooks";
3841
- import { debugVulcan as debugVulcan2 } from "@vulcanjs/utils";
4122
+ import { debugVulcan } from "@vulcanjs/utils";
3842
4123
 
3843
4124
  // components/VulcanCurrentUser/index.ts
3844
4125
  init_esm_shims();
@@ -3849,209 +4130,21 @@ import { useContext as useContext3 } from "react";
3849
4130
 
3850
4131
  // components/VulcanCurrentUser/Context.ts
3851
4132
  init_esm_shims();
3852
- import React9 from "react";
3853
- var VulcanCurrentUserContext = React9.createContext({
3854
- currentUser: null,
3855
- loading: false
3856
- });
3857
-
3858
- // components/VulcanCurrentUser/Consumer.tsx
3859
- var VulcanCurrentUserConsumer = VulcanCurrentUserContext.Consumer;
3860
- var useVulcanCurrentUser = /* @__PURE__ */ __name(() => useContext3(VulcanCurrentUserContext), "useVulcanCurrentUser");
3861
-
3862
- // components/VulcanCurrentUser/Provider.tsx
3863
- init_esm_shims();
3864
- import React10 from "react";
3865
- function _extends5() {
3866
- _extends5 = Object.assign || function(target) {
3867
- for (var i = 1; i < arguments.length; i++) {
3868
- var source = arguments[i];
3869
- for (var key in source) {
3870
- if (Object.prototype.hasOwnProperty.call(source, key)) {
3871
- target[key] = source[key];
3872
- }
3873
- }
3874
- }
3875
- return target;
3876
- };
3877
- return _extends5.apply(this, arguments);
3878
- }
3879
- __name(_extends5, "_extends");
3880
- var VulcanCurrentUserProvider = /* @__PURE__ */ __name((_a) => {
3881
- var _b = _a, { value } = _b, props = __objRest(_b, ["value"]);
3882
- return /* @__PURE__ */ React10.createElement(VulcanCurrentUserContext.Provider, _extends5({
3883
- value
3884
- }, props));
3885
- }, "VulcanCurrentUserProvider");
3886
-
3887
- // components/form/core/FormContainer.tsx
3888
- function _extends6() {
3889
- _extends6 = Object.assign || function(target) {
3890
- for (var i = 1; i < arguments.length; i++) {
3891
- var source = arguments[i];
3892
- for (var key in source) {
3893
- if (Object.prototype.hasOwnProperty.call(source, key)) {
3894
- target[key] = source[key];
3895
- }
3896
- }
3897
- }
3898
- return target;
3899
- };
3900
- return _extends6.apply(this, arguments);
3901
- }
3902
- __name(_extends6, "_extends");
3903
- var debugForm = debugVulcan2("form");
3904
- var useFragments = /* @__PURE__ */ __name((props, formType) => {
3905
- let queryFragment;
3906
- let queryFragmentName;
3907
- let mutationFragment;
3908
- let mutationFragmentName;
3909
- if (props.queryFragment) {
3910
- if (typeof props.queryFragment === "string") {
3911
- queryFragment = lib_default`
3912
- ${props.queryFragment}
3913
- `;
3914
- if (!props.queryFragmentName)
3915
- throw new Error("When using a string queryFragment, queryFragmentName is mandatory");
3916
- queryFragmentName = props.queryFragmentName;
3917
- } else {
3918
- queryFragment = props.queryFragment;
3919
- queryFragmentName = props.queryFragmentName || getFragmentName2(props.queryFragment);
3920
- }
3921
- }
3922
- if (props.mutationFragment) {
3923
- if (typeof props.mutationFragment === "string") {
3924
- mutationFragment = lib_default`
3925
- ${props.mutationFragment}
3926
- `;
3927
- if (!props.mutationFragmentName)
3928
- throw new Error("When using a string mutationFragment, mutationFragmentName is mandatory");
3929
- queryFragmentName = props.mutationFragmentName;
3930
- } else {
3931
- mutationFragment = props.mutationFragment;
3932
- mutationFragmentName = props.mutationFragmentName || getFragmentName2(props.mutationFragment);
3933
- }
3934
- }
3935
- let autoFormFragments;
3936
- if (!props.queryFragment || !props.mutationFragment) {
3937
- const { model, fields, addFields } = props;
3938
- autoFormFragments = formFragments_default({
3939
- formType,
3940
- model,
3941
- fields,
3942
- addFields
3943
- });
3944
- }
3945
- if (!props.queryFragment) {
3946
- queryFragment = autoFormFragments.queryFragment;
3947
- queryFragmentName = autoFormFragments.queryFragmentName;
3948
- }
3949
- if (!props.mutationFragment) {
3950
- mutationFragment = autoFormFragments.mutationFragment;
3951
- mutationFragmentName = autoFormFragments.mutationFragmentName;
3952
- }
3953
- return {
3954
- mutationFragment,
3955
- mutationFragmentName,
3956
- queryFragment,
3957
- queryFragmentName
3958
- };
3959
- }, "useFragments");
3960
- var FormContainer = /* @__PURE__ */ __name((props) => {
3961
- const { model, documentId, slug, fields, addFields, currentUser: currentUserFromProps, loadingCurrentUser: loadingCurrentUserFromProps } = props;
3962
- const { schema } = model;
3963
- const isEdit = documentId || slug;
3964
- const selector = {
3965
- documentId,
3966
- slug
3967
- };
3968
- const formType = isEdit ? "edit" : "new";
3969
- const VulcanComponents = useVulcanComponents();
3970
- const prefix = `${model.name}${capitalize3(formType)}`;
3971
- const childProps = {
3972
- formType,
3973
- schema
3974
- };
3975
- const { mutationFragment, mutationFragmentName, queryFragment, queryFragmentName } = useFragments(props, formType);
3976
- const mutationOptions = {
3977
- model,
3978
- fragment: mutationFragment,
3979
- fragmentName: mutationFragmentName
3980
- };
3981
- const queryOptions = {
3982
- model,
3983
- fragment: queryFragment,
3984
- fragmentName: queryFragmentName,
3985
- input: {
3986
- id: documentId,
3987
- enableCache: false
3988
- },
3989
- queryOptions: {
3990
- fetchPolicy: "network-only",
3991
- pollInterval: 0,
3992
- skip: formType === "new"
3993
- }
3994
- };
3995
- const { data, document: document1, loading, refetch } = useSingle(queryOptions);
3996
- if (formType !== "new") {
3997
- debugForm("useSingle result", "data", data, "document", document1, "loading", loading);
3998
- }
3999
- const [createDocument] = useCreate(mutationOptions);
4000
- const [updateDocument] = useUpdate(mutationOptions);
4001
- const [deleteDocument] = useDelete(mutationOptions);
4002
- const { currentUser: currentUserFromContext, loading: loadingCurrentUserFromContext } = useVulcanCurrentUser();
4003
- const shouldGetCurrentUserFromProps = typeof currentUserFromProps !== "undefined";
4004
- const currentUser = shouldGetCurrentUserFromProps ? currentUserFromProps : currentUserFromContext;
4005
- const loadingCurrentUser = shouldGetCurrentUserFromProps ? loadingCurrentUserFromProps : loadingCurrentUserFromContext;
4006
- const createAndReturnDocument = /* @__PURE__ */ __name(async (variables) => {
4007
- const result = await createDocument(variables);
4008
- const { errors, document } = result;
4009
- return {
4010
- document,
4011
- errors
4012
- };
4013
- }, "createAndReturnDocument");
4014
- const updateAndReturnDocument = /* @__PURE__ */ __name(async (variables) => {
4015
- const result = await updateDocument(variables);
4016
- const { errors, document } = result;
4017
- return {
4018
- document,
4019
- errors
4020
- };
4021
- }, "updateAndReturnDocument");
4022
- const deleteDocumentAndRefetch = /* @__PURE__ */ __name(async (variables) => {
4023
- await deleteDocument(variables);
4024
- }, "deleteDocumentAndRefetch");
4025
- if (isEdit && loading) {
4026
- return /* @__PURE__ */ React11.createElement(VulcanComponents.Loading, null);
4027
- }
4028
- return /* @__PURE__ */ React11.createElement(VulcanComponents.Form, _extends6({
4029
- document: document1,
4030
- loading: loading || loadingCurrentUser,
4031
- createDocument: createAndReturnDocument,
4032
- updateDocument: updateAndReturnDocument,
4033
- deleteDocument: deleteDocumentAndRefetch,
4034
- refetch,
4035
- currentUser
4036
- }, childProps, props));
4037
- }, "FormContainer");
4038
- var SmartForm = FormContainer;
4133
+ import React6 from "react";
4134
+ var VulcanCurrentUserContext = React6.createContext({
4135
+ currentUser: null,
4136
+ loading: false
4137
+ });
4039
4138
 
4040
- // components/VulcanComponents/defaultVulcanComponents/index.ts
4041
- var defaultFormComponents = {
4042
- Form,
4043
- SmartForm: FormContainer
4044
- };
4045
- var defaultDatatableComponents = {};
4046
- var defaultCellComponents = {};
4047
- var defaultCoreComponents = {
4048
- MutationButton,
4049
- LoadingButton
4050
- };
4139
+ // components/VulcanCurrentUser/Consumer.tsx
4140
+ var VulcanCurrentUserConsumer = VulcanCurrentUserContext.Consumer;
4141
+ var useVulcanCurrentUser = /* @__PURE__ */ __name(() => useContext3(VulcanCurrentUserContext), "useVulcanCurrentUser");
4051
4142
 
4052
- // components/form/core/Form/Form.tsx
4053
- function _extends7() {
4054
- _extends7 = Object.assign || function(target) {
4143
+ // components/VulcanCurrentUser/Provider.tsx
4144
+ init_esm_shims();
4145
+ import React7 from "react";
4146
+ function _extends3() {
4147
+ _extends3 = Object.assign || function(target) {
4055
4148
  for (var i = 1; i < arguments.length; i++) {
4056
4149
  var source = arguments[i];
4057
4150
  for (var key in source) {
@@ -4062,459 +4155,364 @@ function _extends7() {
4062
4155
  }
4063
4156
  return target;
4064
4157
  };
4065
- return _extends7.apply(this, arguments);
4158
+ return _extends3.apply(this, arguments);
4066
4159
  }
4067
- __name(_extends7, "_extends");
4068
- var compactParent = /* @__PURE__ */ __name((object, path) => {
4069
- const parentPath = getParentPath(path);
4070
- const compactIfArray = /* @__PURE__ */ __name((x) => Array.isArray(x) ? compact3(x) : x, "compactIfArray");
4071
- update(object, parentPath, compactIfArray);
4072
- }, "compactParent");
4073
- var getDefaultValues = /* @__PURE__ */ __name((convertedSchema) => {
4074
- return pickBy(mapValues(convertedSchema, (field) => field.defaultValue), (value) => value);
4075
- }, "getDefaultValues");
4076
- var compactObject = /* @__PURE__ */ __name((o) => omitBy(o, (f) => f === null || f === void 0), "compactObject");
4077
- var getInitialStateFromProps = /* @__PURE__ */ __name((nextProps) => {
4078
- const schema = nextProps.schema || nextProps.model.schema;
4079
- const convertedSchema = convertSchema(schema);
4080
- const formType = nextProps.document ? "edit" : "new";
4081
- const defaultValues = formType === "new" ? getDefaultValues(convertedSchema) : {};
4082
- const initialDocument = merge2({}, defaultValues, nextProps.prefilledProps, compactObject(nextProps.document));
4083
- Object.keys(convertedSchema).forEach((key) => {
4084
- let minCount = convertedSchema[key].minCount;
4085
- if (minCount) {
4086
- initialDocument[key] = initialDocument[key] || [];
4087
- while (initialDocument[key].length < minCount)
4088
- initialDocument[key].push({});
4089
- }
4090
- });
4091
- removeProperty(initialDocument, "__typename");
4092
- return {
4093
- disabled: nextProps.disabled,
4094
- errors: [],
4095
- deletedValues: [],
4096
- currentValues: {},
4097
- originalSchema: convertSchema(schema, {
4098
- removeArrays: false
4099
- }),
4100
- schema: convertedSchema,
4101
- flatSchema: convertSchema(schema, {
4102
- flatten: true
4103
- }),
4104
- initialDocument,
4105
- currentDocument: initialDocument
4106
- };
4107
- }, "getInitialStateFromProps");
4108
- var getChildrenProps = /* @__PURE__ */ __name((props, state, options, callbacks) => {
4109
- const { currentUser, repeatErrors, submitLabel, cancelLabel, revertLabel, cancelCallback, revertCallback, id, model, prefilledProps, itemProperties, contextName, showRemove, showDelete } = props;
4110
- const { disabled, currentDocument } = state;
4111
- const { formType } = options;
4112
- const { deleteDocument } = callbacks;
4113
- const commonProps = {
4114
- document: currentDocument,
4115
- formType,
4116
- currentUser,
4117
- disabled,
4118
- prefilledProps,
4119
- itemProperties,
4120
- contextName
4121
- };
4122
- const docClassName = `document-${formType}`;
4123
- const modelName = model.name.toLowerCase();
4124
- const formProps = {
4125
- className: `${docClassName} ${docClassName}-${modelName}`,
4126
- id
4127
- };
4128
- const formGroupProps = /* @__PURE__ */ __name((group) => __spreadValues(__spreadProps(__spreadValues({
4129
- key: group.name
4130
- }, group), {
4131
- group: omit(group, [
4132
- "fields"
4133
- ])
4134
- }), commonProps), "formGroupProps");
4135
- const formSubmitProps = {
4136
- model,
4137
- currentUser,
4138
- submitLabel,
4139
- cancelLabel,
4140
- revertLabel,
4141
- cancelCallback,
4142
- revertCallback,
4143
- document: currentDocument,
4144
- deleteDocument: formType === "edit" && showRemove && showDelete && deleteDocument || null
4145
- };
4146
- const formLayoutProps = {
4147
- formProps,
4148
- repeatErrors,
4149
- submitProps: formSubmitProps,
4150
- commonProps
4151
- };
4152
- return {
4153
- commonProps,
4154
- formSubmitProps,
4155
- formGroupProps,
4156
- formLayoutProps
4157
- };
4158
- }, "getChildrenProps");
4159
- var FormWarnUnsaved = /* @__PURE__ */ __name(({ isChanged, warnUnsavedChanges, children }) => {
4160
- useWarnOnUnsaved({
4161
- isChanged,
4162
- warnUnsavedChanges
4163
- });
4164
- return /* @__PURE__ */ React12.createElement(React12.Fragment, null, children);
4165
- }, "FormWarnUnsaved");
4166
- var getData = /* @__PURE__ */ __name((customArgs, props, state, { submitFormCallbacks, form }) => {
4167
- const { currentDocument } = state;
4168
- const { model, prefilledProps } = props;
4169
- let data = prefilledProps || {};
4170
- data = omitBy(data, (value, key) => key.endsWith(".$"));
4171
- const args = __spreadValues({
4172
- schema: model.schema,
4173
- excludeRemovedFields: false,
4174
- excludeHiddenFields: false,
4175
- replaceIntlFields: true,
4176
- addExtraFields: false
4177
- }, customArgs);
4178
- const fields = getFieldNames(props, currentDocument, args);
4179
- data = __spreadValues(__spreadValues({}, data), pick2(currentDocument, ...fields));
4180
- state.deletedValues.forEach((path) => {
4181
- if (path.includes(".")) {
4182
- compactParent(data, path);
4183
- }
4184
- });
4185
- data = runCallbacks({
4186
- callbacks: submitFormCallbacks,
4187
- iterator: data,
4188
- args: [
4189
- {}
4190
- ]
4191
- });
4192
- return data;
4193
- }, "getData");
4194
- var Form = /* @__PURE__ */ __name((props) => {
4195
- const { initCallback, createDocument, updateDocument, deleteDocument } = props;
4196
- const initialState = getInitialStateFromProps(props);
4197
- const { schema, originalSchema, flatSchema, initialDocument } = initialState;
4198
- const isFirstRender = useRef2(true);
4199
- useEffect2(() => {
4200
- if (isFirstRender.current) {
4201
- isFirstRender.current = false;
4202
- return;
4203
- }
4204
- if (initCallback)
4205
- initCallback(initialState.currentDocument);
4206
- }, [
4207
- initCallback
4208
- ]);
4209
- const defaultProps = {
4210
- layout: "horizontal",
4211
- prefilledProps: {},
4212
- repeatErrors: false,
4213
- showRemove: true,
4214
- showDelete: true
4215
- };
4216
- const allProps = __spreadValues(__spreadValues({}, defaultProps), props);
4217
- const defaultValues = {};
4218
- const submitFormCallbacks = [];
4219
- const successFormCallbacks = [];
4220
- const failureFormCallbacks = [];
4221
- const intl = useIntlContext2();
4222
- const [errors, setErrors] = useState2([]);
4223
- const throwError = /* @__PURE__ */ __name((error) => {
4224
- let formErrors = getErrors(error);
4225
- console.log(formErrors);
4226
- setErrors((prevErrors) => [
4227
- ...prevErrors,
4228
- ...formErrors
4229
- ]);
4230
- }, "throwError");
4231
- const clearFieldErrors = /* @__PURE__ */ __name((path) => {
4232
- setErrors((prevErrors) => prevErrors.filter((error) => error.path !== path));
4233
- }, "clearFieldErrors");
4234
- const [deletedValues, setDeletedValues] = useState2([]);
4235
- const addToDeletedValues = /* @__PURE__ */ __name((name) => {
4236
- setDeletedValues((prevDeletedValues) => [
4237
- ...prevDeletedValues,
4238
- name
4239
- ]);
4240
- }, "addToDeletedValues");
4241
- const [callbacks, setCallbacks] = useState2({
4242
- submitFormCallbacks: [],
4243
- successFormCallbacks: [],
4244
- failureFormCallbacks: []
4245
- });
4246
- const addToSubmitForm = /* @__PURE__ */ __name((callback) => {
4247
- setCallbacks((cbs) => __spreadValues({}, cbs));
4248
- }, "addToSubmitForm");
4249
- const addToSuccessForm = /* @__PURE__ */ __name((callback) => {
4250
- setCallbacks((cbs) => __spreadProps(__spreadValues({}, cbs), {
4251
- successFormCallbacks: [
4252
- ...cbs.successFormCallbacks,
4253
- callback
4254
- ]
4255
- }));
4256
- }, "addToSuccessForm");
4257
- const addToFailureForm = /* @__PURE__ */ __name((callback) => {
4258
- setCallbacks((cbs) => __spreadProps(__spreadValues({}, cbs), {
4259
- failureFormCallbacks: [
4260
- ...cbs.failureFormCallbacks,
4261
- callback
4262
- ]
4263
- }));
4264
- }, "addToFailureForm");
4265
- const clearFormCallbacks = /* @__PURE__ */ __name(() => {
4266
- setCallbacks({
4267
- submitFormCallbacks: [],
4268
- successFormCallbacks: [],
4269
- failureFormCallbacks: []
4270
- });
4271
- }, "clearFormCallbacks");
4272
- const [currentValues, setCurrentValues] = useState2({});
4273
- const submitFormContext = /* @__PURE__ */ __name((formType) => (event) => {
4274
- submitForm(formType)(event);
4275
- }, "submitFormContext");
4276
- const [currentDocument, setCurrentDocument] = useState2(initialDocument);
4277
- const updateCurrentValues = /* @__PURE__ */ __name((newValues, options = {}) => {
4278
- const { mode = "overwrite" } = options;
4279
- const { changeCallback } = props;
4280
- const newState = {
4281
- currentValues: cloneDeep(currentValues),
4282
- currentDocument: cloneDeep(currentDocument),
4283
- deletedValues: cloneDeep(deletedValues)
4284
- };
4285
- Object.keys(newValues).forEach((key) => {
4286
- const path = key;
4287
- let value = newValues[key];
4288
- if (isEmptyValue(value)) {
4289
- unset(newState.currentValues, path);
4290
- set2(newState.currentDocument, path, null);
4291
- newState.deletedValues = [
4292
- ...newState.deletedValues,
4293
- path
4294
- ];
4295
- } else {
4296
- set2(newState.currentValues, path, value);
4297
- if (mode === "merge" && (Array.isArray(value) || isObject(value))) {
4298
- const oldValue = get2(newState.currentDocument, path);
4299
- set2(newState.currentDocument, path, merge2(oldValue, value));
4300
- } else {
4301
- set2(newState.currentDocument, path, value);
4160
+ __name(_extends3, "_extends");
4161
+ var VulcanCurrentUserProvider = /* @__PURE__ */ __name((_a) => {
4162
+ var _b = _a, { value } = _b, props = __objRest(_b, ["value"]);
4163
+ return /* @__PURE__ */ React7.createElement(VulcanCurrentUserContext.Provider, _extends3({
4164
+ value
4165
+ }, props));
4166
+ }, "VulcanCurrentUserProvider");
4167
+
4168
+ // components/form/core/FormContainer.tsx
4169
+ function _extends4() {
4170
+ _extends4 = Object.assign || function(target) {
4171
+ for (var i = 1; i < arguments.length; i++) {
4172
+ var source = arguments[i];
4173
+ for (var key in source) {
4174
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
4175
+ target[key] = source[key];
4302
4176
  }
4303
- newState.deletedValues = without(newState.deletedValues, path);
4304
4177
  }
4305
- });
4306
- if (changeCallback)
4307
- changeCallback(newState.currentDocument);
4308
- setCurrentValues(newState.currentValues);
4309
- setCurrentDocument(newState.currentDocument);
4310
- setDeletedValues(newState.deletedValues);
4311
- }, "updateCurrentValues");
4312
- const refetchForm = /* @__PURE__ */ __name(() => {
4313
- if (props.refetch) {
4314
- props.refetch();
4315
- }
4316
- }, "refetchForm");
4317
- const [disabled, setDisabled] = useState2(false);
4318
- const [success, setSuccess] = useState2(false);
4319
- const clearForm = /* @__PURE__ */ __name((options = {}) => {
4320
- const { document: optionsDocument } = options;
4321
- const document = optionsDocument ? merge2({}, props.prefilledProps, optionsDocument) : null;
4322
- setErrors([]);
4323
- setCurrentValues({});
4324
- setDeletedValues([]);
4325
- setCurrentDocument(document || initialDocument);
4326
- setDisabled(false);
4327
- }, "clearForm");
4328
- const newMutationSuccessCallback = /* @__PURE__ */ __name(function(result) {
4329
- mutationSuccessCallback(result, "new");
4330
- }, "newMutationSuccessCallback");
4331
- const editMutationSuccessCallback = /* @__PURE__ */ __name(function(result) {
4332
- mutationSuccessCallback(result, "edit");
4333
- }, "editMutationSuccessCallback");
4334
- const formRef = useRef2(null);
4335
- const mutationSuccessCallback = /* @__PURE__ */ __name(function(result, mutationType) {
4336
- setDisabled(false);
4337
- setSuccess(true);
4338
- if (mutationType === "new")
4339
- refetchForm();
4340
- let { document } = result;
4341
- if (formRef.current) {
4342
- clearForm({
4343
- document: mutationType === "edit" ? document : void 0
4344
- });
4345
4178
  }
4346
- document = runCallbacks({
4347
- callbacks: successFormCallbacks,
4348
- iterator: document,
4349
- args: [
4350
- {
4351
- form: formRef.current
4352
- }
4353
- ]
4354
- });
4355
- if (props.successCallback)
4356
- props.successCallback(document, {
4357
- form: this
4358
- });
4359
- }, "mutationSuccessCallback");
4360
- const mutationErrorCallback = /* @__PURE__ */ __name((document, error) => {
4361
- setDisabled(false);
4362
- console.error("// graphQL Error");
4363
- console.error(error);
4364
- runCallbacks({
4365
- callbacks: failureFormCallbacks,
4366
- iterator: error,
4367
- args: [
4368
- {
4369
- error,
4370
- form: formRef.current
4371
- }
4372
- ]
4373
- });
4374
- if (!isEmpty(error)) {
4375
- throwError(error);
4179
+ return target;
4180
+ };
4181
+ return _extends4.apply(this, arguments);
4182
+ }
4183
+ __name(_extends4, "_extends");
4184
+ var debugForm = debugVulcan("form");
4185
+ var useFragments = /* @__PURE__ */ __name((props, formType) => {
4186
+ let queryFragment;
4187
+ let queryFragmentName;
4188
+ let mutationFragment;
4189
+ let mutationFragmentName;
4190
+ if (props.queryFragment) {
4191
+ if (typeof props.queryFragment === "string") {
4192
+ queryFragment = lib_default`
4193
+ ${props.queryFragment}
4194
+ `;
4195
+ if (!props.queryFragmentName)
4196
+ throw new Error("When using a string queryFragment, queryFragmentName is mandatory");
4197
+ queryFragmentName = props.queryFragmentName;
4198
+ } else {
4199
+ queryFragment = props.queryFragment;
4200
+ queryFragmentName = props.queryFragmentName || getFragmentName2(props.queryFragment);
4376
4201
  }
4377
- if (props.errorCallback)
4378
- props.errorCallback(document, error, {
4379
- form: void 0
4380
- });
4381
- }, "mutationErrorCallback");
4382
- const submitForm = /* @__PURE__ */ __name((formType) => async (event) => {
4383
- var _a, _b;
4384
- event && event.preventDefault();
4385
- event && event.stopPropagation();
4386
- const { contextName } = props;
4387
- if (disabled) {
4388
- return;
4202
+ }
4203
+ if (props.mutationFragment) {
4204
+ if (typeof props.mutationFragment === "string") {
4205
+ mutationFragment = lib_default`
4206
+ ${props.mutationFragment}
4207
+ `;
4208
+ if (!props.mutationFragmentName)
4209
+ throw new Error("When using a string mutationFragment, mutationFragmentName is mandatory");
4210
+ queryFragmentName = props.mutationFragmentName;
4211
+ } else {
4212
+ mutationFragment = props.mutationFragment;
4213
+ mutationFragmentName = props.mutationFragmentName || getFragmentName2(props.mutationFragment);
4389
4214
  }
4390
- setErrors([]);
4391
- setDisabled(true);
4392
- let data = getData({
4393
- replaceIntlFields: true,
4394
- addExtraFields: false,
4395
- mutableFields
4396
- }, props, {
4397
- currentDocument,
4398
- deletedValues
4399
- }, {
4400
- form: formRef.current,
4401
- submitFormCallbacks
4215
+ }
4216
+ let autoFormFragments;
4217
+ if (!props.queryFragment || !props.mutationFragment) {
4218
+ const { model, fields, addFields } = props;
4219
+ autoFormFragments = formFragments_default({
4220
+ formType,
4221
+ model,
4222
+ fields,
4223
+ addFields
4402
4224
  });
4403
- if (props.submitCallback) {
4404
- data = props.submitCallback(data) || data;
4225
+ }
4226
+ if (!props.queryFragment) {
4227
+ queryFragment = autoFormFragments.queryFragment;
4228
+ queryFragmentName = autoFormFragments.queryFragmentName;
4229
+ }
4230
+ if (!props.mutationFragment) {
4231
+ mutationFragment = autoFormFragments.mutationFragment;
4232
+ mutationFragmentName = autoFormFragments.mutationFragmentName;
4233
+ }
4234
+ return {
4235
+ mutationFragment,
4236
+ mutationFragmentName,
4237
+ queryFragment,
4238
+ queryFragmentName
4239
+ };
4240
+ }, "useFragments");
4241
+ var FormContainer = /* @__PURE__ */ __name((props) => {
4242
+ const { model, documentId, slug, fields, addFields, currentUser: currentUserFromProps, loadingCurrentUser: loadingCurrentUserFromProps } = props;
4243
+ const { schema } = model;
4244
+ const isEdit = documentId || slug;
4245
+ const selector = {
4246
+ documentId,
4247
+ slug
4248
+ };
4249
+ const formType = isEdit ? "edit" : "new";
4250
+ const VulcanComponents = useVulcanComponents();
4251
+ const prefix = `${model.name}${capitalize3(formType)}`;
4252
+ const childProps = {
4253
+ formType,
4254
+ schema
4255
+ };
4256
+ const { mutationFragment, mutationFragmentName, queryFragment, queryFragmentName } = useFragments(props, formType);
4257
+ const mutationOptions = {
4258
+ model,
4259
+ fragment: mutationFragment,
4260
+ fragmentName: mutationFragmentName
4261
+ };
4262
+ const queryOptions = {
4263
+ model,
4264
+ fragment: queryFragment,
4265
+ fragmentName: queryFragmentName,
4266
+ input: {
4267
+ id: documentId,
4268
+ enableCache: false
4269
+ },
4270
+ queryOptions: {
4271
+ fetchPolicy: "network-only",
4272
+ pollInterval: 0,
4273
+ skip: formType === "new"
4405
4274
  }
4406
- if (formType === "new") {
4407
- try {
4408
- const result = await createDocument({
4409
- input: {
4410
- data,
4411
- contextName
4412
- }
4413
- });
4414
- if ((_a = result.errors) == null ? void 0 : _a.length) {
4415
- mutationErrorCallback(document1, result.errors[0]);
4416
- } else {
4417
- newMutationSuccessCallback(result);
4275
+ };
4276
+ const { data, document: document1, loading, refetch } = useSingle(queryOptions);
4277
+ if (formType !== "new") {
4278
+ debugForm("useSingle result", "data", data, "document", document1, "loading", loading);
4279
+ }
4280
+ const [createDocument] = useCreate(mutationOptions);
4281
+ const [updateDocument] = useUpdate(mutationOptions);
4282
+ const [deleteDocument] = useDelete(mutationOptions);
4283
+ const { currentUser: currentUserFromContext, loading: loadingCurrentUserFromContext } = useVulcanCurrentUser();
4284
+ const shouldGetCurrentUserFromProps = typeof currentUserFromProps !== "undefined";
4285
+ const currentUser = shouldGetCurrentUserFromProps ? currentUserFromProps : currentUserFromContext;
4286
+ const loadingCurrentUser = shouldGetCurrentUserFromProps ? loadingCurrentUserFromProps : loadingCurrentUserFromContext;
4287
+ const createAndReturnDocument = /* @__PURE__ */ __name(async (variables) => {
4288
+ const result = await createDocument(variables);
4289
+ const { errors, document } = result;
4290
+ return {
4291
+ document,
4292
+ errors
4293
+ };
4294
+ }, "createAndReturnDocument");
4295
+ const updateAndReturnDocument = /* @__PURE__ */ __name(async (variables) => {
4296
+ const result = await updateDocument(variables);
4297
+ const { errors, document } = result;
4298
+ return {
4299
+ document,
4300
+ errors
4301
+ };
4302
+ }, "updateAndReturnDocument");
4303
+ const deleteDocumentAndRefetch = /* @__PURE__ */ __name(async (variables) => {
4304
+ await deleteDocument(variables);
4305
+ }, "deleteDocumentAndRefetch");
4306
+ if (isEdit && loading) {
4307
+ return /* @__PURE__ */ React8.createElement(VulcanComponents.Loading, null);
4308
+ }
4309
+ return /* @__PURE__ */ React8.createElement(VulcanComponents.Form, _extends4({
4310
+ document: document1,
4311
+ loading: loading || loadingCurrentUser,
4312
+ createDocument: createAndReturnDocument,
4313
+ updateDocument: updateAndReturnDocument,
4314
+ deleteDocument: deleteDocumentAndRefetch,
4315
+ refetch,
4316
+ currentUser
4317
+ }, childProps, props));
4318
+ }, "FormContainer");
4319
+ var SmartForm = FormContainer;
4320
+
4321
+ // components/VulcanComponents/index.ts
4322
+ init_esm_shims();
4323
+
4324
+ // components/VulcanComponents/Provider.tsx
4325
+ init_esm_shims();
4326
+ import React9 from "react";
4327
+ import { debugVulcan as debugVulcan2 } from "@vulcanjs/utils";
4328
+ function _extends5() {
4329
+ _extends5 = Object.assign || function(target) {
4330
+ for (var i = 1; i < arguments.length; i++) {
4331
+ var source = arguments[i];
4332
+ for (var key in source) {
4333
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
4334
+ target[key] = source[key];
4418
4335
  }
4419
- } catch (error) {
4420
- mutationErrorCallback(document1, error);
4421
4336
  }
4337
+ }
4338
+ return target;
4339
+ };
4340
+ return _extends5.apply(this, arguments);
4341
+ }
4342
+ __name(_extends5, "_extends");
4343
+ var debugComponents = debugVulcan2("components");
4344
+ var VulcanComponentsProvider = /* @__PURE__ */ __name((_a) => {
4345
+ var _b = _a, { value } = _b, props = __objRest(_b, ["value"]);
4346
+ const currentComponents = useVulcanComponents();
4347
+ debugComponents("Current components __not_initialized?", currentComponents.__not_initialized);
4348
+ const mergedComponents = __spreadValues(__spreadValues({}, (currentComponents == null ? void 0 : currentComponents.__not_initialized) ? {} : currentComponents || {}), value || {});
4349
+ debugComponents("Merged components", mergedComponents);
4350
+ Object.keys(mergedComponents).forEach((componentName) => {
4351
+ if (mergedComponents[componentName]) {
4352
+ mergedComponents[componentName].displayName = "Vulcan." + componentName;
4422
4353
  } else {
4423
- try {
4424
- const documentId = currentDocument._id;
4425
- const result = await updateDocument({
4426
- input: {
4427
- id: documentId,
4428
- data,
4429
- contextName
4430
- }
4431
- });
4432
- if ((_b = result.errors) == null ? void 0 : _b.length) {
4433
- mutationErrorCallback(document1, result.errors[0]);
4434
- } else {
4435
- editMutationSuccessCallback(result);
4354
+ console.warn(`Encountered an undefined component: ${componentName}.
4355
+ The component may not be registered, or import failed.
4356
+ For instance due to an infinite import loop when importing
4357
+ "useVulcanComponents" from index instead of Consumer.`);
4358
+ }
4359
+ });
4360
+ return /* @__PURE__ */ React9.createElement(VulcanComponentsContext.Provider, _extends5({
4361
+ value: mergedComponents
4362
+ }, props));
4363
+ }, "VulcanComponentsProvider");
4364
+
4365
+ // components/VulcanComponents/typings.ts
4366
+ init_esm_shims();
4367
+ import React10 from "react";
4368
+
4369
+ // components/VulcanComponents/defaultVulcanComponents/index.ts
4370
+ init_esm_shims();
4371
+
4372
+ // components/core/LoadingButton.tsx
4373
+ init_esm_shims();
4374
+ import React11 from "react";
4375
+ function _extends6() {
4376
+ _extends6 = Object.assign || function(target) {
4377
+ for (var i = 1; i < arguments.length; i++) {
4378
+ var source = arguments[i];
4379
+ for (var key in source) {
4380
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
4381
+ target[key] = source[key];
4436
4382
  }
4437
- } catch (error) {
4438
- mutationErrorCallback(document1, error);
4439
4383
  }
4440
4384
  }
4441
- }, "submitForm");
4442
- const deleteDocumentWithConfirm = /* @__PURE__ */ __name(() => {
4443
- const document = currentDocument;
4444
- const documentId = props.document._id;
4445
- const documentTitle = document.title || document.name || "";
4446
- const deleteDocumentConfirm = intl.formatMessage({
4447
- id: "forms.delete_confirm"
4448
- }, {
4449
- title: documentTitle
4450
- });
4451
- if (window.confirm(deleteDocumentConfirm)) {
4452
- deleteDocument({
4453
- input: {
4454
- id: documentId
4385
+ return target;
4386
+ };
4387
+ return _extends6.apply(this, arguments);
4388
+ }
4389
+ __name(_extends6, "_extends");
4390
+ var LoadingButton = /* @__PURE__ */ __name((_a) => {
4391
+ var _b = _a, { loading, label, onClick, children, className = "" } = _b, rest = __objRest(_b, ["loading", "label", "onClick", "children", "className"]);
4392
+ const Components2 = useVulcanComponents();
4393
+ const wrapperStyle = {
4394
+ position: "relative"
4395
+ };
4396
+ const labelStyle = loading ? {
4397
+ opacity: 0.5
4398
+ } : {};
4399
+ const loadingStyle = loading ? {
4400
+ position: "absolute",
4401
+ top: 0,
4402
+ bottom: 0,
4403
+ left: 0,
4404
+ right: 0,
4405
+ display: "flex",
4406
+ justifyContent: "center",
4407
+ alignItems: "center"
4408
+ } : {
4409
+ display: "none"
4410
+ };
4411
+ return /* @__PURE__ */ React11.createElement(Components2.Button, _extends6({
4412
+ className: `loading-button ${loading ? "loading-button-loading" : "loading-button-notloading"} ${className}`,
4413
+ onClick
4414
+ }, rest), /* @__PURE__ */ React11.createElement("span", {
4415
+ style: wrapperStyle
4416
+ }, /* @__PURE__ */ React11.createElement("span", {
4417
+ style: labelStyle
4418
+ }, label || children), /* @__PURE__ */ React11.createElement("span", {
4419
+ style: loadingStyle
4420
+ }, /* @__PURE__ */ React11.createElement(Components2.Loading, null))));
4421
+ }, "LoadingButton");
4422
+
4423
+ // components/core/MutationButton.tsx
4424
+ init_esm_shims();
4425
+ import React12, { useState as useState2 } from "react";
4426
+ import { useMutation } from "@apollo/client";
4427
+ function _extends7() {
4428
+ _extends7 = Object.assign || function(target) {
4429
+ for (var i = 1; i < arguments.length; i++) {
4430
+ var source = arguments[i];
4431
+ for (var key in source) {
4432
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
4433
+ target[key] = source[key];
4455
4434
  }
4456
- }).then((mutationResult) => {
4457
- if (props.removeSuccessCallback)
4458
- props.removeSuccessCallback({
4459
- documentId,
4460
- documentTitle
4461
- });
4462
- refetchForm();
4463
- }).catch((error) => {
4464
- console.log(error);
4465
- });
4435
+ }
4466
4436
  }
4467
- }, "deleteDocumentWithConfirm");
4468
- const { successComponent, document: document1, currentUser, model, warnUnsavedChanges } = props;
4469
- const FormComponents = useVulcanComponents();
4470
- const formType1 = document1 ? "edit" : "new";
4471
- const mutableFields = formType1 === "edit" ? getEditableFields(schema, currentUser, initialDocument) : getInsertableFields(schema, currentUser);
4472
- const { formLayoutProps, formGroupProps } = getChildrenProps(props, {
4473
- disabled,
4474
- currentDocument
4475
- }, {
4476
- formType: formType1
4477
- }, {
4478
- deleteDocument: deleteDocumentWithConfirm
4479
- });
4480
- const isChanged = isNotSameDocument(initialDocument, currentDocument);
4481
- return success && successComponent ? successComponent : /* @__PURE__ */ React12.createElement(FormWarnUnsaved, {
4482
- isChanged,
4483
- warnUnsavedChanges
4484
- }, /* @__PURE__ */ React12.createElement(FormContext.Provider, {
4485
- value: {
4486
- throwError,
4487
- clearForm,
4488
- refetchForm,
4489
- isChanged,
4490
- submitForm: submitFormContext(formType1),
4491
- addToDeletedValues,
4492
- updateCurrentValues,
4493
- getDocument: () => currentDocument,
4494
- getLabel: (fieldName, fieldLocale) => getLabel(model, flatSchema, intl, fieldName, fieldLocale),
4495
- initialDocument,
4496
- addToSubmitForm,
4497
- addToSuccessForm,
4498
- addToFailureForm,
4499
- clearFormCallbacks,
4500
- errors,
4501
- currentValues,
4502
- deletedValues,
4503
- clearFieldErrors
4437
+ return target;
4438
+ };
4439
+ return _extends7.apply(this, arguments);
4440
+ }
4441
+ __name(_extends7, "_extends");
4442
+ var MutationButton = /* @__PURE__ */ __name((props) => {
4443
+ const Components2 = useVulcanComponents();
4444
+ const [loading, setLoading] = useState2(false);
4445
+ const [error1, setError] = useState2();
4446
+ const {
4447
+ mutation,
4448
+ loadingButtonProps = {},
4449
+ label
4450
+ } = props;
4451
+ let { mutationArguments } = props;
4452
+ const mutationAsNode = typeof mutation === "string" ? lib_default`
4453
+ ${mutation}
4454
+ ` : mutation;
4455
+ const [mutationFunc] = useMutation(mutationAsNode);
4456
+ const handleClick = /* @__PURE__ */ __name(async (e) => {
4457
+ e.preventDefault();
4458
+ setLoading(true);
4459
+ setError(void 0);
4460
+ const {
4461
+ submitCallback,
4462
+ successCallback,
4463
+ errorCallback
4464
+ } = props;
4465
+ try {
4466
+ if (submitCallback) {
4467
+ const callbackReturn = await submitCallback(mutationArguments);
4468
+ if (callbackReturn && callbackReturn.mutationArguments) {
4469
+ mutationArguments = callbackReturn.mutationArguments;
4470
+ }
4471
+ }
4472
+ const result = await mutationFunc({
4473
+ variables: mutationArguments
4474
+ });
4475
+ if (successCallback) {
4476
+ await successCallback(result);
4477
+ }
4478
+ } catch (error) {
4479
+ setError(error);
4480
+ if (errorCallback) {
4481
+ await errorCallback(error);
4482
+ }
4483
+ } finally {
4484
+ setLoading(false);
4504
4485
  }
4505
- }, /* @__PURE__ */ React12.createElement(FormComponents.FormLayout, _extends7({}, formLayoutProps), getFieldGroups(props, {
4506
- currentDocument,
4507
- schema,
4508
- flatSchema,
4509
- originalSchema
4510
- }, intl, mutableFields, intl.formatMessage).map((group, i) => /* @__PURE__ */ React12.createElement(FormComponents.FormGroup, _extends7({
4511
- key: i
4512
- }, formGroupProps(group)))))));
4513
- }, "Form");
4486
+ }, "handleClick");
4487
+ const loadingButton = /* @__PURE__ */ React12.createElement(Components2.LoadingButton, _extends7({
4488
+ loading,
4489
+ onClick: handleClick,
4490
+ label
4491
+ }, loadingButtonProps));
4492
+ if (error1) {
4493
+ return /* @__PURE__ */ React12.createElement(Components2.TooltipTrigger, {
4494
+ trigger: loadingButton,
4495
+ defaultShow: true
4496
+ }, error1.message.replace("GraphQL error: ", ""));
4497
+ }
4498
+ return loadingButton;
4499
+ }, "MutationButton");
4514
4500
 
4515
- // components/form/core/Form/typings.ts
4501
+ // components/form/core/index.ts
4516
4502
  init_esm_shims();
4517
4503
 
4504
+ // components/VulcanComponents/defaultVulcanComponents/index.ts
4505
+ var defaultFormComponents = {
4506
+ Form,
4507
+ SmartForm: FormContainer
4508
+ };
4509
+ var defaultDatatableComponents = {};
4510
+ var defaultCellComponents = {};
4511
+ var defaultCoreComponents = {
4512
+ MutationButton,
4513
+ LoadingButton
4514
+ };
4515
+
4518
4516
  // componentsHelpers.tsx
4519
4517
  init_esm_shims();
4520
4518
  import React13 from "react";