diff --git a/comps/form/gen/fields.ts b/comps/form/gen/fields.ts index 517ef20..c12179c 100755 --- a/comps/form/gen/fields.ts +++ b/comps/form/gen/fields.ts @@ -109,6 +109,49 @@ export const newField = async ( } else if (["has-many", "has-one"].includes(field.type) && field.relation) { const fields = parseGenField(opt.value); const res = generateSelect(fields); + let link_params = { where: "", create: "", update: "" }; + let rel = field.relation; + let label = formatName(field.name); + + if (field.relation) { + const rels = field.relation.fields.filter((e) => e.relation); + if (rels.length === 1) { + rel = rels[0].relation as any; + label = formatName(rel.to.table); + + link_params = { + where: `{ + "${rel.from.table}": { + some: { + "${field.relation.to.fields[0]}": fm.data["${field.relation.from.fields[0]}"], + } + } + } as Prisma.${rel.to.table}WhereInput`, + create: `{ + "${rel.from.table}": { + create: { + "${field.relation.to.fields[0]}": fm.data["${field.relation.from.fields[0]}"] + } + }, + } as Prisma.${rel.to.table}CreateInput`, + update: `{}`, + }; + } else { + link_params = { + where: `{ + "${rel.to.fields[0]}": fm.data["${field.relation.from.fields[0]}"], + } as Prisma.${rel.to.table}WhereInput`, + create: `{ + "${rel.from.table}": { + connect: { + "${rel.from.fields[0]}": fm.data["${field.relation.from.fields[0]}"] + } + }, + } as Prisma.${rel.to.table}CreateInput`, + update: `{}`, + }; + } + } if (res && res.select && Object.keys(res.select).length === 1) { return createItem({ @@ -118,22 +161,59 @@ export const newField = async ( name: field.name, label: formatName(field.name), type: "link", - link_opt: [ - `({ - url: () => { - return ""; - }, - where: () => { - return {} as ${ - opt.parent_table - ? `Prisma.${opt.parent_table}WhereInput` - : `Record` - }; - }, - breadcrumbs: (existing: any[]) => { - return [...existing]; - }, -})`, + link__params: [ + `async (field: any) => { + return { + where: ${link_params.where}, + create: ${link_params.create}, + update: ${link_params.update} + }; +}`, + ], + link__text: [ + ` +({ Link }) => { + const rel = fm.data["${field.name}"]; + return ( + <> + {Array.isArray(rel) && ( +
+ {rel.length === 0 ? "No" : rel.length}{" "} + {rel.length > 1 ? "items" : "item"} +
+ )} + + {({ icon }) => { + return ( + <> +
Detail
{icon} + + ); + }} + + + ); +}`, + `({ Link }) => { +const rel = fm.data["${field.name}"]; +return (React.createElement(React.Fragment, null, +Array.isArray(rel) && (React.createElement("div", { className: cx("flex items-center border-r", css\`padding:0px 10px 0px 5px;margin-right:10px;\`) }, +rel.length === 0 ? "No" : rel.length, +" ", +rel.length > 1 ? "items" : "item")), +React.createElement(Link, null, ({ icon }) => { +return (React.createElement(React.Fragment, null, + React.createElement("div", null, "Detail"), + " ", + icon)); +}))); +}; +`, ], }, }, @@ -147,6 +227,7 @@ export const newField = async ( pks: {}, type: field.type === "has-many" ? "typeahead" : "dropdown", }); + if (["has-one"].includes(field.type)) { const rel__gen_fields = JSON.stringify( field.relation?.fields.map((e) => { @@ -238,49 +319,6 @@ export const newField = async ( type = "link"; } - let label = formatName(field.name); - let link_params = { where: "", create: "", update: "" }; - let rel = field.relation; - if (type === "link" && field.relation) { - const rels = field.relation.fields.filter((e) => e.relation); - if (rels.length === 1) { - rel = rels[0].relation as any; - label = formatName(rel.to.table); - - link_params = { - where: `{ - "${rel.from.table}": { - some: { - "${field.relation.to.fields[0]}": fm.data["${field.relation.from.fields[0]}"], - } - } - } as Prisma.${rel.to.table}WhereInput`, - create: `{ - "${rel.from.table}": { - create: { - "${field.relation.to.fields[0]}": fm.data["${field.relation.from.fields[0]}"] - } - }, - } as Prisma.${rel.to.table}CreateInput`, - update: `{}`, - }; - } else { - link_params = { - where: `{ - "${rel.to.fields[0]}": fm.data["${field.relation.from.fields[0]}"], - } as Prisma.${rel.to.table}WhereInput`, - create: `{ - "${rel.from.table}": { - connect: { - "${rel.from.fields[0]}": fm.data["${field.relation.from.fields[0]}"] - } - }, - } as Prisma.${rel.to.table}CreateInput`, - update: `{}`, - }; - } - } - return createItem({ component: { id: "32550d01-42a3-4b15-a04a-2c2d5c3c8e67", diff --git a/comps/md/gen/gen-table-list.ts b/comps/md/gen/gen-table-list.ts index b2c3bae..e24cb91 100755 --- a/comps/md/gen/gen-table-list.ts +++ b/comps/md/gen/gen-table-list.ts @@ -58,7 +58,6 @@ export const generateTableList = async ( Object.keys(table_prop).map((e) => { set(data, e, table_prop[e]); }); - console.log(data, table_prop); } } }; diff --git a/comps/ui/accordion.tsx b/comps/ui/accordion.tsx new file mode 100755 index 0000000..890ffa1 --- /dev/null +++ b/comps/ui/accordion.tsx @@ -0,0 +1,51 @@ +import { useLocal } from "lib/utils/use-local"; +import { FC } from "react"; + +export const Accordion: FC<{ + on_load: () => Promise; + PassProp: any; + child: any; +}> = ({ on_load, PassProp, child }) => { + const local = useLocal( + { list: [] as any[], loading: true, active: 0 }, + async () => { + local.list = await on_load(); + local.loading = false; + local.render(); + } + ); + + return ( + <> + {local.loading ? ( + {}} + active={-1} + idx={-1} + > + {child} + + ) : ( + local.list.map((item, idx) => { + return ( + { + local.active = idx; + local.render(); + }} + > + {child} + + ); + }) + )} + + ); +}; diff --git a/exports.tsx b/exports.tsx index c1a5a14..b75ce03 100755 --- a/exports.tsx +++ b/exports.tsx @@ -4,6 +4,10 @@ export { guessLabel } from "./utils/guess-label"; export { fetchLinkParams } from "./comps/form/field/type/TypeLink"; export { prasi_gen } from "./gen/prasi_gen"; +export const Accordion = lazify( + async () => (await import("@/comps/ui/accordion")).Accordion +); + export const Popover = lazify( async () => (await import("@/comps/custom/Popover")).Popover ); diff --git a/preset/menu/Layout.tsx b/preset/menu/Layout.tsx index a96f76d..4b2da2c 100755 --- a/preset/menu/Layout.tsx +++ b/preset/menu/Layout.tsx @@ -139,7 +139,6 @@ export const Layout: FC = (props) => { if (!w.user) { local.loading = true; loadSession(props.login_url || "/auth/login"); - console.log(w.user); } }