@lobb-js/studio 0.20.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/actions.d.ts CHANGED
@@ -7,6 +7,7 @@ export interface OpenDataTableDrawerProps {
7
7
  title?: string;
8
8
  showHeader?: boolean;
9
9
  showFooter?: boolean;
10
+ position?: "side" | "bottom";
10
11
  }
11
12
  export declare function showDialog(title: string, description: string): Promise<boolean>;
12
13
  export declare function openCreateDetailView(studioContext: StudioContext, props: CreateDetailViewProp): void;
@@ -103,24 +103,18 @@ export function getCollectionParamsFields(ctx, collectionName, allFields) {
103
103
  };
104
104
  });
105
105
  var columns = [];
106
- for (var index = 0; index < foreignFields.length; index++) {
107
- var foreignField = foreignFields[index];
108
- if (!foreignField.collection || !ctx.meta.collections[foreignField.collection]) {
106
+ for (var _i = 0, foreignFields_1 = foreignFields; _i < foreignFields_1.length; _i++) {
107
+ var foreignField = foreignFields_1[_i];
108
+ if (!foreignField.collection || !ctx.meta.collections[foreignField.collection])
109
109
  continue;
110
+ if (allFields) {
111
+ columns.push("".concat(foreignField.field, ".*"));
110
112
  }
111
- columns.push("".concat(foreignField.field, ".id"));
112
- if (!allFields) {
113
+ else {
114
+ columns.push("".concat(foreignField.field, ".id"));
113
115
  var primaryField = getCollectionPrimaryField(ctx, foreignField.collection);
114
- if (primaryField) {
116
+ if (primaryField)
115
117
  columns.push("".concat(foreignField.field, ".").concat(primaryField));
116
- }
117
- }
118
- else {
119
- var fieldNames = Object.keys(ctx.meta.collections[foreignField.collection].fields);
120
- for (var index_1 = 0; index_1 < fieldNames.length; index_1++) {
121
- var fieldName = fieldNames[index_1];
122
- columns.push("".concat(foreignField.field, ".").concat(fieldName));
123
- }
124
118
  }
125
119
  }
126
120
  var foreignColumns = columns.join(",");
@@ -12,6 +12,7 @@
12
12
  showHeader?: boolean;
13
13
  showFooter?: boolean;
14
14
  tableProps?: Partial<TableProps>;
15
+ position?: "side" | "bottom";
15
16
  onClose?: () => void;
16
17
  }
17
18
 
@@ -22,20 +23,23 @@
22
23
  showHeader = true,
23
24
  showFooter = true,
24
25
  tableProps,
26
+ position = "side",
25
27
  onClose,
26
28
  }: Props = $props();
27
29
  </script>
28
30
 
29
- <Drawer onHide={async () => onClose?.()}>
30
- <div class="flex h-12 shrink-0 items-center gap-4 border-b px-4">
31
- <Button
32
- variant="outline"
33
- onclick={() => onClose?.()}
34
- class="h-8 w-8 rounded-full text-xs font-normal"
35
- Icon={ArrowLeft}
36
- />
37
- <div class="text-sm font-medium">{title ?? collectionName}</div>
38
- </div>
31
+ <Drawer onHide={async () => onClose?.()} {position}>
32
+ {#if position !== "bottom"}
33
+ <div class="flex h-12 shrink-0 items-center gap-4 border-b px-4">
34
+ <Button
35
+ variant="outline"
36
+ onclick={() => onClose?.()}
37
+ class="h-8 w-8 rounded-full text-xs font-normal"
38
+ Icon={ArrowLeft}
39
+ />
40
+ <div class="text-sm font-medium">{title ?? collectionName}</div>
41
+ </div>
42
+ {/if}
39
43
  <div class="min-h-0 flex-1 overflow-auto">
40
44
  <DataTable
41
45
  {collectionName}
@@ -6,6 +6,7 @@ interface Props {
6
6
  showHeader?: boolean;
7
7
  showFooter?: boolean;
8
8
  tableProps?: Partial<TableProps>;
9
+ position?: "side" | "bottom";
9
10
  onClose?: () => void;
10
11
  }
11
12
  declare const DataTableDrawer: import("svelte").Component<Props, {}, "">;
@@ -28,7 +28,7 @@
28
28
  import { getField, getFieldIcon } from "../../dataTable/utils";
29
29
  import Children from "./children.svelte";
30
30
  import {
31
- generateTransactionBody,
31
+ buildChildren,
32
32
  getDefaultEntry,
33
33
  parseDetailViewValues,
34
34
  serializeEntry,
@@ -82,31 +82,15 @@
82
82
  }
83
83
  }
84
84
 
85
- const serializedEntry = serializeEntry(
86
- ctx,
87
- collectionName,
88
- localEntry,
89
- rollback,
90
- );
91
-
92
- let transactionBody;
93
85
  if (rollback) {
94
- transactionBody = [
95
- {
96
- method: "createOne",
97
- props: { collectionName: collectionName, data: serializedEntry },
98
- },
99
- ];
100
- } else {
101
- transactionBody = generateTransactionBody(
102
- ctx,
103
- collectionName,
104
- serializedEntry,
105
- );
86
+ if (onSuccessfullSave) await onSuccessfullSave(localEntry);
87
+ onCancel?.();
88
+ return;
106
89
  }
107
90
 
108
- // create the record
109
- let response = await lobb.transactions(transactionBody, rollback);
91
+ const serializedEntry = serializeEntry(ctx, collectionName, localEntry);
92
+ const children = buildChildren(ctx, collectionName, localEntry);
93
+ const response = await lobb.createOne(collectionName, serializedEntry, children);
110
94
 
111
95
  await emitEvent({ lobb, ctx }, "studio.collections.create", {
112
96
  collectionName,
@@ -126,15 +110,8 @@
126
110
  }
127
111
  }
128
112
 
129
- // close detailView side bar
130
- if (onSuccessfullSave) {
131
- await onSuccessfullSave(localEntry);
132
- }
133
-
134
- if (!rollback) {
135
- toast.success(`The record was successfully created`);
136
- }
137
-
113
+ if (onSuccessfullSave) await onSuccessfullSave(localEntry);
114
+ toast.success(`The record was successfully created`);
138
115
  onCancel?.();
139
116
  }
140
117
  </script>
@@ -3,7 +3,10 @@ import type { DetailFormField } from "./detailViewForm.svelte";
3
3
  export declare function getDefaultEntry(ctx: CTX, fieldNames: string[], collectionName: string, values?: Record<string, any>): {
4
4
  [k: string]: any;
5
5
  };
6
- export declare function serializeEntry(ctx: CTX, collectionName: string, entry: Record<string, any>, rollback?: boolean): Record<string, any>;
7
- export declare function generateTransactionBody(ctx: CTX, collectionName: string, entry: Record<string, any>): any[];
6
+ export declare function serializeEntry(ctx: CTX, collectionName: string, entry: Record<string, any>): Record<string, any>;
7
+ export declare function buildChildren(ctx: CTX, collectionName: string, entry: Record<string, any>): Record<string, {
8
+ create?: any[];
9
+ link?: any[];
10
+ }> | undefined;
8
11
  export declare function parseDetailViewValues(ctx: CTX, collectionName: string, values: Record<string, any>): void;
9
12
  export declare function getCollectionFields(ctx: CTX, collectionName: string): DetailFormField[];
@@ -33,11 +33,9 @@ export function getDefaultEntry(ctx, fieldNames, collectionName, values) {
33
33
  return [fieldName, value];
34
34
  }));
35
35
  }
36
- export function serializeEntry(ctx, collectionName, entry, rollback) {
37
- if (rollback === void 0) { rollback = false; }
38
- // deep clone the object
36
+ export function serializeEntry(ctx, collectionName, entry) {
39
37
  entry = __assign({}, entry);
40
- // serialize the foreign key field's value
38
+ // extract FK object fields ID
41
39
  for (var _i = 0, _a = Object.entries(entry); _i < _a.length; _i++) {
42
40
  var _b = _a[_i], fieldName = _b[0], fieldValue = _b[1];
43
41
  var isRefrenceField = isRelationField(ctx, collectionName, fieldName);
@@ -45,7 +43,7 @@ export function serializeEntry(ctx, collectionName, entry, rollback) {
45
43
  entry[fieldName] = fieldValue.id;
46
44
  }
47
45
  }
48
- // serialize polymorphic id_field values ({ id: 5, title: "..." } 5)
46
+ // extract polymorphic id_field objectsID
49
47
  for (var _c = 0, _d = ctx.meta.relations; _c < _d.length; _c++) {
50
48
  var relation = _d[_c];
51
49
  if (relation.type !== "polymorphic")
@@ -58,76 +56,35 @@ export function serializeEntry(ctx, collectionName, entry, rollback) {
58
56
  entry[idField] = fieldValue.id;
59
57
  }
60
58
  }
61
- // check for related collections properties and serialize them too
62
- if (!rollback) {
63
- var childrenRelations = ctx.meta.relations.filter(function (relation) { return relation.to.collection === collectionName; });
64
- var childrenCollectionNames = childrenRelations.map(function (relation) { return relation.from.collection; });
65
- for (var index = 0; index < childrenCollectionNames.length; index++) {
66
- var childrenCollectionName = childrenCollectionNames[index];
67
- var childrenEntries = entry[childrenCollectionName];
68
- if (childrenEntries) {
69
- for (var index_1 = 0; index_1 < childrenEntries.length; index_1++) {
70
- childrenEntries[index_1] = serializeEntry(ctx, childrenCollectionName, childrenEntries[index_1]);
71
- }
72
- }
73
- }
74
- }
75
59
  return entry;
76
60
  }
77
- export function generateTransactionBody(ctx, collectionName, entry) {
78
- entry = __assign({}, entry);
79
- function handleEntryRecursive(transactionBody, collectionName, entry, parentTransactionIndex) {
80
- var _a;
81
- var _b;
82
- var parentCollectionName = parentTransactionIndex !== undefined ? transactionBody[parentTransactionIndex].props.collectionName : null;
83
- var foreignKeyFieldName = (_b = ctx.meta.relations.find(function (relation) { return relation.from.collection === collectionName && relation.to.collection === parentCollectionName; })) === null || _b === void 0 ? void 0 : _b.from.field;
84
- var collectionFieldNames = Object.keys(ctx.meta.collections[collectionName].fields);
85
- var payload = {};
86
- for (var index = 0; index < collectionFieldNames.length; index++) {
87
- var fieldName = collectionFieldNames[index];
88
- var isForeignKeyField = fieldName === foreignKeyFieldName;
89
- if (isForeignKeyField) {
90
- payload[fieldName] = "{{ responses[".concat(parentTransactionIndex, "].data.id }}");
91
- continue;
92
- }
93
- payload[fieldName] = entry[fieldName];
94
- }
95
- var localTransactionIndex = transactionBody.length;
96
- if (payload.id) {
97
- var localPayload = (_a = {},
98
- _a[foreignKeyFieldName] = payload[foreignKeyFieldName],
99
- _a);
100
- transactionBody.push({
101
- method: "updateMany",
102
- props: {
103
- collectionName: collectionName,
104
- data: localPayload,
105
- filter: { id: payload.id },
106
- },
107
- });
108
- }
109
- else {
110
- transactionBody.push({
111
- method: "createOne",
112
- props: { collectionName: collectionName, data: payload },
113
- });
114
- }
115
- var childrenRelations = ctx.meta.relations.filter(function (relation) { return relation.to.collection === collectionName; });
116
- var childrenCollectionNames = childrenRelations.map(function (relation) { return relation.from.collection; });
117
- for (var index = 0; index < childrenCollectionNames.length; index++) {
118
- var childrenCollectionName = childrenCollectionNames[index];
119
- var childrenEntries = entry[childrenCollectionName];
120
- if (childrenEntries) {
121
- for (var index_2 = 0; index_2 < childrenEntries.length; index_2++) {
122
- var childrenEntry = childrenEntries[index_2];
123
- handleEntryRecursive(transactionBody, childrenCollectionName, childrenEntry, localTransactionIndex);
124
- }
125
- }
61
+ export function buildChildren(ctx, collectionName, entry) {
62
+ var childrenRelations = ctx.meta.relations.filter(function (relation) { return relation.type !== "polymorphic" && relation.to.collection === collectionName; });
63
+ var children = {};
64
+ var _loop_1 = function (relation) {
65
+ var childCollection = relation.from.collection;
66
+ var childEntries = entry[childCollection];
67
+ if (!(childEntries === null || childEntries === void 0 ? void 0 : childEntries.length))
68
+ return "continue";
69
+ var toCreate = childEntries
70
+ .filter(function (e) { return !e.id; })
71
+ .map(function (e) { return serializeEntry(ctx, childCollection, e); });
72
+ var toLink = childEntries
73
+ .filter(function (e) { return e.id; })
74
+ .map(function (e) { return e.id; });
75
+ if (toCreate.length || toLink.length) {
76
+ children[childCollection] = {};
77
+ if (toCreate.length)
78
+ children[childCollection].create = toCreate;
79
+ if (toLink.length)
80
+ children[childCollection].link = toLink;
126
81
  }
82
+ };
83
+ for (var _i = 0, childrenRelations_1 = childrenRelations; _i < childrenRelations_1.length; _i++) {
84
+ var relation = childrenRelations_1[_i];
85
+ _loop_1(relation);
127
86
  }
128
- var transactionBody = [];
129
- handleEntryRecursive(transactionBody, collectionName, entry);
130
- return transactionBody;
87
+ return Object.keys(children).length ? children : undefined;
131
88
  }
132
89
  export function parseDetailViewValues(ctx, collectionName, values) {
133
90
  var forignFieldNames = ctx.meta.relations
@@ -1,30 +1,46 @@
1
1
  <script lang="ts">
2
2
  import { calculateDrawerWidth } from "../utils";
3
3
  import type { Snippet } from "svelte";
4
- import { fade, fly } from "svelte/transition";
4
+ import { fade } from "svelte/transition";
5
+ import { cubicOut } from "svelte/easing";
5
6
  import Portal from "svelte-portal";
6
7
 
7
8
  interface Props {
8
9
  children?: Snippet<[]>;
9
10
  onHide?: () => Promise<void>;
11
+ position?: "side" | "bottom";
10
12
  }
11
13
 
12
- let { onHide, children }: Props = $props();
14
+ let { onHide, children, position = "side" }: Props = $props();
15
+
16
+ function slide(_node: Element, { duration = 250, axis }: { duration?: number; axis: "x" | "y" }) {
17
+ return {
18
+ duration,
19
+ easing: cubicOut,
20
+ css: (t: number) => {
21
+ const offset = (1 - t) * 100;
22
+ return axis === "y"
23
+ ? `transform: translateY(${offset}%)`
24
+ : `transform: translateX(${offset}%)`;
25
+ },
26
+ };
27
+ }
13
28
  </script>
14
29
 
15
30
  <Portal target="body">
16
31
  <button
17
- transition:fade={{ duration: 250 }}
32
+ transition:fade={{ duration: 200 }}
18
33
  onclick={() => onHide?.()}
19
- class="backgroundDrawerButton fixed left-0 top-0 z-40 h-screen w-screen bg-background opacity-50 cursor-default"
34
+ class="backgroundDrawerButton fixed left-0 top-0 z-40 h-screen w-screen bg-black/50 cursor-default"
20
35
  aria-label="background used to hide the background"
21
36
  ></button>
22
37
 
23
- <!-- the drawer -->
24
38
  <div
25
- transition:fly={{ x: "100%", duration: 250 }}
26
- class="fixed right-0 top-0 z-40 flex h-full w-full flex-col border-l bg-background"
27
- style="max-width: {calculateDrawerWidth()}px;"
39
+ transition:slide={{ axis: position === "bottom" ? "y" : "x" }}
40
+ class={position === "bottom"
41
+ ? "fixed bottom-0 left-0 z-40 flex h-[60vh] w-full flex-col border-t bg-background"
42
+ : "fixed right-0 top-0 z-40 flex h-full w-full flex-col border-l bg-background"}
43
+ style={position === "side" ? `max-width: ${calculateDrawerWidth()}px;` : ""}
28
44
  >
29
45
  {@render children?.()}
30
46
  </div>
@@ -2,6 +2,7 @@ import type { Snippet } from "svelte";
2
2
  interface Props {
3
3
  children?: Snippet<[]>;
4
4
  onHide?: () => Promise<void>;
5
+ position?: "side" | "bottom";
5
6
  }
6
7
  declare const Drawer: import("svelte").Component<Props, {}, "">;
7
8
  type Drawer = ReturnType<typeof Drawer>;
@@ -59,7 +59,9 @@ export interface ExtensionUtils {
59
59
  title?: string;
60
60
  showHeader?: boolean;
61
61
  showFooter?: boolean;
62
+ position?: "side" | "bottom";
62
63
  }) => void;
64
+ emitEvent: (eventName: string, input: Record<string, any>) => Promise<Record<string, any>>;
63
65
  components: Components;
64
66
  mediaQueries: typeof mediaQueries;
65
67
  intlDate: typeof intlDate;
@@ -45,6 +45,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
45
45
  };
46
46
  import { toast } from "svelte-sonner";
47
47
  import { showDialog, openDataTableDrawer } from "../actions";
48
+ import { emitEvent } from "../eventSystem";
48
49
  import { Button } from "../components/ui/button";
49
50
  import { Input } from "../components/ui/input";
50
51
  import { Separator } from "../components/ui/separator";
@@ -100,6 +101,7 @@ export function getExtensionUtils(lobb, ctx) {
100
101
  toast: toast,
101
102
  showDialog: showDialog,
102
103
  openDataTableDrawer: function (props) { return openDataTableDrawer({ lobb: lobb, ctx: ctx }, props); },
104
+ emitEvent: function (eventName, input) { return emitEvent({ lobb: lobb, ctx: ctx }, eventName, input); },
103
105
  components: getComponents(),
104
106
  mediaQueries: mediaQueries,
105
107
  intlDate: intlDate,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lobb-js/studio",
3
3
  "license": "UNLICENSED",
4
- "version": "0.20.0",
4
+ "version": "0.21.0",
5
5
  "type": "module",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -42,7 +42,7 @@
42
42
  "postpublish": "./scripts/postpublish.sh"
43
43
  },
44
44
  "devDependencies": {
45
- "@lobb-js/core": "^0.24.0",
45
+ "@lobb-js/core": "^0.25.0",
46
46
  "@chromatic-com/storybook": "^4.1.2",
47
47
  "@storybook/addon-a11y": "^10.0.1",
48
48
  "@storybook/addon-docs": "^10.0.1",
@@ -15,6 +15,7 @@ export interface OpenDataTableDrawerProps {
15
15
  title?: string;
16
16
  showHeader?: boolean;
17
17
  showFooter?: boolean;
18
+ position?: "side" | "bottom";
18
19
  }
19
20
 
20
21
  export function showDialog(title: string, description: string): Promise<boolean> {
@@ -113,24 +113,15 @@ export function getCollectionParamsFields(ctx: CTX, collectionName: string, allF
113
113
  });
114
114
 
115
115
  const columns = [];
116
- for (let index = 0; index < foreignFields.length; index++) {
117
- const foreignField = foreignFields[index];
118
- if (!foreignField.collection || !ctx.meta.collections[foreignField.collection]) {
119
- continue;
120
- }
121
- columns.push(`${foreignField.field}.id`);
116
+ for (const foreignField of foreignFields) {
117
+ if (!foreignField.collection || !ctx.meta.collections[foreignField.collection]) continue;
122
118
 
123
- if (!allFields) {
124
- const primaryField = getCollectionPrimaryField(ctx, foreignField.collection)
125
- if (primaryField) {
126
- columns.push(`${foreignField.field}.${primaryField}`);
127
- }
119
+ if (allFields) {
120
+ columns.push(`${foreignField.field}.*`);
128
121
  } else {
129
- const fieldNames = Object.keys(ctx.meta.collections[foreignField.collection].fields);
130
- for (let index = 0; index < fieldNames.length; index++) {
131
- const fieldName = fieldNames[index];
132
- columns.push(`${foreignField.field}.${fieldName}`);
133
- }
122
+ columns.push(`${foreignField.field}.id`);
123
+ const primaryField = getCollectionPrimaryField(ctx, foreignField.collection);
124
+ if (primaryField) columns.push(`${foreignField.field}.${primaryField}`);
134
125
  }
135
126
  }
136
127
 
@@ -12,6 +12,7 @@
12
12
  showHeader?: boolean;
13
13
  showFooter?: boolean;
14
14
  tableProps?: Partial<TableProps>;
15
+ position?: "side" | "bottom";
15
16
  onClose?: () => void;
16
17
  }
17
18
 
@@ -22,20 +23,23 @@
22
23
  showHeader = true,
23
24
  showFooter = true,
24
25
  tableProps,
26
+ position = "side",
25
27
  onClose,
26
28
  }: Props = $props();
27
29
  </script>
28
30
 
29
- <Drawer onHide={async () => onClose?.()}>
30
- <div class="flex h-12 shrink-0 items-center gap-4 border-b px-4">
31
- <Button
32
- variant="outline"
33
- onclick={() => onClose?.()}
34
- class="h-8 w-8 rounded-full text-xs font-normal"
35
- Icon={ArrowLeft}
36
- />
37
- <div class="text-sm font-medium">{title ?? collectionName}</div>
38
- </div>
31
+ <Drawer onHide={async () => onClose?.()} {position}>
32
+ {#if position !== "bottom"}
33
+ <div class="flex h-12 shrink-0 items-center gap-4 border-b px-4">
34
+ <Button
35
+ variant="outline"
36
+ onclick={() => onClose?.()}
37
+ class="h-8 w-8 rounded-full text-xs font-normal"
38
+ Icon={ArrowLeft}
39
+ />
40
+ <div class="text-sm font-medium">{title ?? collectionName}</div>
41
+ </div>
42
+ {/if}
39
43
  <div class="min-h-0 flex-1 overflow-auto">
40
44
  <DataTable
41
45
  {collectionName}
@@ -28,7 +28,7 @@
28
28
  import { getField, getFieldIcon } from "../../dataTable/utils";
29
29
  import Children from "./children.svelte";
30
30
  import {
31
- generateTransactionBody,
31
+ buildChildren,
32
32
  getDefaultEntry,
33
33
  parseDetailViewValues,
34
34
  serializeEntry,
@@ -82,31 +82,15 @@
82
82
  }
83
83
  }
84
84
 
85
- const serializedEntry = serializeEntry(
86
- ctx,
87
- collectionName,
88
- localEntry,
89
- rollback,
90
- );
91
-
92
- let transactionBody;
93
85
  if (rollback) {
94
- transactionBody = [
95
- {
96
- method: "createOne",
97
- props: { collectionName: collectionName, data: serializedEntry },
98
- },
99
- ];
100
- } else {
101
- transactionBody = generateTransactionBody(
102
- ctx,
103
- collectionName,
104
- serializedEntry,
105
- );
86
+ if (onSuccessfullSave) await onSuccessfullSave(localEntry);
87
+ onCancel?.();
88
+ return;
106
89
  }
107
90
 
108
- // create the record
109
- let response = await lobb.transactions(transactionBody, rollback);
91
+ const serializedEntry = serializeEntry(ctx, collectionName, localEntry);
92
+ const children = buildChildren(ctx, collectionName, localEntry);
93
+ const response = await lobb.createOne(collectionName, serializedEntry, children);
110
94
 
111
95
  await emitEvent({ lobb, ctx }, "studio.collections.create", {
112
96
  collectionName,
@@ -126,15 +110,8 @@
126
110
  }
127
111
  }
128
112
 
129
- // close detailView side bar
130
- if (onSuccessfullSave) {
131
- await onSuccessfullSave(localEntry);
132
- }
133
-
134
- if (!rollback) {
135
- toast.success(`The record was successfully created`);
136
- }
137
-
113
+ if (onSuccessfullSave) await onSuccessfullSave(localEntry);
114
+ toast.success(`The record was successfully created`);
138
115
  onCancel?.();
139
116
  }
140
117
  </script>
@@ -30,12 +30,10 @@ export function serializeEntry(
30
30
  ctx: CTX,
31
31
  collectionName: string,
32
32
  entry: Record<string, any>,
33
- rollback: boolean = false,
34
33
  ) {
35
- // deep clone the object
36
34
  entry = { ...entry }
37
35
 
38
- // serialize the foreign key field's value
36
+ // extract FK object fields ID
39
37
  for (const [fieldName, fieldValue] of Object.entries(entry)) {
40
38
  const isRefrenceField = isRelationField(ctx, collectionName, fieldName);
41
39
  if (isRefrenceField && fieldValue !== null && fieldValue.id !== undefined) {
@@ -43,7 +41,7 @@ export function serializeEntry(
43
41
  }
44
42
  }
45
43
 
46
- // serialize polymorphic id_field values ({ id: 5, title: "..." } 5)
44
+ // extract polymorphic id_field objectsID
47
45
  for (const relation of ctx.meta.relations) {
48
46
  if (relation.type !== "polymorphic") continue;
49
47
  if ((relation as any).from.collection !== collectionName) continue;
@@ -54,90 +52,40 @@ export function serializeEntry(
54
52
  }
55
53
  }
56
54
 
57
- // check for related collections properties and serialize them too
58
- if (!rollback) {
59
- const childrenRelations = ctx.meta.relations.filter((relation) => relation.to.collection === collectionName);
60
- const childrenCollectionNames = childrenRelations.map((relation) => relation.from.collection);
61
- for (let index = 0; index < childrenCollectionNames.length; index++) {
62
- const childrenCollectionName = childrenCollectionNames[index];
63
- const childrenEntries = entry[childrenCollectionName];
64
- if (childrenEntries) {
65
- for (let index = 0; index < childrenEntries.length; index++) {
66
- childrenEntries[index] = serializeEntry(ctx, childrenCollectionName, childrenEntries[index]);
67
- }
68
- }
69
- }
70
- }
71
-
72
55
  return entry;
73
56
  }
74
57
 
75
- export function generateTransactionBody(
58
+ export function buildChildren(
76
59
  ctx: CTX,
77
60
  collectionName: string,
78
61
  entry: Record<string, any>,
79
- ) {
80
- entry = { ...entry }
81
- function handleEntryRecursive(
82
- transactionBody: any[],
83
- collectionName: string,
84
- entry: Record<string, any>,
85
- parentTransactionIndex?: number,
86
- ) {
87
- const parentCollectionName = parentTransactionIndex !== undefined ? transactionBody[parentTransactionIndex].props.collectionName : null;
88
- const foreignKeyFieldName = ctx.meta.relations.find(relation => relation.from.collection === collectionName && relation.to.collection === parentCollectionName)?.from.field;
89
- const collectionFieldNames = Object.keys(ctx.meta.collections[collectionName].fields);
90
- const payload: any = {};
91
- for (let index = 0; index < collectionFieldNames.length; index++) {
92
- const fieldName = collectionFieldNames[index];
93
- const isForeignKeyField = fieldName === foreignKeyFieldName;
94
- if (isForeignKeyField) {
95
- payload[fieldName] = `{{ responses[${parentTransactionIndex}].data.id }}`
96
- continue;
97
- }
62
+ ): Record<string, { create?: any[]; link?: any[] }> | undefined {
63
+ const childrenRelations = ctx.meta.relations.filter(
64
+ (relation) => relation.type !== "polymorphic" && relation.to.collection === collectionName,
65
+ );
98
66
 
99
- payload[fieldName] = entry[fieldName];
100
- }
67
+ const children: Record<string, { create?: any[]; link?: any[] }> = {};
101
68
 
102
- const localTransactionIndex = transactionBody.length;
103
- if (payload.id) {
104
- const localPayload = {
105
- [foreignKeyFieldName]: payload[foreignKeyFieldName],
106
- };
107
- transactionBody.push({
108
- method: "updateMany",
109
- props: {
110
- collectionName: collectionName,
111
- data: localPayload,
112
- filter: { id: payload.id },
113
- },
114
- });
115
- } else {
116
- transactionBody.push({
117
- method: "createOne",
118
- props: { collectionName: collectionName, data: payload },
119
- });
120
- }
69
+ for (const relation of childrenRelations) {
70
+ const childCollection = (relation as any).from.collection;
71
+ const childEntries: any[] = entry[childCollection];
72
+ if (!childEntries?.length) continue;
121
73
 
122
- const childrenRelations = ctx.meta.relations.filter((relation) => relation.to.collection === collectionName);
123
- const childrenCollectionNames = childrenRelations.map((relation) => relation.from.collection);
124
- for (let index = 0; index < childrenCollectionNames.length; index++) {
125
- const childrenCollectionName = childrenCollectionNames[index];
126
- const childrenEntries = entry[childrenCollectionName];
127
- if (childrenEntries) {
128
- for (let index = 0; index < childrenEntries.length; index++) {
129
- const childrenEntry = childrenEntries[index];
130
- handleEntryRecursive(transactionBody, childrenCollectionName, childrenEntry, localTransactionIndex);
131
- }
132
- }
74
+ const toCreate = childEntries
75
+ .filter((e) => !e.id)
76
+ .map((e) => serializeEntry(ctx, childCollection, e));
77
+ const toLink = childEntries
78
+ .filter((e) => e.id)
79
+ .map((e) => e.id);
80
+
81
+ if (toCreate.length || toLink.length) {
82
+ children[childCollection] = {};
83
+ if (toCreate.length) children[childCollection].create = toCreate;
84
+ if (toLink.length) children[childCollection].link = toLink;
133
85
  }
134
86
  }
135
87
 
136
- const transactionBody: any[] = [];
137
-
138
- handleEntryRecursive(transactionBody, collectionName, entry);
139
-
140
- return transactionBody
88
+ return Object.keys(children).length ? children : undefined;
141
89
  }
142
90
 
143
91
  export function parseDetailViewValues(ctx: CTX, collectionName: string, values: Record<string, any>) {
@@ -1,30 +1,46 @@
1
1
  <script lang="ts">
2
2
  import { calculateDrawerWidth } from "../utils";
3
3
  import type { Snippet } from "svelte";
4
- import { fade, fly } from "svelte/transition";
4
+ import { fade } from "svelte/transition";
5
+ import { cubicOut } from "svelte/easing";
5
6
  import Portal from "svelte-portal";
6
7
 
7
8
  interface Props {
8
9
  children?: Snippet<[]>;
9
10
  onHide?: () => Promise<void>;
11
+ position?: "side" | "bottom";
10
12
  }
11
13
 
12
- let { onHide, children }: Props = $props();
14
+ let { onHide, children, position = "side" }: Props = $props();
15
+
16
+ function slide(_node: Element, { duration = 250, axis }: { duration?: number; axis: "x" | "y" }) {
17
+ return {
18
+ duration,
19
+ easing: cubicOut,
20
+ css: (t: number) => {
21
+ const offset = (1 - t) * 100;
22
+ return axis === "y"
23
+ ? `transform: translateY(${offset}%)`
24
+ : `transform: translateX(${offset}%)`;
25
+ },
26
+ };
27
+ }
13
28
  </script>
14
29
 
15
30
  <Portal target="body">
16
31
  <button
17
- transition:fade={{ duration: 250 }}
32
+ transition:fade={{ duration: 200 }}
18
33
  onclick={() => onHide?.()}
19
- class="backgroundDrawerButton fixed left-0 top-0 z-40 h-screen w-screen bg-background opacity-50 cursor-default"
34
+ class="backgroundDrawerButton fixed left-0 top-0 z-40 h-screen w-screen bg-black/50 cursor-default"
20
35
  aria-label="background used to hide the background"
21
36
  ></button>
22
37
 
23
- <!-- the drawer -->
24
38
  <div
25
- transition:fly={{ x: "100%", duration: 250 }}
26
- class="fixed right-0 top-0 z-40 flex h-full w-full flex-col border-l bg-background"
27
- style="max-width: {calculateDrawerWidth()}px;"
39
+ transition:slide={{ axis: position === "bottom" ? "y" : "x" }}
40
+ class={position === "bottom"
41
+ ? "fixed bottom-0 left-0 z-40 flex h-[60vh] w-full flex-col border-t bg-background"
42
+ : "fixed right-0 top-0 z-40 flex h-full w-full flex-col border-l bg-background"}
43
+ style={position === "side" ? `max-width: ${calculateDrawerWidth()}px;` : ""}
28
44
  >
29
45
  {@render children?.()}
30
46
  </div>
@@ -57,7 +57,8 @@ export interface ExtensionUtils {
57
57
  location: Location;
58
58
  toast: typeof toast;
59
59
  showDialog: typeof showDialog;
60
- openDataTableDrawer: (props: { collectionName: string; filter?: Record<string, any>; title?: string; showHeader?: boolean; showFooter?: boolean }) => void;
60
+ openDataTableDrawer: (props: { collectionName: string; filter?: Record<string, any>; title?: string; showHeader?: boolean; showFooter?: boolean; position?: "side" | "bottom" }) => void;
61
+ emitEvent: (eventName: string, input: Record<string, any>) => Promise<Record<string, any>>;
61
62
  components: Components;
62
63
  mediaQueries: typeof mediaQueries;
63
64
  intlDate: typeof intlDate;
@@ -8,6 +8,7 @@ import type { LobbClient } from "@lobb-js/sdk";
8
8
  import type { CTX } from "../store.types";
9
9
  import { toast } from "svelte-sonner";
10
10
  import { showDialog, openDataTableDrawer } from "../actions";
11
+ import { emitEvent } from "../eventSystem";
11
12
  import { Button } from "../components/ui/button";
12
13
  import { Input } from "../components/ui/input";
13
14
  import { Separator } from "../components/ui/separator";
@@ -65,6 +66,7 @@ export function getExtensionUtils(lobb: LobbClient, ctx: CTX): ExtensionUtils {
65
66
  toast: toast,
66
67
  showDialog: showDialog,
67
68
  openDataTableDrawer: (props) => openDataTableDrawer({ lobb, ctx }, props),
69
+ emitEvent: (eventName, input) => emitEvent({ lobb, ctx }, eventName, input),
68
70
  components: getComponents(),
69
71
  mediaQueries: mediaQueries,
70
72
  intlDate: intlDate,