109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import type { Context } from "koa";
|
|
import type { FormData } from "@sealcode/sealgen";
|
|
import { Form, Controls, fieldsToShape } from "@sealcode/sealgen";
|
|
import html from "../../../../html.js";
|
|
|
|
import { NavbarLinksFormFields, NavbarLinksFormControls } from "../shared.js";
|
|
import { NavbarLinks } from "../../../../collections/collections.js";
|
|
import { NavbarLinksCRUDListURL } from "../../../urls.js";
|
|
import { tempstream } from "tempstream";
|
|
|
|
import { withFallback } from "@sealcode/sealgen";
|
|
|
|
export const actionName = "NavbarLinksCRUDEdit";
|
|
|
|
const fields = {
|
|
...NavbarLinksFormFields,
|
|
};
|
|
|
|
export const NavbarLinksCRUDEditShape = fieldsToShape(fields);
|
|
|
|
export default new (class NavbarLinksCRUDEditForm extends Form<typeof fields, void> {
|
|
defaultSuccessMessage = "Formularz wypełniony poprawnie";
|
|
getFields = () => fields;
|
|
|
|
getControls = () => [
|
|
new Controls.FormHeader("Edit NavbarLinks"),
|
|
...NavbarLinksFormControls,
|
|
];
|
|
|
|
async getID(ctx: Context): Promise<string> {
|
|
// trying to automatically guess which param is the one that represents the ID
|
|
// of the resource to edit.
|
|
// If sealgen got it wrong, just return the param name that should be used here instead
|
|
const param_name = "id";
|
|
const id = ctx.params[param_name];
|
|
if (!id) {
|
|
throw new Error("Missing URL parameter: " + param_name);
|
|
}
|
|
return id;
|
|
}
|
|
|
|
async getInitialValues(ctx: Context) {
|
|
const id = await this.getID(ctx);
|
|
|
|
const {
|
|
items: [item],
|
|
} = await ctx.$app.collections["navbar-links"]
|
|
.list(ctx.$context)
|
|
.ids([id])
|
|
.attach({})
|
|
.fetch();
|
|
|
|
if (!item) {
|
|
throw new Error("Item with given id not found: " + id);
|
|
}
|
|
|
|
return {
|
|
label: withFallback(item.get("label"), ""),
|
|
href: withFallback(item.get("href"), ""),
|
|
};
|
|
}
|
|
|
|
async onSubmit(ctx: Context) {
|
|
const data = await this.getParsedValues(ctx);
|
|
const id = await this.getID(ctx);
|
|
const {
|
|
items: [item],
|
|
} = await ctx.$app.collections["navbar-links"]
|
|
.list(ctx.$context)
|
|
.ids([id])
|
|
.fetch();
|
|
|
|
if (!item) {
|
|
throw new Error("Unknown id: " + id);
|
|
}
|
|
|
|
if (!data) {
|
|
throw new Error("Error when parsing the form values");
|
|
}
|
|
|
|
item.setMultiple({
|
|
label: data["label"] != null ? data["label"] : "",
|
|
href: data["href"] != null ? data["href"] : "",
|
|
});
|
|
await item.save(ctx.$context);
|
|
}
|
|
|
|
canAccess = async (ctx: Context) => {
|
|
const policy = NavbarLinks.getPolicy("edit");
|
|
const response = await policy.check(ctx.$context);
|
|
return { canAccess: response?.allowed || false, message: response?.reason || "" };
|
|
};
|
|
|
|
async render(ctx: Context, data: FormData, show_field_errors: boolean) {
|
|
return html({
|
|
ctx,
|
|
title: "Edit NavbarLinks",
|
|
body: tempstream /* HTML */ ` <div class="sealgen-crud-form">
|
|
<a class="" href="${NavbarLinksCRUDListURL}"
|
|
>← Back to navbar-links list</a
|
|
>
|
|
${await super.render(ctx, data, show_field_errors)}
|
|
</div>`,
|
|
description: "",
|
|
css_clumps: ["admin-forms"],
|
|
});
|
|
}
|
|
})();
|