Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22395686
create.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
create.ts
View Options
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-explicit-any */
import
type
{
Context
}
from
"koa"
;
import
{
Collection
,
ExtractFieldInput
}
from
"sealious"
;
import
type
{
FormData
,
FormField
,
HTMLFunc
}
from
"@sealcode/sealgen"
;
import
{
Form
,
Controls
}
from
"@sealcode/sealgen"
;
import
{
tempstream
}
from
"tempstream"
;
import
type
{
FormControl
}
from
"@sealcode/sealgen/@types/src/forms/controls/form-control"
;
export
default
class
CRUDCreatePage
<
C
extends
Collection
,
Fields
extends
Record
<
keyof
Collection
[
"fields"
],
FormField
>
>
extends
Form
<
Fields
,
void
>
{
defaultSuccessMessage
=
"Formularz wypełniony poprawnie"
;
collection
:
C
;
controls
:
FormControl
[];
// overwritten below;
constructor
(
public
args
:
{
collection
:
C
;
fields
:
Fields
;
controls
:
FormControl
[];
create_title
:
string
;
html
:
HTMLFunc
;
base_url
:
string
;
back_to_list_button_text
:
string
;
form_value_to_sealious_value
:
{
[
field
in
keyof
C
[
"fields"
]]
?:
(
data
:
unknown
)
=>
ExtractFieldInput
<
C
[
"fields"
][
field
]
>
;
};
}
)
{
super
();
this
.
fields
=
args
.
fields
;
this
.
collection
=
args
.
collection
;
this
.
controls
=
[
new
Controls
.
FormHeader
(
this
.
args
.
create_title
),
...
this
.
args
.
controls
,
];
}
async
onSubmit
(
ctx
:
Context
)
{
const
data
=
await
this
.
getParsedValues
(
ctx
);
if
(
!
data
)
{
throw
new
Error
(
"Error when parsing the form values"
);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment
const
obj
=
Object
.
fromEntries
(
Object
.
entries
(
this
.
collection
.
fields
).
map
(([
field_name
])
=>
[
field_name
,
this
.
args
.
form_value_to_sealious_value
[
field_name
]
?
.(
data
[
field_name
]
)
as
any
,
])
)
as
any
;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await
this
.
collection
.
create
(
ctx
.
$context
,
obj
);
}
canAccess
=
async
(
ctx
:
Context
)
=>
{
const
policy
=
this
.
collection
.
getPolicy
(
"create"
);
const
response
=
await
policy
.
check
(
ctx
.
$context
);
return
{
canAccess
:
response
?
.
allowed
||
false
,
message
:
response
?
.
reason
||
""
,
};
};
async
renderNavbar
(
_ctx
:
Context
)
{
return
/* HTML */
`<a class="" href="
${
this
.
args
.
base_url
}
"
>
${
this
.
args
.
back_to_list_button_text
}
</a
>`
;
}
async
render
(
ctx
:
Context
,
data
:
FormData
,
show_field_errors
:
boolean
)
{
return
this
.
args
.
html
({
ctx
,
title
:
this
.
args
.
create_title
,
description
:
""
,
body
:
tempstream
/* HTML */
` <div class="crud-ui-create">
${
this
.
renderNavbar
(
ctx
)
}
${
await
super
.
render
(
ctx
,
data
,
show_field_errors
)
}
</div>`
,
});
}
}
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 16:28 (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1229131
Default Alt Text
create.ts (2 KB)
Attached To
Mode
rCUI crud-ui
Attached
Detach File
Event Timeline