Page MenuHomeSealhub

edit-jdd-field.tsx
No OneTemporary

edit-jdd-field.tsx

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/consistent-type-assertions */
/* eslint-disable @typescript-eslint/no-unused-vars */
import type Router from "@koa/router";
import type { JDDocumentContainer, RawJDDocument } from "@sealcode/jdd";
import {
documentContainerFromStorage,
documentToParsed,
documentToStorage,
} from "@sealcode/jdd";
import type { BaseContext } from "koa";
import type { Collection, CollectionItem, FieldNames } from "sealious";
import { TempstreamJSX } from "tempstream";
import { registry } from "../../jdd-components/registry.js";
import { makeJDDContext } from "../../jdd-context.js";
import JDDCreator from "./jdd-creator.js";
import type { JDDPageState } from "./jdd-page.js";
export const actionName = "ArticleContentEdit";
export abstract class EditJDDField<C extends Collection> extends JDDCreator {
async getID(ctx: BaseContext): Promise<string> {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const id = ctx.params["id"] as string;
if (!id) {
throw new Error("Missing URL parameter: " + "id");
}
return id;
}
abstract getCollection(ctx: BaseContext): C;
async getItem(ctx: BaseContext): Promise<CollectionItem<C>> {
const {
items: [item],
} = await this.getCollection(ctx)
.list(ctx.$context)
.ids([await this.getID(ctx)])
.fetch();
if (!item) {
throw new Error("Couldn't get item of id " + (await this.getID(ctx)));
}
return item;
}
abstract getJDDFieldName(): FieldNames<C["fields"]>;
mount(router: Router, path: string) {
super.mount(router, path);
router.post(path + "save/", async (ctx) => {
const { state } = await this.extractState(ctx);
const item = await this.getItem(ctx);
item.set(
this.getJDDFieldName(),
(
await documentToStorage(registry, makeJDDContext(ctx), {
value: state.components,
} as unknown as JDDocumentContainer<"parsed">)
).value as any
);
await item.save(ctx.$context);
ctx.type = "html";
ctx.status = 422;
if (!state.messages) {
state.messages = [];
}
state.messages.push("Saved!");
ctx.body = this.render(ctx, state);
});
}
async renderHeader(_ctx: BaseContext, _item: CollectionItem<C>) {
return <h1>Edit JDD</h1>;
}
async renderPreParameterButtons(ctx: BaseContext) {
const item = await this.getItem(ctx);
return <div>{this.renderHeader(ctx, item)}</div>;
}
renderParameterButtons(state: JDDPageState) {
{
/*The below button has to be here in order for it to be the default behavior */
}
return (
<div>
<input type="submit" value="Preview" />
<select name="component">
{Object.keys(this.getRegistryComponents()).map((cmp) => (
<option value={cmp}>{cmp}</option>
))}
</select>
{this.makeActionButton(state, {
action: "add_component",
label: "Add component",
})}
<input type="submit" formaction="./save/" value="zapisz" />
</div>
);
}
async getInitialState(ctx: BaseContext) {
const article = await this.getItem(ctx);
const parsed_document = await documentToParsed(
registry,
makeJDDContext(ctx),
documentContainerFromStorage(
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
(article.get(this.getJDDFieldName()) as RawJDDocument) || []
)
);
return {
components: parsed_document.value.map((e) => ({ ...e, open: true })),
};
}
// uncomment to create whitelist of allowed components
// getAllowedComponents() {
// return ["nice-box"];
// }
}

File Metadata

Mime Type
text/x-java
Expires
Tue, May 27, 23:48 (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
714103
Default Alt Text
edit-jdd-field.tsx (3 KB)

Event Timeline