This commit is contained in:
rizky 2024-07-23 23:06:41 -07:00
parent 3034a12ded
commit 032d9187ce
5 changed files with 152 additions and 61 deletions

View File

@ -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<string, any>`
link__params: [
`async (field: any) => {
return {
where: ${link_params.where},
create: ${link_params.create},
update: ${link_params.update}
};
},
breadcrumbs: (existing: any[]) => {
return [...existing];
},
})`,
}`,
],
link__text: [
`
({ Link }) => {
const rel = fm.data["${field.name}"];
return (
<>
{Array.isArray(rel) && (
<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"}
</div>
)}
<Link>
{({ icon }) => {
return (
<>
<div>Detail</div> {icon}
</>
);
}}
</Link>
</>
);
}`,
`({ 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",

View File

@ -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);
}
}
};

51
comps/ui/accordion.tsx Executable file
View File

@ -0,0 +1,51 @@
import { useLocal } from "lib/utils/use-local";
import { FC } from "react";
export const Accordion: FC<{
on_load: () => Promise<any[]>;
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 ? (
<PassProp
loading={true}
item={{}}
activate={() => {}}
active={-1}
idx={-1}
>
{child}
</PassProp>
) : (
local.list.map((item, idx) => {
return (
<PassProp
loading={false}
item={item}
idx={idx}
key={idx}
active={local.active}
activate={() => {
local.active = idx;
local.render();
}}
>
{child}
</PassProp>
);
})
)}
</>
);
};

View File

@ -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
);

View File

@ -139,7 +139,6 @@ export const Layout: FC<LYTChild> = (props) => {
if (!w.user) {
local.loading = true;
loadSession(props.login_url || "/auth/login");
console.log(w.user);
}
}