Page MenuHomeSealhub

collection-create-form.ts
No OneTemporary

Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None

collection-create-form.ts

import { toKebabCase, toPascalCase } from "js-convert-case";
import { formTemplate } from "../form.js";
import { curryImportPath } from "../../utils/import-path.js";
import _locreq from "locreq";
import extract_fields_from_collection from "../../utils/extract-fields-from-collection.js";
export async function createItemFormTemplate(
create_action_name: string,
newfilefullpath: string,
collection_name: string,
list_action: string
): Promise<string> {
const collection_fields = await extract_fields_from_collection(
collection_name
);
const formFieldsVar = `${toPascalCase(collection_name)}FormFields`;
return formTemplate(create_action_name, newfilefullpath, {
success_message: "Created!",
postimport: `import {${formFieldsVar}, get${toPascalCase(
collection_name
)}FormControls }from "./shared.js";
import { ${toPascalCase(
collection_name
)} } from "src/back/collections/collections.js";
import { ${list_action}URL } from "src/back/routes/urls.js";
import {tempstream} from "tempstream";
`,
fields: `...${toPascalCase(collection_name)}FormFields`,
controls: `
new Controls.FormHeader("Create ${toPascalCase(
collection_name
)}"),
...get${toPascalCase(collection_name)}FormControls()`,
can_access: `canAccess = async (ctx: Context) => {
const policy = ${toPascalCase(collection_name)}.getPolicy("create");
const response = await policy.check(ctx.$context);
return { canAccess: response?.allowed || false, message: response?.reason || "" };
};`,
get_initial_values: "",
onsubmit: `
async onSubmit(ctx: Context, formData: FormData) {
const data = await this.getParsedValues(ctx);
if (!data) {
throw new Error("Error when parsing the form values");
}
const created = await ctx.$app.collections["${toKebabCase(
collection_name
)}"].create(
ctx.$context,
{
}
);
for(const field of Object.values(${formFieldsVar})){
field.postSealiousCreate(ctx, created, formData)
}
}
`,
render: ` async render(ctx: Context, data: FormData, show_field_errors: boolean) {
return html({
ctx,
title: "Create ${toKebabCase(collection_name)}",
body: tempstream/* HTML */ \` <div class="sealgen-crud-form">
<a class="" href="\${${list_action}URL}"
>← Back to ${collection_name} list</a
>
\${await super.render(ctx, data, show_field_errors)}
</div>\`,
description: "",
css_clumps: ["admin-forms"]
}
);
}`,
});
}

File Metadata

Mime Type
application/javascript
Expires
Fri, Nov 27, 15:32 (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1228977
Default Alt Text
collection-create-form.ts (2 KB)

Event Timeline