Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F9583698
list.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
list.ts
View Options
import
_locreq
from
"locreq"
;
import
{
curryImportPath
}
from
"../utils/import-path.js"
;
import
prompts
from
"prompts"
;
import
{
listCollections
}
from
"../utils/list-collections.js"
;
import
{
extractCollectionClassname
}
from
"../generate-collections.js"
;
import
extract_fields_from_collection
from
"../utils/extract-fields-from-collection.js"
;
import
{
DefaultListFilters
}
from
"../page/default-list-filters.js"
;
import
{
formatWithPrettier
}
from
"../utils/prettier.js"
;
import
{
toKebabCase
}
from
"js-convert-case"
;
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
importPath
=
curryImportPath
(
newfilefullpath
);
const
result
=
`import type { Context } from "koa";
import type { CollectionItem, ItemListResult } from "sealious";
import type { FlatTemplatable } from "tempstream";
import { TempstreamJSX } from "tempstream";
import {
${
uppercase_collection
}
} from "
${
importPath
(
target_locreq
.
resolve
(
"src/back/collections/collections.ts"
)
)
}
";
import html from "
${
importPath
(
target_locreq
.
resolve
(
"src/back/html.ts"
))
}
";
import type { ListFilterRender } from "@sealcode/sealgen";
import {
SealiousItemListPage,
BaseListPageFields,
DefaultListFilters,
} from "@sealcode/sealgen";
import qs from "qs";
import type { FilePointer } from "@sealcode/file-manager";
import { imageRouter } from "
${
importPath
(
target_locreq
.
resolve
(
"src/back/image-router.ts"
)
)
}
";
export const actionName = "
${
action_name
}
";
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const filterFields = [
${
collection_fields
.
map
(
(
field
)
=>
` { field: "
${
field
.
name
}
", ...
${
DefaultListFilters
[
field
.
type
]
?
`DefaultListFilters["
${
field
.
type
}
"]`
:
`DefaultListFilters.fallback`
}
}`
)
.
join
(
",\n"
)
}
] as {
field: keyof (typeof
${
uppercase_collection
}
)["fields"];
render?: ListFilterRender;
prepareValue?: (filter_value: unknown)=>unknown; // set this function to change what filter value is passed to Sealious
}[];
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
(
" "
);
},
}
`;
}
if (field.type == "image") {
return `
{
field
:
"${field.name}"
,
label
:
"${field.name}"
,
format
:
async
(
value
:
FilePointer
)
=>
{
return
imageRouter
.
image
(
await
value
.
getPath
(),
{
container
:
{
width
:
45
,
height
:
45
},
alt
:
""
,
});
},
}
`;
}
return `
{
field
:
"${field.name}"
,
label
:
"${field.name}"
}
`;
})
.join(",\n")}
] as {
field: string;
label: string;
format?: (value: unknown, item: CollectionItem<typeof Opinions>) => FlatTemplatable;
}[];
export default new (class
${
action_name
}
Page extends SealiousItemListPage<
typeof
${
uppercase_collection
}
,
typeof BaseListPageFields
> {
fields = BaseListPageFields;
async renderFilters(ctx: Context): Promise<FlatTemplatable> {
const query_params = qs.parse(ctx.search.slice(1));
query_params.page = "1";
const filter_values = await super.getFilterValues(ctx);
return <form>
{Object.entries(query_params).map(([key, value]) => {
if (key == "filter") {
return "";
}
// this is necessary to not lose any query params when the user changes the filter values
return <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) {
// adding opportunity to adjust the values for a given field filter before it's sent to Sealious
const values = await super.getFilterValues(ctx);
for (const filterField of filterFields) {
const key = filterField.field as keyof typeof values;
if (key in values) {
const prepare_fn = filterField.prepareValue;
if (prepare_fn) {
values[key] = prepare_fn(values[key]) as any;
}
}
}
return values;
}
async renderItem(ctx: Context, item: CollectionItem<typeof
${
uppercase_collection
}
>) {
return <tr>
{displayFields.map(({ field, format }) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
const value = item.get(field as any);
return <td>{format ? format(value, item) : value}</td>;
})}
</tr>;
}
renderListContainer(ctx: Context, content: Templatable): FlatTemplatable {
return (
<table class="sealious-list
${
toKebabCase
(
action_name
)
}
-table">
{this.renderTableHead(ctx, displayFields)}
<tbody>{content}</tbody>
</table>
);
}
async render(ctx: Context) {
return html(
ctx,
"
${
action_name
}
",
<div class="sealious-list-wrapper
${
toKebabCase
(
action_name
)
}
--wrapper">
<h2>
${
action_name
}
List</h2>
{super.render(ctx)}
</div>
);
}
})(
${
uppercase_collection
}
);
`
;
return
formatWithPrettier
(
result
);
};
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sat, Oct 11, 10:27 (22 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
982907
Default Alt Text
list.ts (5 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment