Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22424032
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/forms/fields/deep-reverse-single-reference-list.ts b/src/forms/fields/deep-reverse-single-reference-list.ts
index 6adb24b..a60ab5a 100644
--- a/src/forms/fields/deep-reverse-single-reference-list.ts
+++ b/src/forms/fields/deep-reverse-single-reference-list.ts
@@ -1,127 +1,141 @@
/* eslint @typescript-eslint/no-explicit-any: off */
import { Context } from "koa";
-import { CollectionItem, FieldTypes as SealiousFieldTypes } from "sealious";
+import {
+ CollectionItem,
+ ItemList,
+ FieldTypes as SealiousFieldTypes,
+} from "sealious";
import { FormDataValue, FormData } from "../form-types.js";
import { CheckboxedListField } from "./checkboxed-list.js";
import { ExtractedFieldInfo } from "../../utils/extract-fields-from-collection.js";
import { wrapKeyName } from "../../utils/wrap-attributes.js";
export class DeepReverseSingleReferenceList extends CheckboxedListField<
false,
string
> {
public item_label_getter: (item: CollectionItem<any>) => string = (item) =>
item.get("name") as string;
+ public item_list_process: (list: ItemList<any>) => void = () => {};
+
constructor(
public sealious_field: SealiousFieldTypes.DeepReverseSingleReference
) {
super(false, async (ctx: Context) => {
- const { items } = await ctx.$app.collections[
+ const list = ctx.$app.collections[
sealious_field.target_collection
- ]!.list(ctx.$context).fetch();
+ ]!.list(ctx.$context);
+ this.item_list_process(list);
+ const { items } = await list.fetch();
return items.map((s) => ({
label: this.item_label_getter(s),
value: s.id,
}));
});
}
- setItemLabelGetter(getter: (item: CollectionItem<any>) => string) {
+ setItemLabelGetter(getter: (item: CollectionItem<any>) => string): this {
this.item_label_getter = getter;
+ return this;
+ }
+
+ setItemListProcess(fn: (list: ItemList<any>) => void): this {
+ this.item_list_process = fn;
+ return this;
}
parsedToSealious(): never {
throw new Error(
"This field is not set directly through field value, but instead relies on postSealiousCreate and postSealiousEdit"
);
}
async postSealiousCreate(
ctx: Context,
created_item: CollectionItem<any>,
form_data: FormData
) {
// First, we remove links refering to current item
const { items: existing_links } = await ctx.$app.collections[
this.sealious_field.intermediary_collection
]!.list(ctx.$context)
.filter({
[this.sealious_field.referencing_field]: created_item.id,
})
.fetch();
const { parsed: value } = await this.getParsedValue(
ctx,
form_data.raw_values,
false
);
const should_exist_ids = Object.entries(value || {})
.filter(([, value]) => value === "on")
.map(([key]) => key);
const to_delete = existing_links.filter(
(link) =>
!should_exist_ids.includes(
link.get(
this.sealious_field.intermediary_field_that_points_there
) as string
)
);
const to_create_ids = should_exist_ids.filter(
(id) =>
!existing_links
.map((link) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
link.get(
this.sealious_field
.intermediary_field_that_points_there
)
)
.includes(id)
);
await Promise.all(
to_delete.map((item) => {
return item.remove(ctx.$context);
})
);
// Then, we iterate over all selected values and insert appropriate missing links
const promises = to_create_ids.map(async (id) => {
await this.sealious_field
.getReferencingCollection()
.create(ctx.$context, {
[this.sealious_field.referencing_field]: created_item.id,
[this.sealious_field.intermediary_field_that_points_there]:
id,
});
});
await Promise.all(promises);
}
async postSealiousEdit(
ctx: Context,
edited_item: CollectionItem<any>,
value: FormData
) {
return this.postSealiousCreate(ctx, edited_item, value);
}
async sealiousValueToForm(
_ctx: Context,
value: string[] | null
): Promise<FormDataValue> {
return Object.fromEntries((value || []).map((id) => [id, "on"]));
}
generateFieldDeclaration(
_field_info: ExtractedFieldInfo,
vars: { form_field_types: string; sealious_field: string }
): string {
return `new ${vars.form_field_types}.${this.constructor.name}(${vars.sealious_field})`;
}
generateCreateValueGetter(field_info: ExtractedFieldInfo): string {
return `${wrapKeyName(
field_info.name
)}: undefined /* handled below in postSealiousCreate */`;
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 28, 03:50 (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1230224
Default Alt Text
(4 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline