Page MenuHomeSealhub

collection-create-form.ts
No OneTemporary

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";
import { getFieldHandler } from "./shared-crud-form-fields.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 importPath = curryImportPath(newfilefullpath);
const field_handler_results = await Promise.all(
collection_fields.map((field) =>
getFieldHandler(collection_name, field, importPath)
)
);
const post_create_actions = field_handler_results
.map(({ post_create }) => post_create)
.filter((e) => e != "");
return formTemplate(create_action_name, newfilefullpath, {
postimport: `import {${toPascalCase(
collection_name
)}FormFields, ${toPascalCase(
collection_name
)}FormControls }from "./shared.js";
import { ${toPascalCase(collection_name)} } from "${importPath(
"src/back/collections/collections.js"
)}";
import { ${list_action}URL } from "${importPath("src/back/routes/urls.js")}";
import {tempstream} from "tempstream";
`,
fields: `...${toPascalCase(collection_name)}FormFields`,
controls: `
new Controls.FormHeader("Create ${toPascalCase(
collection_name
)}"),
...${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: field_handler_results
.filter((r) => !r.hide_initial_value_create)
.map(({ initial_value_create }) => initial_value_create)
.filter((e) => e != "").length
? `getInitialValues(ctx: Context){
return {
${field_handler_results
.filter((r) => !r.hide_initial_value_create)
.map(
({ initial_value_create }) =>
initial_value_create
)
.join(",\n")}
}
}`
: ``,
onsubmit: `
async onSubmit(ctx: Context) {
const data = await this.getParsedValues(ctx);
if (!data) {
throw new Error("Error when parsing the form values");
}
${field_handler_results
.map(({ pre_create }) => pre_create)
.filter((e) => e !== "")
.join("\n")}
${
post_create_actions.length ? `const created = ` : ""
} await ctx.$app.collections["${toKebabCase(collection_name)}"].create(
ctx.$context,
{
${field_handler_results
.filter((result) => !result.hide_sealious_value)
.map((result) => result.sealious_value(result))
.filter((e) => e != "")
.join(",\n")}
}
);
${
post_create_actions.length
? `/* ==== POST CREATE ACTIONS ==== */`
: ""
}
${post_create_actions.join("\n")}
}
`,
render: ` async render(ctx: Context, data: FormData, show_field_errors: boolean) {
return html(
ctx,
"Create ${toKebabCase(collection_name)}",
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>\`
);
}`,
});
}

File Metadata

Mime Type
text/x-java
Expires
Wed, May 7, 19:45 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
635682
Default Alt Text
collection-create-form.ts (3 KB)

Event Timeline