Page MenuHomeSealhub

list.ts
No OneTemporary

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 } from "sealious";
import { FlatTemplatable, TempstreamJSX } 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 ${uppercase_collection})["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 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 <form>
{Object.entries(query_params).map(([key, value]) => {
if (key == "filter") {
return "";
}
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 renderItem(_: Context, item: CollectionItem<typeof ${uppercase_collection}>) {
return <tr>
{displayFields.map(({ field, format }) => {
const value = item.get(field as any);
return <td>{format ? format(value) : value}</td>;
})}
</tr>;
}
async render(ctx: Context) {
return html(
ctx,
"${action_name}",
<div>
<h2>${action_name} List</h2>
<table class="sealious-list">
{this.renderTableHead(ctx, displayFields)}
<tbody>{super.render(ctx)}</tbody>
</table>
</div>
);
}
})(${uppercase_collection});
`;
};

File Metadata

Mime Type
text/html
Expires
Tue, Dec 24, 14:02 (21 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557120
Default Alt Text
list.ts (3 KB)

Event Timeline