fix master

This commit is contained in:
rizky 2024-07-20 21:00:56 -07:00
parent 9b0fcd40e8
commit 01d213326e
15 changed files with 259 additions and 84 deletions

View File

@ -50,6 +50,9 @@ export const Breadcrumb: FC<BreadcrumbProps> = ({ value, className }) => {
<FieldLoading /> <FieldLoading />
) : ( ) : (
<> <>
{(!local.value || local.value.length === 0) &&
isEditor &&
"Breadcrumb"}
{(Array.isArray(local.value) ? local.value : []).map( {(Array.isArray(local.value) ? local.value : []).map(
(cur, index): ReactNode => { (cur, index): ReactNode => {
const lastIndex = (local.value || []).length - 1; const lastIndex = (local.value || []).length - 1;

View File

@ -19,6 +19,7 @@ export const Form: FC<FMProps> = (props) => {
data: editorFormData[props.item.id] data: editorFormData[props.item.id]
? editorFormData[props.item.id].data ? editorFormData[props.item.id].data
: {}, : {},
deps: {},
status: "init", status: "init",
reload: async () => { reload: async () => {
formReload(fm); formReload(fm);
@ -130,8 +131,7 @@ export const Form: FC<FMProps> = (props) => {
} }
}, [getPathname()]); }, [getPathname()]);
useEffect( useEffect(() => {
() => {
if (fm.status === "ready") { if (fm.status === "ready") {
fm.status = "init"; fm.status = "init";
fm.render(); fm.render();
@ -155,9 +155,9 @@ export const Form: FC<FMProps> = (props) => {
} }
i++; i++;
}, 10); }, 10);
}, }, Object.values(props.deps) || []);
Array.isArray(props.on_load_deps) ? props.on_load_deps : []
); fm.deps = props.deps;
if (fm.status === "init") { if (fm.status === "init") {
formInit(fm, props); formInit(fm, props);

View File

@ -1,3 +1,5 @@
import { BreadItem } from "lib/comps/custom/Breadcrumb";
import { MDLocal } from "lib/comps/md/utils/typings";
import { FieldLoading, Spinner } from "lib/comps/ui/field-loading"; import { FieldLoading, Spinner } from "lib/comps/ui/field-loading";
import { hashSum } from "lib/utils/hash-sum"; import { hashSum } from "lib/utils/hash-sum";
import { getPathname } from "lib/utils/pathname"; import { getPathname } from "lib/utils/pathname";
@ -6,6 +8,8 @@ import { ArrowUpRight } from "lucide-react";
import { FC, ReactNode, useEffect } from "react"; import { FC, ReactNode, useEffect } from "react";
import { FMLocal, FieldLocal, FieldProp } from "../../typings"; import { FMLocal, FieldLocal, FieldProp } from "../../typings";
const link_cache = {} as Record<string, any>;
export const FieldLink: FC<{ export const FieldLink: FC<{
field: FieldLocal; field: FieldLocal;
fm: FMLocal; fm: FMLocal;
@ -14,8 +18,8 @@ export const FieldLink: FC<{
const local = useLocal({ const local = useLocal({
text: "", text: "",
init: false, init: false,
navigating: false,
custom: false, custom: false,
md: null as null | MDLocal,
}); });
const Link = ({ const Link = ({
@ -23,10 +27,13 @@ export const FieldLink: FC<{
}: { }: {
children: (arg: { icon: any }) => ReactNode; children: (arg: { icon: any }) => ReactNode;
}) => { }) => {
const link_local = useLocal({
navigating: false,
});
return ( return (
<div <div
className={cx( className={cx(
"c-flex-1 c-my-1 c-px-2 c-rounded-md c-flex c-items-center cursor-pointer c-space-x-1 c-transition-all", "c-my-1 c-px-2 c-rounded-md c-flex c-items-center cursor-pointer c-space-x-1 c-transition-all",
css` css`
border: 1px solid #aaa; border: 1px solid #aaa;
&:hover { &:hover {
@ -36,17 +43,17 @@ export const FieldLink: FC<{
)} )}
onClick={async () => { onClick={async () => {
if (!isEditor) { if (!isEditor) {
local.navigating = true; link_local.navigating = true;
local.render(); link_local.render();
if (!(await navigateLink(arg.link, field))) { if (!(await navigateLink(arg.link, field, fm.deps.md))) {
local.navigating = false; link_local.navigating = false;
local.render(); link_local.render();
} }
} }
}} }}
> >
{children({ {children({
icon: local.navigating ? ( icon: link_local.navigating ? (
<Spinner <Spinner
className={cx( className={cx(
css` css`
@ -68,6 +75,7 @@ export const FieldLink: FC<{
local.text = arg.link.text; local.text = arg.link.text;
local.init = true; local.init = true;
} else if (typeof arg.link.text === "function") { } else if (typeof arg.link.text === "function") {
local.custom = true;
const res = arg.link.text({ const res = arg.link.text({
field, field,
Link, Link,
@ -84,13 +92,18 @@ export const FieldLink: FC<{
} }
} }
} }
local.render(); local.render();
}, []); }, [arg.link.params]);
return ( return (
<div className={cx("c-px-2")}> <div className={cx("c-px-2 c-flex-1 c-flex")}>
{!local.init ? ( {!local.init ? (
<FieldLoading height="short" /> <FieldLoading height="short" />
) : (
<>
{local.custom ? (
local.text
) : ( ) : (
<Link> <Link>
{({ icon }) => { {({ icon }) => {
@ -103,6 +116,8 @@ export const FieldLink: FC<{
}} }}
</Link> </Link>
)} )}
</>
)}
</div> </div>
); );
}; };
@ -110,6 +125,8 @@ export const FieldLink: FC<{
export type LinkParam = { export type LinkParam = {
url: string; url: string;
where: any; where: any;
create: any;
update: any;
hash: any; hash: any;
prefix: { prefix: {
label: any; label: any;
@ -118,8 +135,12 @@ export type LinkParam = {
}[]; }[];
}; };
const navigateLink = async (link: FieldProp["link"], field: FieldLocal) => { const navigateLink = async (
let params = link.params(field); link: FieldProp["link"],
field: FieldLocal,
md?: MDLocal
) => {
let params = link.params(field) as { where: any; create: any; update: any };
if (typeof params === "object") { if (typeof params === "object") {
if (params instanceof Promise) { if (params instanceof Promise) {
@ -127,17 +148,19 @@ const navigateLink = async (link: FieldProp["link"], field: FieldLocal) => {
} }
} }
const md = params.md;
const where = params.where;
const prefix: LinkParam["prefix"] = []; const prefix: LinkParam["prefix"] = [];
if (md) { if (md) {
md.header.render(); let bread: BreadItem[] = [];
if (md.header.breadcrumb.length > 0) { if (md.selected && md.header.child.breadcrumb) {
bread = md.header.child.breadcrumb();
} else if (!md.selected && md.header.master.breadcrumb) {
bread = md.header.master.breadcrumb();
}
if (bread.length > 0) {
const path = getPathname({ hash: false }); const path = getPathname({ hash: false });
let i = 0; let i = 0;
for (const b of md.header.breadcrumb) { for (const b of bread) {
prefix.push({ prefix.push({
label: b.label, label: b.label,
url: `${path}`, url: `${path}`,
@ -152,14 +175,16 @@ const navigateLink = async (link: FieldProp["link"], field: FieldLocal) => {
} }
const values: LinkParam = { const values: LinkParam = {
...params,
url: getPathname({ hash: false }), url: getPathname({ hash: false }),
where,
prefix, prefix,
hash: "", hash: "",
}; };
const vhash = hashSum(values); const vhash = hashSum(values);
values.hash = vhash; values.hash = vhash;
link_cache[vhash] = values;
if (!link.url) { if (!link.url) {
alert("No URL defined!"); alert("No URL defined!");
return false; return false;
@ -187,10 +212,21 @@ export const parseLink = () => {
} }
return []; return [];
}; };
export const fetchLinkParams = async ( export const fetchLinkParams = async (
parsed_link?: ReturnType<typeof parseLink> parsed_link?: ReturnType<typeof parseLink>
) => { ) => {
const parsed = parsed_link || parseLink(); const parsed = parsed_link || parseLink();
return await Promise.all(parsed.map((e) => api._kv("get", e))); return await Promise.all(
parsed.map(async (e) => {
if (link_cache[e]) {
return link_cache[e];
}
const result = await api._kv("get", e);
link_cache[e] = result;
return result;
})
);
}; };

View File

@ -218,9 +218,10 @@ export const newField = async (
return v; return v;
}) })
); );
let type = "multi-option";
let sub_type = "typeahead"; let sub_type = "typeahead";
if (field.relation?.fields?.length > 2) { if (field.relation?.fields.filter((e) => !e.is_pk)?.length >= 2) {
sub_type = "table-edit"; sub_type = "table-edit";
child = createItem({ child = createItem({
childs: await generateRelation( childs: await generateRelation(
@ -233,15 +234,115 @@ export const newField = async (
false false
), ),
}); });
} else {
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({ return createItem({
component: { component: {
id: "32550d01-42a3-4b15-a04a-2c2d5c3c8e67", id: "32550d01-42a3-4b15-a04a-2c2d5c3c8e67",
props: { props: {
name: field.name, type,
label: formatName(field.name),
type: "multi-option",
sub_type, sub_type,
name: field.name,
label,
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));
})));
};
`,
],
link__params: [
`async (field: any) => {
return {
where: ${link_params.where},
create: ${link_params.create},
update: ${link_params.update}
};
}`,
],
rel__gen_table: field.name, rel__gen_table: field.name,
opt__on_load: [result.on_load], opt__on_load: [result.on_load],
ext__show_label: show ? "y" : "n", ext__show_label: show ? "y" : "n",

View File

@ -207,14 +207,8 @@ ${
`\ `\
if (typeof md !== "undefined") { if (typeof md !== "undefined") {
fm.status = "ready"; fm.status = "ready";
// kembali ke tabel
setTimeout(() => {
md.selected = null;
md.tab.active = "master";
md.internal.action_should_refresh = true;
md.params.apply();
md.render(); md.render();
}, 500); fm.render();
}` }`
} }
} catch (e) { } catch (e) {

View File

@ -210,6 +210,7 @@ export const genRelMany = (prop: {
} }
`; `;
} }
return result; return result;
}; };
export const getColumn = (data: any) => { export const getColumn = (data: any) => {

View File

@ -306,7 +306,7 @@ export const genTableEdit = async (
}, },
}); });
const child_sub_name = createItem({ const child_sub_name = createItem({
name: "tbl-col", name: "table: columns",
childs: childs, childs: childs,
}); });
if (commit) { if (commit) {

View File

@ -17,7 +17,7 @@ export type FMProps = {
label_width: number; label_width: number;
gen_fields: any; gen_fields: any;
gen_table: string; gen_table: string;
on_load_deps?: any[]; deps?: any;
feature?: any[]; feature?: any[];
sfd_field?: any; sfd_field?: any;
render_parent?: () => void; render_parent?: () => void;
@ -52,12 +52,10 @@ export type FieldProp = {
| string | string
| ((arg: { | ((arg: {
field: FieldLocal; field: FieldLocal;
Link: FC<{ children: any; }>; Link: FC<{ children: any }>;
}) => Promise<string> | string); }) => Promise<string> | string);
url: string; url: string;
params: ( params: (field: FieldLocal) => { where: any } | Promise<{ where: any }>;
field: FieldLocal
) => { md: MDLocal; where: any } | Promise<{ md: MDLocal; where: any }>;
}; };
fm: FMLocal; fm: FMLocal;
type: FieldType | (() => FieldType); type: FieldType | (() => FieldType);
@ -119,6 +117,7 @@ export type FieldProp = {
export type FMInternal = { export type FMInternal = {
status: "init" | "resizing" | "loading" | "saving" | "ready"; status: "init" | "resizing" | "loading" | "saving" | "ready";
data: any; data: any;
deps: any;
reload: () => Promise<void>; reload: () => Promise<void>;
submit: () => Promise<boolean>; submit: () => Promise<boolean>;
events: { events: {

View File

@ -118,9 +118,32 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
</> </>
); );
} }
const form = JSON.parse(JSON.stringify(fm.data));
if (fm.deps.md) {
const md = fm.deps.md;
const last = md.params.links[md.params.links.length - 1];
if (last) {
const pk = Object.values(fm.field_def).find((e) => e.is_pk);
if (pk) {
let obj = last.update;
if (!fm.data[pk.name]) {
obj = last.create;
}
if (typeof obj === "object" && obj) {
for (const [k, v] of Object.entries(obj)) {
form[k] = v;
}
}
}
}
}
const success = await fm.props.on_submit({ const success = await fm.props.on_submit({
fm, fm,
form: fm.data, form,
error: fm.error.object, error: fm.error.object,
}); });

View File

@ -29,6 +29,7 @@ import { getFilter } from "../filter/utils/get-filter";
import { Skeleton } from "../ui/skeleton"; import { Skeleton } from "../ui/skeleton";
import { sortTree } from "./utils/sort-tree"; import { sortTree } from "./utils/sort-tree";
import { set } from "lib/utils/set"; import { set } from "lib/utils/set";
import { MDLocal } from "../md/utils/typings";
type OnRowClick = (arg: { type OnRowClick = (arg: {
row: any; row: any;
@ -70,6 +71,7 @@ type TableListProp = {
gen_table?: string; gen_table?: string;
softdel_type?: string; softdel_type?: string;
cache_row?: boolean; cache_row?: boolean;
md?: MDLocal;
}; };
const w = window as any; const w = window as any;
const selectCellClassname = css` const selectCellClassname = css`
@ -102,6 +104,7 @@ export const TableList: FC<TableListProp> = ({
value, value,
cache_row, cache_row,
__props, __props,
md,
}) => { }) => {
if (mode === "auto") { if (mode === "auto") {
if (w.isMobile) { if (w.isMobile) {
@ -232,6 +235,15 @@ export const TableList: FC<TableListProp> = ({
const orderBy = local.sort.orderBy || undefined; const orderBy = local.sort.orderBy || undefined;
const where = filterWhere(filter_name, __props); const where = filterWhere(filter_name, __props);
if (md) {
const last = md.params.links[md.params.links.length - 1];
if (last && last.where) {
for (const [k, v] of Object.entries(last.where)) {
where[k] = v;
}
}
}
call_prasi_events("tablelist", "where", [__props?.gen__table, where]); call_prasi_events("tablelist", "where", [__props?.gen__table, where]);
const load_args: any = { const load_args: any = {
@ -290,14 +302,14 @@ export const TableList: FC<TableListProp> = ({
); );
let childs: any[] = []; let childs: any[] = [];
let sub_name = "fields"; let sub_name = ["fields"];
switch (mode) { switch (mode) {
case "table": case "table":
sub_name = "tbl-col"; sub_name = ["tbl-col", "table: columns"];
break; break;
case "list": case "list":
sub_name = "list-row"; sub_name = ["list-row", "list: rows"];
break; break;
} }
@ -344,7 +356,7 @@ export const TableList: FC<TableListProp> = ({
} }
}; };
const mode_child = raw_childs.find( const mode_child = raw_childs.find(
(e: any) => e.name === sub_name || e.name === mode (e: any) => sub_name.includes(e.name) || e.name === mode
); );
if (mode_child) { if (mode_child) {
const tbl = _item.edit.childs[0].edit.childs.find( const tbl = _item.edit.childs[0].edit.childs.find(

View File

@ -93,7 +93,8 @@ export const MasterDetail: FC<MDProps> = (arg) => {
if (pk) { if (pk) {
const value = md.params.hash[md.name]; const value = md.params.hash[md.name];
if (value) { if (value) {
if (!md.selected) { if (md.selected && md.selected[pk.name] === value) {
} else {
md.selected = { [pk.name]: value }; md.selected = { [pk.name]: value };
} }
const tab = md.params.tabs[md.name]; const tab = md.params.tabs[md.name];

View File

@ -25,6 +25,10 @@ export const generateMDForm = async (
mode: "raw", mode: "raw",
value: `${JSON.stringify(arg.fields)}`, value: `${JSON.stringify(arg.fields)}`,
}, },
deps: {
mode: "raw",
value: `({ md: typeof md !== "undefined" ? md : undefined })`,
},
on_load: { on_load: {
mode: "string", mode: "string",
value: "", value: "",

View File

@ -1,11 +1,11 @@
export const modeTableList = (mode: string) => { export const modeTableList = (mode: string) => {
let sub_name = "tbl-col"; let sub_name = "table: columns";
switch (mode) { switch (mode) {
case "table": case "table":
sub_name = "tbl-col"; sub_name = "table: columns";
break; break;
case "list": case "list":
sub_name = "list-row"; sub_name = "list: rows";
break; break;
} }
return sub_name; return sub_name;

View File

@ -21,6 +21,7 @@ export const MDRenderMaster: FC<{
md.header.master.breadcrumb = breadcrumb; md.header.master.breadcrumb = breadcrumb;
useEffect(() => { useEffect(() => {
md.header.render();
if (md) { if (md) {
let width = 0; let width = 0;
let min_width = 0; let min_width = 0;

View File

@ -79,8 +79,8 @@ const get_layer = async (
default: v.default, default: v.default,
}), }),
label: k + (!v.optional && !v.default ? " *" : ""), label: k + (!v.optional && !v.default ? " *" : ""),
alt: v.type, alt: `${v.is_pk ? "🔑" : ""} ${v.type}`,
checked: v.is_pk && depth === 0, checked: v.is_pk,
}); });
} }
} }