Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F10352573
collection-edit-form.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
collection-edit-form.ts
View Options
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
{
wrapAttribute
,
wrapKeyName
}
from
"../../utils/wrap-attributes.js"
;
export
async
function
editItemFormTemplate
(
edit_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
formFieldsVar
=
`
${
toPascalCase
(
collection_name
)
}
FormFields`
;
const
collectionVar
=
`
${
toPascalCase
(
collection_name
)
}
`
;
return
formTemplate
(
edit_action_name
,
newfilefullpath
,
{
success_message
:
"Changes saved!"
,
postimport
:
`import {
${
formFieldsVar
}
, get
${
toPascalCase
(
collection_name
)
}
FormControls }from "../shared.js";
import {
${
collectionVar
}
} from "src/back/collections/collections.js";
import {
${
list_action
}
URL } from "src/back/routes/urls.js";
import {tempstream} from "tempstream";
import type { FormDataValue } from "@sealcode/sealgen";
import type { CollectionInputWithAllKeys } from "sealious";
`
,
fields
:
`...
${
toPascalCase
(
collection_name
)
}
FormFields`
,
controls
:
`
new Controls.FormHeader("Edit
${
toPascalCase
(
collection_name
)
}
"),
...get
${
toPascalCase
(
collection_name
)
}
FormControls()`
,
can_access
:
`canAccess = async (ctx: Context) => {
const policy =
${
toPascalCase
(
collection_name
)
}
.getPolicy("edit");
const response = await policy.check(ctx.$context);
return { canAccess: response?.allowed || false, message: response?.reason || "" };
};`
,
onsubmit
:
`
async onSubmit(ctx: Context, formData: FormData) {
const fctx = this.makeFormControlContext(ctx, formData, true);
const id = await this.getID(ctx);
const {items: [item]} = await ctx.$app.collections["
${
toKebabCase
(
collection_name
)
}
"]
.list(ctx.$context)
.ids([id])
.fetch();
if (!item) {
throw new Error("Unknown id: " + id);
}
item.setMultipleExtraSafe(
{
${
collection_fields
.
map
((
field_info
)
=>
field_info
.
form_field
.
generateCreateValueGetter
(
field_info
,
{
form_field_types
:
"FormFieldTypes"
,
sealious_field
:
`
${
collectionVar
}
.fields
${
wrapAttribute
(
field_info
.
name
)
}
`
,
form_fields
:
"fields"
,
}
)
)}
} as CollectionInputWithAllKeys<typeof
${
collectionVar
}
>
);
await item.save(ctx.$context);
for(const field of Object.values(
${
formFieldsVar
}
)){
field.postSealiousEdit(ctx, item, formData)
}
}
`
,
other_methods
:
`
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
}
`
,
get_initial_values
:
`async getInitialValues(ctx: Context): Promise<{ [key in keyof typeof fields]: FormDataValue | undefined }> {
const id = await this.getID(ctx);
const {
items: [item],
} = await ctx.$app.collections["
${
toKebabCase
(
collection_name
)
}
"]
.list(ctx.$context)
.ids([id])
.attach({})
.fetch();
if (!item) {
throw new Error("Item with given id not found: " + id);
}
return {
${
collection_fields
.
map
((
field_info
)
=>
field_info
.
form_field
.
generateInitialValue
(
field_info
,
{
form_field_types
:
"FieldTypes"
,
form_fields
:
"fields"
,
}
)
)}
};
}
`
,
render
:
` async render(ctx: Context, data: FormData, show_field_errors: boolean) {
return html({
ctx,
title: "Edit
${
toPascalCase
(
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
Details
Attached
Mime Type
text/x-java
Expires
Sun, Nov 2, 16:57 (13 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1030446
Default Alt Text
collection-edit-form.ts (4 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment