import { unique } from "radashi"; import { z } from "zod"; import { EntityManager } from "../../entity/entity-manager"; import { type EntityNamesRecord } from "../../entity/entity-manager"; import { type RenderingNode, type TemplateOptions } from "../../types/types"; import { getEnumInfoFromColName, getRelationNameFromColumnName, getRelationPropFromColName, } from "../helpers"; import { Template } from "../template"; export class Template__view_form extends Template { constructor() { super("view_form"); } getTargetAndPath(names: EntityNamesRecord) { return { target: "web/src/routes/admin", path: `${names.fsPlural}/form.tsx`, }; } override getRequiredDictKeys(): string[] | null { return ["entity.create", "entity.edit", "common.backToList", "form.createdAt", "common.save"]; } wrapFC(body: string, label?: string): string { return [ `
${label ? `\n ` : ""}`, ` ${body}`, `
`, ].join("\n"); } wrapFG(body: string, label?: string): string { return this.wrapFC(body, label); } renderColumnImport(entityId: string, col: RenderingNode) { if (col.renderType === "enums") { const { id } = getEnumInfoFromColName(entityId, col.name); return { type: "enum" as const, enumId: id }; } else if (col.renderType === "number-fk_id" || col.renderType === "string-fk_id") { try { const relProp = getRelationPropFromColName(entityId, col.name.replace("_id", "")); return { type: "fk" as const, entityId: relProp.with }; } catch { return null; } } else if (col.renderType === "array" && col.name.endsWith("_ids")) { // ManyToMany relation의 FK 배열 try { const baseName = col.name.replace(/_ids$/, ""); const relProp = getRelationPropFromColName(entityId, baseName); return { type: "fk" as const, entityId: relProp.with }; } catch { return null; } } return null; } renderColumn(entityId: string, col: RenderingNode): string { const regExpr = `{...register("${col.name}")}`; const placeholderName = getRelationNameFromColumnName(entityId, col.name); const placeholder = `{SD("entity.${entityId}.${placeholderName}")}`; switch (col.renderType) { case "string-plain": if (col.zodType instanceof z.ZodString && (col.zodType.maxLength ?? 0) <= 256) { return ``; } else { return `