118 lines
3.3 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 { PagesFormFields, PagesFormControls } from "../shared.js";
import { Pages } from "../../../../collections/collections.js";
import { PagesCRUDListURL } from "../../../urls.js";
import { tempstream } from "tempstream";
import { withFallback } from "@sealcode/sealgen";
export const actionName = "PagesCRUDEdit";
const fields = {
...PagesFormFields,
};
export const PagesCRUDEditShape = fieldsToShape(fields);
export default new (class PagesCRUDEditForm extends Form<typeof fields, void> {
defaultSuccessMessage = "Formularz wypełniony poprawnie";
fields = fields;
controls = [new Controls.FormHeader("Edit Pages"), ...PagesFormControls];
async getID(ctx: Context): Promise<string> {
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["pages"]
.list(ctx.$context)
.ids([id])
.attach({})
.fetch();
if (!item) {
throw new Error("Item with given id not found: " + id);
}
return {
url: item.get("url"),
domain: withFallback(item.get("domain"), ""),
title: withFallback(item.get("title"), ""),
heading: withFallback(item.get("heading"), ""),
description: withFallback(item.get("description"), ""),
imageForMetadata: { old: item.get("imageForMetadata") },
hideNavigation: withFallback(
item.get("hideNavigation"),
String(item.get("hideNavigation")),
"false"
),
};
}
async onSubmit(ctx: Context) {
const data = await this.getParsedValues(ctx);
const id = await this.getID(ctx);
const {
items: [item],
} = await ctx.$app.collections["pages"].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");
}
const preparedImageForMetadata =
data.imageForMetadata.new || data.imageForMetadata.old;
if (!preparedImageForMetadata) {
throw new Error("Missing field: imageForMetadata");
}
item.setMultiple({
url: data["url"],
content: [],
domain: data["domain"] != null ? data["domain"] : "",
title: data["title"] != null ? data["title"] : "",
heading: data["heading"] != null ? data["heading"] : "",
description: data["description"] != null ? data["description"] : "",
imageForMetadata: preparedImageForMetadata,
hideNavigation: !!data.hideNavigation != null ? !!data.hideNavigation : false,
});
await item.save(ctx.$context);
}
canAccess = async (ctx: Context) => {
const policy = Pages.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 Pages",
body: tempstream/* HTML */ ` <div class="sealgen-crud-form">
<a class="" href="${PagesCRUDListURL}">← Back to pages list</a>
${await super.render(ctx, data, show_field_errors)}
</div>`,
description: "",
});
}
})();