Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F10352835
edit.form.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
edit.form.ts
View Options
import
type
{
Context
}
from
"koa"
;
import
type
{
FormData
}
from
"@sealcode/sealgen"
;
import
{
Form
,
Controls
,
fieldsToShape
}
from
"@sealcode/sealgen"
;
import
html
from
"../../../../html.js"
;
import
{
NavbarLinksFormFields
,
NavbarLinksFormControls
}
from
"../shared.js"
;
import
{
NavbarLinks
}
from
"../../../../collections/collections.js"
;
import
{
NavbarLinksCRUDListURL
}
from
"../../../urls.js"
;
import
{
tempstream
}
from
"tempstream"
;
import
{
withFallback
}
from
"@sealcode/sealgen"
;
export
const
actionName
=
"NavbarLinksCRUDEdit"
;
const
fields
=
{
...
NavbarLinksFormFields
,
};
export
const
NavbarLinksCRUDEditShape
=
fieldsToShape
(
fields
);
export
default
new
(
class
NavbarLinksCRUDEditForm
extends
Form
<
typeof
fields
,
void
>
{
defaultSuccessMessage
=
"Formularz wypełniony poprawnie"
;
fields
=
fields
;
controls
=
[
new
Controls
.
FormHeader
(
"Edit NavbarLinks"
),
...
NavbarLinksFormControls
];
async
getID
(
ctx
:
Context
)
:
Promise
<
string
>
{
// trying to automatically guess which param is the one that represents the ID
// of the resource to edit.
// If sealgen got it wrong, just return the param name that should be used here instead
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
[
"navbar-links"
]
.
list
(
ctx
.
$context
)
.
ids
([
id
])
.
attach
({})
.
fetch
();
if
(
!
item
)
{
throw
new
Error
(
"Item with given id not found: "
+
id
);
}
return
{
label
:
withFallback
(
item
.
get
(
"label"
),
""
),
href
:
withFallback
(
item
.
get
(
"href"
),
""
),
};
}
async
onSubmit
(
ctx
:
Context
)
{
const
data
=
await
this
.
getParsedValues
(
ctx
);
const
id
=
await
this
.
getID
(
ctx
);
const
{
items
:
[
item
],
}
=
await
ctx
.
$app
.
collections
[
"navbar-links"
]
.
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"
);
}
item
.
setMultiple
({
label
:
data
[
"label"
]
!=
null
?
data
[
"label"
]
:
""
,
href
:
data
[
"href"
]
!=
null
?
data
[
"href"
]
:
""
,
});
await
item
.
save
(
ctx
.
$context
);
}
canAccess
=
async
(
ctx
:
Context
)
=>
{
const
policy
=
NavbarLinks
.
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 NavbarLinks"
,
body
:
tempstream
/* HTML */
` <div class="sealgen-crud-form">
<a class="" href="
${
NavbarLinksCRUDListURL
}
"
>← Back to navbar-links 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, 21:36 (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1030534
Default Alt Text
edit.form.ts (2 KB)
Attached To
Mode
rAPROXY Assetproxy
Attached
Detach File
Event Timeline
Log In to Comment