Page MenuHomeSealhub

create.ts
No OneTemporary

create.ts

/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Context } from "koa";
import { Collection, ExtractFieldInput } from "sealious";
import type { FormData, FormField, HTMLFunc } from "@sealcode/sealgen";
import { Form, Controls } from "@sealcode/sealgen";
import { tempstream } from "tempstream";
import type { FormControl } from "@sealcode/sealgen/@types/src/forms/controls/form-control";
export default class CRUDCreatePage<
C extends Collection,
Fields extends Record<keyof Collection["fields"], FormField>
> extends Form<Fields, void> {
defaultSuccessMessage = "Formularz wypełniony poprawnie";
collection: C;
controls: FormControl[]; // overwritten below;
constructor(
public args: {
collection: C;
fields: Fields;
controls: FormControl[];
create_title: string;
html: HTMLFunc;
base_url: string;
back_to_list_button_text: string;
form_value_to_sealious_value: {
[field in keyof C["fields"]]?: (
data: unknown
) => ExtractFieldInput<C["fields"][field]>;
};
}
) {
super();
this.fields = args.fields;
this.collection = args.collection;
this.controls = [
new Controls.FormHeader(this.args.create_title),
...this.args.controls,
];
}
async onSubmit(ctx: Context) {
const data = await this.getParsedValues(ctx);
if (!data) {
throw new Error("Error when parsing the form values");
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment
const obj = Object.fromEntries(
Object.entries(this.collection.fields).map(([field_name]) => [
field_name,
this.args.form_value_to_sealious_value[field_name]?.(
data[field_name]
) as any,
])
) as any;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await this.collection.create(ctx.$context, obj);
}
canAccess = async (ctx: Context) => {
const policy = this.collection.getPolicy("create");
const response = await policy.check(ctx.$context);
return {
canAccess: response?.allowed || false,
message: response?.reason || "",
};
};
async renderNavbar(_ctx: Context) {
return /* HTML */ `<a class="" href="${this.args.base_url}"
>${this.args.back_to_list_button_text}</a
>`;
}
async render(ctx: Context, data: FormData, show_field_errors: boolean) {
return this.args.html({
ctx,
title: this.args.create_title,
description: "",
body: tempstream/* HTML */ ` <div class="crud-ui-create">
${this.renderNavbar(ctx)}
${await super.render(ctx, data, show_field_errors)}
</div>`,
});
}
}

File Metadata

Mime Type
text/x-java
Expires
Mon, Dec 23, 00:57 (4 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556765
Default Alt Text
create.ts (2 KB)

Event Timeline