fix master detail
This commit is contained in:
parent
48b6b284b3
commit
dc793a5062
|
|
@ -11,6 +11,10 @@ import {
|
||||||
import { masterDetailInit, masterDetailSelected } from "./utils/md-init";
|
import { masterDetailInit, masterDetailSelected } from "./utils/md-init";
|
||||||
import { MDLocal, MDLocalInternal, MDRef } from "./utils/typings";
|
import { MDLocal, MDLocalInternal, MDRef } from "./utils/typings";
|
||||||
|
|
||||||
|
const w = window as unknown as {
|
||||||
|
generating_prasi_md: Record<string, true>;
|
||||||
|
};
|
||||||
|
|
||||||
export const MasterDetail: FC<{
|
export const MasterDetail: FC<{
|
||||||
child: any;
|
child: any;
|
||||||
PassProp: any;
|
PassProp: any;
|
||||||
|
|
@ -34,6 +38,19 @@ export const MasterDetail: FC<{
|
||||||
gen_table,
|
gen_table,
|
||||||
on_init,
|
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 _ref = useRef({ PassProp, child });
|
||||||
const md = useLocal<MDLocalInternal>({
|
const md = useLocal<MDLocalInternal>({
|
||||||
name,
|
name,
|
||||||
|
|
@ -92,7 +109,8 @@ export const MasterDetail: FC<{
|
||||||
masterDetailInit(md, child, editor_tab);
|
masterDetailInit(md, child, editor_tab);
|
||||||
masterDetailSelected(md);
|
masterDetailSelected(md);
|
||||||
}
|
}
|
||||||
|
console.log("MASUK?");
|
||||||
|
if(isGenerate) return <>Generate Master Detail...</>
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cx(
|
className={cx(
|
||||||
|
|
@ -112,7 +130,6 @@ const ModeFull: FC<{ md: MDLocal; mdr: MDRef }> = ({ md, mdr }) => {
|
||||||
if (should_show_tab(md)) {
|
if (should_show_tab(md)) {
|
||||||
return <MDTab md={md} mdr={mdr} />;
|
return <MDTab md={md} mdr={mdr} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{!md.selected && <Master md={md} mdr={mdr} />}
|
{!md.selected && <Master md={md} mdr={mdr} />}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ export const newField = async (arg: GFCol, opt: { parent_table: string }) => {
|
||||||
const childs = [];
|
const childs = [];
|
||||||
|
|
||||||
let type = "text";
|
let type = "text";
|
||||||
if (["int", "string"].includes(arg.type)) {
|
if (["int", "string", "text"].includes(arg.type)) {
|
||||||
childs.push(
|
childs.push(
|
||||||
createItem({
|
createItem({
|
||||||
component: {
|
component: {
|
||||||
|
|
@ -87,6 +87,20 @@ export const newField = async (arg: GFCol, opt: { parent_table: string }) => {
|
||||||
parent_table: opt.parent_table,
|
parent_table: opt.parent_table,
|
||||||
});
|
});
|
||||||
childs.push(item);
|
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({
|
const item = createItem({
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,8 @@ type Init = { submit: () => Promise<boolean>; reload: () => void }
|
||||||
props: {
|
props: {
|
||||||
breadcrumb: res.breadcrumb,
|
breadcrumb: res.breadcrumb,
|
||||||
actions: res.actions,
|
actions: res.actions,
|
||||||
|
name: "detail",
|
||||||
|
label: "Detail",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,28 @@ import { genForm } from "./gen-form";
|
||||||
import { genList } from "./gen-list";
|
import { genList } from "./gen-list";
|
||||||
import { GenMasterDetailArg } from "./utils";
|
import { GenMasterDetailArg } from "./utils";
|
||||||
|
|
||||||
|
const w = window as unknown as {
|
||||||
|
generating_prasi_md: Record<string, true>;
|
||||||
|
};
|
||||||
|
|
||||||
export const gen_master_detail: GenFn<GenMasterDetailArg> = async (
|
export const gen_master_detail: GenFn<GenMasterDetailArg> = async (
|
||||||
modify,
|
modify,
|
||||||
data,
|
data,
|
||||||
arg
|
arg
|
||||||
) => {
|
) => {
|
||||||
|
console.log("LAGI GENERATE");
|
||||||
|
w.generating_prasi_md = {
|
||||||
|
"master_detail": true
|
||||||
|
};
|
||||||
|
const result: any = {};
|
||||||
const fields = parseGenField(arg.gen_fields);
|
const fields = parseGenField(arg.gen_fields);
|
||||||
|
modify(result);
|
||||||
const should_gen = parseOpt<{
|
const should_gen = parseOpt<{
|
||||||
list: { number: boolean; actions: boolean };
|
list: { number: boolean; actions: boolean };
|
||||||
form: boolean;
|
form: boolean;
|
||||||
view: boolean;
|
view: boolean;
|
||||||
}>(arg.gen_feature);
|
}>(arg.gen_feature);
|
||||||
|
|
||||||
const result: any = {};
|
|
||||||
if (should_gen.list) {
|
if (should_gen.list) {
|
||||||
await genList(arg, data);
|
await genList(arg, data);
|
||||||
}
|
}
|
||||||
|
|
@ -24,5 +33,9 @@ export const gen_master_detail: GenFn<GenMasterDetailArg> = async (
|
||||||
}
|
}
|
||||||
result.child = data.child;
|
result.child = data.child;
|
||||||
|
|
||||||
|
delete w.generating_prasi_md["master_detail"];
|
||||||
modify(result);
|
modify(result);
|
||||||
|
|
||||||
|
console.log("LAGI GENERATE");
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import get from "lodash.get";
|
||||||
|
|
||||||
const single = {} as Record<
|
const single = {} as Record<
|
||||||
string,
|
string,
|
||||||
{
|
{
|
||||||
|
|
@ -37,98 +39,149 @@ const load_single = async (table: string) => {
|
||||||
}
|
}
|
||||||
return single[table];
|
return single[table];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const gen_prop_fields = async (gen_table: string) => {
|
export const gen_prop_fields = async (gen_table: string) => {
|
||||||
const result: {
|
const path = window.location.pathname;
|
||||||
label: string;
|
let id_site = null;
|
||||||
value: string;
|
try {
|
||||||
options?: any[];
|
id_site = path.split("/")[2];
|
||||||
checked?: boolean;
|
} catch (e) {
|
||||||
}[] = [];
|
id_site = window.location.hostname;
|
||||||
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) {
|
const schema = getSchemaOnStorage(id_site, gen_table);
|
||||||
for (const [k, v] of Object.entries(rels)) {
|
console.log({schema})
|
||||||
let options = [];
|
if (!schema) {
|
||||||
const to = v.to;
|
const result: {
|
||||||
const from = v.from;
|
label: string;
|
||||||
const parent_name = k;
|
value: string;
|
||||||
const parent_rel = v;
|
options?: any[];
|
||||||
const { cols, rels } = await load_single(v.to.table);
|
checked?: boolean;
|
||||||
if (cols) {
|
}[] = [];
|
||||||
for (const [k, v] of Object.entries(cols)) {
|
const { cols, rels } = await load_single(gen_table);
|
||||||
options.push({
|
if (cols) {
|
||||||
value: JSON.stringify({
|
for (const [k, v] of Object.entries(cols) as any) {
|
||||||
name: k,
|
result.push({
|
||||||
is_pk: v.is_pk,
|
value: JSON.stringify({
|
||||||
type: v.db_type || v.type,
|
name: k,
|
||||||
optional: v.optional,
|
is_pk: v.is_pk,
|
||||||
}),
|
type: v.db_type || v.type,
|
||||||
label: k,
|
optional: v.optional,
|
||||||
checked: v.is_pk,
|
}),
|
||||||
});
|
label: k,
|
||||||
}
|
checked: v.is_pk,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (rels) {
|
}
|
||||||
for (const [k, v] of Object.entries(rels)) {
|
if (rels) {
|
||||||
let sub_opt = [];
|
for (const [k, v] of Object.entries(rels)) {
|
||||||
const to = v.to;
|
let options = [];
|
||||||
const from = v.from;
|
const to = v.to;
|
||||||
const { cols } = await load_single(v.to.table);
|
const from = v.from;
|
||||||
for (const [k, v] of Object.entries(cols)) {
|
const parent_name = k;
|
||||||
sub_opt.push({
|
const parent_rel = v;
|
||||||
value: JSON.stringify({
|
if (to) {
|
||||||
name: k,
|
const { cols, rels } = await load_single(to.table);
|
||||||
is_pk: v.is_pk,
|
if (cols) {
|
||||||
type: v.db_type || v.type,
|
for (const [k, v] of Object.entries(cols)) {
|
||||||
optional: v.optional,
|
options.push({
|
||||||
}),
|
value: JSON.stringify({
|
||||||
label: k,
|
name: k,
|
||||||
checked: v.is_pk,
|
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<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
|
||||||
|
}
|
||||||
|
|
@ -3,10 +3,16 @@ const cache: any = [];
|
||||||
export const gen_props_table = async () => {
|
export const gen_props_table = async () => {
|
||||||
if (cache.length > 0) return cache;
|
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 result = [{ value: "", label: "" }];
|
||||||
const final = [
|
const final = [
|
||||||
...result,
|
...result,
|
||||||
...(await db._schema.tables()).map((e) => ({
|
...tables.map((e) => ({
|
||||||
value: e,
|
value: e,
|
||||||
label: e,
|
label: e,
|
||||||
reload: ["gen_fields", "gen_label"],
|
reload: ["gen_fields", "gen_label"],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue