Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22396350
list.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
4 KB
Referenced Files
None
Subscribers
None
list.ts
View Options
import
_locreq
from
"locreq"
;
import
{
curryImportPath
}
from
"../utils/import-path"
;
import
prompts
=
require
(
"prompts"
);
import
{
listCollections
}
from
"../utils/list-collections"
;
import
{
extractCollectionClassname
}
from
"../generate-collections"
;
import
extract_fields_from_collection
from
"../utils/extract-fields-from-collection"
;
import
{
DefaultListFilters
}
from
"../page/default-list-filters"
;
const
target_locreq
=
_locreq
(
process
.
cwd
());
export
const
listTemplate
=
async
(
action_name
:
string
,
newfilefullpath
:
string
)
:
Promise
<
string
>
=>
{
const
response
=
await
prompts
({
type
:
"autocomplete"
,
name
:
"collection"
,
message
:
"Which sealious collection do you want to list?"
,
choices
:
(
await
listCollections
()
).
map
((
collection
)
=>
({
title
:
collection
,
value
:
collection
,
})),
});
const
collection_name
=
response
.
collection
as
string
;
const
[
uppercase_collection
,
collection_fields
]
=
await
Promise
.
all
([
extractCollectionClassname
(
target_locreq
.
resolve
(
"src/back/collections/"
+
collection_name
+
".ts"
)
),
extract_fields_from_collection
(
collection_name
),
]);
const
rel
=
curryImportPath
(
newfilefullpath
);
return
`import { Context } from "koa";
import { CollectionItem, Field } from "sealious";
import { FlatTemplatable, tempstream } from "tempstream";
import {
${
uppercase_collection
}
} from "
${
rel
(
"src/back/collections/collections.js"
)
}
";
import html from "
${
rel
(
"src/back/html.js"
)
}
";
import {
SealiousItemListPage,
BaseListPageFields,
DefaultListFilters,
ListFilterRender,
} from "@sealcode/sealgen";;
import qs from "qs";
import { is, predicates } from "@sealcode/ts-predicates";
export const actionName = "
${
action_name
}
";
const filterFields = [
${
collection_fields
.
map
(
(
field
)
=>
` { field: "
${
field
.
name
}
", render:
${
DefaultListFilters
[
field
.
type
]
?
`DefaultListFilters["
${
field
.
type
}
"]`
:
`DefaultListFilters.fallback`
}
}`
)
.
join
(
",\n"
)
}
] as {
field: keyof (typeof PatronEvents)["fields"];
render?: ListFilterRender;
}[];
const displayFields = [
${
collection_fields
.
map
((
field
)
=>
{
if
(
field
.
type
==
"boolean"
)
{
return
` {
field: "
${
field
.
name
}
",
label: "
${
field
.
name
}
",
format: (v: boolean) => (v ? "YES" : "NO"),
}`
;
}
if (field.type == "datetime") {
return `
{
field
:
"${field.name}"
,
label
:
"${field.name}"
,
format
:
(
v
:
number
)
=>
{
const
d
=
new
Date
();
d
.
setTime
(
v
);
return
d
.
toString
().
split
(
" "
).
slice
(
1
,
5
).
join
(
" "
);
},
}
`;
}
return `
{
field
:
"${field.name}"
,
label
:
"${field.name}"
}
`;
})
.join(",\n")}
];
export default new (class
${
action_name
}
Page extends SealiousItemListPage<
typeof
${
uppercase_collection
}
,
typeof BaseListPageFields
> {
fields = BaseListPageFields;
async render(ctx: Context) {
return html(
ctx,
"
${
action_name
}
",
tempstream/* HTML */ \`<div>
<h2>
${
action_name
}
</h2>
<table class="sealious-list">
\${this.renderTableHead(ctx, displayFields)}
<tbody>
\${super.render(ctx)}
</tbody>
</table>
</div>\`
);
}
async renderFilters(ctx: Context): Promise<FlatTemplatable> {
const query_params = qs.parse(ctx.search.slice(1));
query_params.page = "1";
const filter_values = await this.getFilterValues(ctx);
return tempstream/* HTML */ \`<form>
\${Object.entries(query_params).map(([key, value]) => {
if (key == "filter") {
return "";
}
return /* HTML */ \`<input
type="hidden"
name="\${key}"
value="\${value}"
/>\`;
})}
\${filterFields.map(({ field, render }) => {
if (!render) {
render = DefaultListFilters.fallback;
}
return (
render(filter_values[field] || "", this.collection.fields[field]) ||
""
);
})}
<input type="submit" />
</form>\`;
}
async getFilterValues(ctx: Context) {
const query_params = qs.parse(ctx.search.slice(1));
const _filter_values = query_params?.filter || {};
let filter_values: Record<string, string | undefined> = is(
_filter_values,
predicates.record(predicates.string, predicates.string)
)
? _filter_values
: {};
return filter_values;
}
async renderItem(_: Context, item: CollectionItem<typeof
${
uppercase_collection
}
>) {
return tempstream/* HTML */ \`<tr>
\${displayFields.map(({ field, format }) => {
const value = item.get(field as any);
return tempstream\`<td>\${format ? format(value) : value}</td>\`;
})}
</tr>\`;
}
})(
${
uppercase_collection
}
);
`
;
};
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 16:43 (1 d, 23 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1202411
Default Alt Text
list.ts (4 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline