From dc793a50621a31f1037a3faf0c34c5cc2b8a38ad Mon Sep 17 00:00:00 2001 From: rizky Date: Tue, 30 Apr 2024 04:23:03 -0700 Subject: [PATCH] fix master detail --- comps/md/MasterDetail.tsx | 21 +++- gen/gen_form/new_field.ts | 16 ++- gen/master_detail/gen-form.ts | 2 + gen/master_detail/gen.ts | 15 ++- gen/prop/gen_prop_fields.ts | 225 +++++++++++++++++++++------------- gen/prop/gen_prop_table.ts | 8 +- 6 files changed, 196 insertions(+), 91 deletions(-) diff --git a/comps/md/MasterDetail.tsx b/comps/md/MasterDetail.tsx index 012b5c2..cce6468 100755 --- a/comps/md/MasterDetail.tsx +++ b/comps/md/MasterDetail.tsx @@ -11,6 +11,10 @@ import { import { masterDetailInit, masterDetailSelected } from "./utils/md-init"; import { MDLocal, MDLocalInternal, MDRef } from "./utils/typings"; +const w = window as unknown as { + generating_prasi_md: Record; +}; + export const MasterDetail: FC<{ child: any; PassProp: any; @@ -34,6 +38,19 @@ export const MasterDetail: FC<{ gen_table, on_init, }) => { + let isGenerate = false as Boolean; + try{ + console.log("MASUK"); + isGenerate = false; + if (w.generating_prasi_md["master_detail"]){ + isGenerate = true; + } + }catch(ex){ + + } + console.log(isGenerate); + if (w.generating_prasi_md && w.generating_prasi_md["xxxx"]) + return "generating"; const _ref = useRef({ PassProp, child }); const md = useLocal({ name, @@ -92,7 +109,8 @@ export const MasterDetail: FC<{ masterDetailInit(md, child, editor_tab); masterDetailSelected(md); } - + console.log("MASUK?"); + if(isGenerate) return <>Generate Master Detail... return (
= ({ md, mdr }) => { if (should_show_tab(md)) { return ; } - return ( <> {!md.selected && } diff --git a/gen/gen_form/new_field.ts b/gen/gen_form/new_field.ts index 7adf7e6..0961b6e 100755 --- a/gen/gen_form/new_field.ts +++ b/gen/gen_form/new_field.ts @@ -33,7 +33,7 @@ export const newField = async (arg: GFCol, opt: { parent_table: string }) => { const childs = []; let type = "text"; - if (["int", "string"].includes(arg.type)) { + if (["int", "string", "text"].includes(arg.type)) { childs.push( createItem({ component: { @@ -87,6 +87,20 @@ export const newField = async (arg: GFCol, opt: { parent_table: string }) => { parent_table: opt.parent_table, }); childs.push(item); + } else { + // type not found, + console.warn("Type not found", type); + type = "text"; + childs.push( + createItem({ + component: { + id: "ca7ac237-8f22-4492-bb9d-4b715b1f5c25", + props: { + type: "text", + }, + }, + }) + ); } const item = createItem({ diff --git a/gen/master_detail/gen-form.ts b/gen/master_detail/gen-form.ts index 0d5413e..739e571 100755 --- a/gen/master_detail/gen-form.ts +++ b/gen/master_detail/gen-form.ts @@ -94,6 +94,8 @@ type Init = { submit: () => Promise; reload: () => void } props: { breadcrumb: res.breadcrumb, actions: res.actions, + name: "detail", + label: "Detail", }, }, }); diff --git a/gen/master_detail/gen.ts b/gen/master_detail/gen.ts index 5cd6d87..127a549 100755 --- a/gen/master_detail/gen.ts +++ b/gen/master_detail/gen.ts @@ -3,19 +3,28 @@ import { genForm } from "./gen-form"; import { genList } from "./gen-list"; import { GenMasterDetailArg } from "./utils"; +const w = window as unknown as { + generating_prasi_md: Record; +}; + export const gen_master_detail: GenFn = async ( modify, data, arg ) => { + console.log("LAGI GENERATE"); + w.generating_prasi_md = { + "master_detail": true + }; + const result: any = {}; const fields = parseGenField(arg.gen_fields); + modify(result); const should_gen = parseOpt<{ list: { number: boolean; actions: boolean }; form: boolean; view: boolean; }>(arg.gen_feature); - const result: any = {}; if (should_gen.list) { await genList(arg, data); } @@ -24,5 +33,9 @@ export const gen_master_detail: GenFn = async ( } result.child = data.child; + delete w.generating_prasi_md["master_detail"]; modify(result); + + console.log("LAGI GENERATE"); + }; diff --git a/gen/prop/gen_prop_fields.ts b/gen/prop/gen_prop_fields.ts index 3ef4855..245268b 100755 --- a/gen/prop/gen_prop_fields.ts +++ b/gen/prop/gen_prop_fields.ts @@ -1,3 +1,5 @@ +import get from "lodash.get"; + const single = {} as Record< string, { @@ -37,98 +39,149 @@ const load_single = async (table: string) => { } return single[table]; }; - export const gen_prop_fields = async (gen_table: string) => { - const result: { - label: string; - value: string; - options?: any[]; - checked?: boolean; - }[] = []; - const { cols, rels } = await load_single(gen_table); - if (cols) { - for (const [k, v] of Object.entries(cols) as any) { - result.push({ - value: JSON.stringify({ - name: k, - is_pk: v.is_pk, - type: v.db_type || v.type, - optional: v.optional, - }), - label: k, - checked: v.is_pk, - }); - } + const path = window.location.pathname; + let id_site = null; + try { + id_site = path.split("/")[2]; + } catch (e) { + id_site = window.location.hostname; } - if (rels) { - for (const [k, v] of Object.entries(rels)) { - let options = []; - const to = v.to; - const from = v.from; - const parent_name = k; - const parent_rel = v; - const { cols, rels } = await load_single(v.to.table); - if (cols) { - for (const [k, v] of Object.entries(cols)) { - options.push({ - value: JSON.stringify({ - name: k, - is_pk: v.is_pk, - type: v.db_type || v.type, - optional: v.optional, - }), - label: k, - checked: v.is_pk, - }); - } + const schema = getSchemaOnStorage(id_site, gen_table); + console.log({schema}) + if (!schema) { + const result: { + label: string; + value: string; + options?: any[]; + checked?: boolean; + }[] = []; + const { cols, rels } = await load_single(gen_table); + if (cols) { + for (const [k, v] of Object.entries(cols) as any) { + result.push({ + value: JSON.stringify({ + name: k, + is_pk: v.is_pk, + type: v.db_type || v.type, + optional: v.optional, + }), + label: k, + checked: v.is_pk, + }); } - if (rels) { - for (const [k, v] of Object.entries(rels)) { - let sub_opt = []; - const to = v.to; - const from = v.from; - const { cols } = await load_single(v.to.table); - for (const [k, v] of Object.entries(cols)) { - sub_opt.push({ - value: JSON.stringify({ - name: k, - is_pk: v.is_pk, - type: v.db_type || v.type, - optional: v.optional, - }), - label: k, - checked: v.is_pk, - }); + } + if (rels) { + for (const [k, v] of Object.entries(rels)) { + let options = []; + const to = v.to; + const from = v.from; + const parent_name = k; + const parent_rel = v; + if (to) { + const { cols, rels } = await load_single(to.table); + if (cols) { + for (const [k, v] of Object.entries(cols)) { + options.push({ + value: JSON.stringify({ + name: k, + is_pk: v.is_pk, + type: v.db_type || v.type, + optional: v.optional, + }), + label: k, + checked: v.is_pk, + }); + } + } + if (rels) { + for (const [k, v] of Object.entries(rels)) { + let sub_opt = []; + const to = v.to; + const from = v.from; + const { cols } = await load_single(v.to.table); + for (const [k, v] of Object.entries(cols)) { + sub_opt.push({ + value: JSON.stringify({ + name: k, + is_pk: v.is_pk, + type: v.db_type || v.type, + optional: v.optional, + }), + label: k, + checked: v.is_pk, + }); + } + options.push({ + value: JSON.stringify({ + name: k, + is_pk: false, + type: v.type, + optional: true, + relation: { from, to }, + }), + label: k, + options: sub_opt, + checked: + parent_rel.type === "has-many" && + parent_rel.from.table === v.to.table, + }); + } } - options.push({ - value: JSON.stringify({ - name: k, - is_pk: false, - type: v.type, - optional: true, - relation: { from, to }, - }), - label: k, - options: sub_opt, - checked: - parent_rel.type === "has-many" && - parent_rel.from.table === v.to.table, - }); } + result.push({ + value: JSON.stringify({ + name: k, + is_pk: false, + type: v.type, + optional: true, + relation: { from, to }, + }), + label: k, + options, + }); } - result.push({ - value: JSON.stringify({ - name: k, - is_pk: false, - type: v.type, - optional: true, - relation: { from, to }, - }), - label: k, - options, - }); } + try { + saveSchemaOnStorage(result, id_site, gen_table); + } catch (e: any) { + console.error(e.message); + } + return result; + } else { + return schema; } - - return result; }; + +const saveSchemaOnStorage = (res: any, id_site: string, table: string) => { + let schemaSite = null; + let schema_master_detail: Record = {} + const keys = `schema-md-${id_site}` + try { + let smd = localStorage.getItem(keys) as string; + schemaSite = JSON.parse(smd); + } catch (error) { + } + try { + schema_master_detail = { + ...schemaSite, + [table]: JSON.stringify(res), + }; + console.log({schema_master_detail}) + localStorage.setItem(keys, JSON.stringify(schema_master_detail)); + } catch (e: any) { + console.error(e.message); + } +} +const getSchemaOnStorage = (id_site: string, table: string) => { + const keys = `schema-md-${id_site}` + let schemaSite = null; + try { + let smd = localStorage.getItem(keys) as string; + schemaSite = JSON.parse(smd); + } catch (error) { + } + const schema = get(schemaSite, `${table}`); + if(schema) return JSON.parse(schema); + return null +} \ No newline at end of file diff --git a/gen/prop/gen_prop_table.ts b/gen/prop/gen_prop_table.ts index b803bc0..9c2fc1b 100755 --- a/gen/prop/gen_prop_table.ts +++ b/gen/prop/gen_prop_table.ts @@ -3,10 +3,16 @@ const cache: any = []; export const gen_props_table = async () => { if (cache.length > 0) return cache; + const tables = await db._schema.tables(); + if (!Array.isArray(tables)) { + alert("WARNING: failed to get tables from app server"); + return []; + } + const result = [{ value: "", label: "" }]; const final = [ ...result, - ...(await db._schema.tables()).map((e) => ({ + ...tables.map((e) => ({ value: e, label: e, reload: ["gen_fields", "gen_label"],