fix master detail

This commit is contained in:
rizky 2024-04-30 04:23:03 -07:00
parent 48b6b284b3
commit dc793a5062
6 changed files with 196 additions and 91 deletions

View File

@ -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<string, true>;
};
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<MDLocalInternal>({
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 (
<div
className={cx(
@ -112,7 +130,6 @@ const ModeFull: FC<{ md: MDLocal; mdr: MDRef }> = ({ md, mdr }) => {
if (should_show_tab(md)) {
return <MDTab md={md} mdr={mdr} />;
}
return (
<>
{!md.selected && <Master md={md} mdr={mdr} />}

View File

@ -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({

View File

@ -94,6 +94,8 @@ type Init = { submit: () => Promise<boolean>; reload: () => void }
props: {
breadcrumb: res.breadcrumb,
actions: res.actions,
name: "detail",
label: "Detail",
},
},
});

View File

@ -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<string, true>;
};
export const gen_master_detail: GenFn<GenMasterDetailArg> = 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<GenMasterDetailArg> = async (
}
result.child = data.child;
delete w.generating_prasi_md["master_detail"];
modify(result);
console.log("LAGI GENERATE");
};

View File

@ -1,3 +1,5 @@
import get from "lodash.get";
const single = {} as Record<
string,
{
@ -37,8 +39,17 @@ const load_single = async (table: string) => {
}
return single[table];
};
export const gen_prop_fields = async (gen_table: string) => {
const path = window.location.pathname;
let id_site = null;
try {
id_site = path.split("/")[2];
} catch (e) {
id_site = window.location.hostname;
}
const schema = getSchemaOnStorage(id_site, gen_table);
console.log({schema})
if (!schema) {
const result: {
label: string;
value: string;
@ -67,7 +78,8 @@ export const gen_prop_fields = async (gen_table: string) => {
const from = v.from;
const parent_name = k;
const parent_rel = v;
const { cols, rels } = await load_single(v.to.table);
if (to) {
const { cols, rels } = await load_single(to.table);
if (cols) {
for (const [k, v] of Object.entries(cols)) {
options.push({
@ -116,6 +128,7 @@ export const gen_prop_fields = async (gen_table: string) => {
});
}
}
}
result.push({
value: JSON.stringify({
name: k,
@ -129,6 +142,46 @@ export const gen_prop_fields = async (gen_table: string) => {
});
}
}
try {
saveSchemaOnStorage(result, id_site, gen_table);
} catch (e: any) {
console.error(e.message);
}
return result;
} else {
return schema;
}
};
const saveSchemaOnStorage = (res: any, id_site: string, table: string) => {
let schemaSite = null;
let schema_master_detail: Record<string, any> = {}
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
}

View File

@ -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"],