fix layout

This commit is contained in:
Rizky 2024-06-24 00:51:57 -07:00
parent 952bfb6327
commit 8d8f5291e4
4 changed files with 52 additions and 35 deletions

View File

@ -68,49 +68,58 @@ async ({ form, error, fm }: IForm) => {
md.render(); md.render();
} }
const data = { ...form }; // data form const data = { ...form };
const data_rel = ${JSON.stringify(rel_many)} // list relasi has many const record = {} as Record<string, any>;
const data_master = {} as Record<string, any> | any; // variabel untuk data master
const data_array = [] as Array<{ const relation_ref = ${JSON.stringify(rel_many)};
const has_many = [] as Array<{
table: string; table: string;
data: Array<any>; data: Array<any>;
fk: string; fk: string;
}>; // variabel untuk data array atau has many }>;
// proses untuk membagi antara data master dengan data array // pisahkan antara has_many dengan field biasa
// data array / has many dilihat dari value yang berupa array
for (const [k, v] of Object.entries(data) as any) { for (const [k, v] of Object.entries(data) as any) {
if (Array.isArray(v)) { if (Array.isArray(v)) {
const rel = Array.isArray(data_rel) && data_rel.length ? data_rel.find((e) => e.table === k) : null const rel =
Array.isArray(relation_ref) && relation_ref.length
? relation_ref.find((e) => e.table === k)
: null;
if (rel) { if (rel) {
data_array.push({ has_many.push({
table: k, table: k,
data: v, data: v,
fk: rel.fk, fk: rel.fk,
}); });
} }
} else { } else {
data_master[k] = v; record[k] = v;
} }
} }
// hapus id dari data_master jika ada
try { // prisma create / update ga boleh ada record.${pk}
delete data_master.${pk}; if (record) delete record.${pk};
} catch (ex) {}
if (form.${pk}) { if (form.${pk}) {
await db.${table}.update({ await db.${table}.update({
where: { where: {
${pk}: form.${pk}, ${pk}: form.${pk},
}, },
data: data_master, data: {
...record,
},
}); });
} else { } else {
const res = await db.${table}.create({ const res = await db.${table}.create({
data: data_master, //@ts-ignore
data: {
...record,
},
}); });
if (res) form.${pk} = res.${pk}; if (res) form.id = res.id;
} }
if (data_array.length) {
if (has_many.length) {
const exec_query_bulk = async ( const exec_query_bulk = async (
current: { table: string; data: Array<any>; fk: string }, current: { table: string; data: Array<any>; fk: string },
list: Array<{ table: string; data: Array<any>; fk: string }>, list: Array<{ table: string; data: Array<any>; fk: string }>,
@ -120,7 +129,7 @@ async ({ form, error, fm }: IForm) => {
const data = current.data.map((e) => { const data = current.data.map((e) => {
return { return {
...e, ...e,
${table}: { m_role: {
connect: { connect: {
${pk}: form.${pk}, ${pk}: form.${pk},
}, },
@ -146,7 +155,7 @@ async ({ form, error, fm }: IForm) => {
} }
} }
}; };
await exec_query_bulk(data_array[0], data_array, 0); await exec_query_bulk(has_many[0], has_many, 0);
} }
result = true; result = true;
} catch (e) { } catch (e) {

View File

@ -16,7 +16,6 @@ export type LGProps = {
export const Login: FC<LGProps> = (props) => { export const Login: FC<LGProps> = (props) => {
const local = useLocal({ loading: true }, () => { const local = useLocal({ loading: true }, () => {
loadSession(); loadSession();
setTimeout(() => {
if (w.user) { if (w.user) {
const home = props.url_home[w.user.role]; const home = props.url_home[w.user.role];
if (home) { if (home) {
@ -26,7 +25,6 @@ export const Login: FC<LGProps> = (props) => {
} }
local.loading = false; local.loading = false;
local.render(); local.render();
}, 500);
}); });
if (local.loading) if (local.loading)

View File

@ -11,8 +11,14 @@ export const logout = (url_login?: string) => {
if (typeof get(w, "user") === "object") { if (typeof get(w, "user") === "object") {
w.user = null; w.user = null;
} }
if (localStorage.getItem("user")) {
localStorage.removeItem("user"); let id_site = "";
if (location.hostname === "prasi.avolut.com") {
const parts = location.pathname.split("/");
id_site = parts[2];
}
if (localStorage.getItem("user" + id_site)) {
localStorage.removeItem("user" + id_site);
} }
if (url_login) navigate(url_login); if (url_login) navigate(url_login);
}; };

View File

@ -13,6 +13,7 @@ const local_default = {
nav_timeout: null as any, nav_timeout: null as any,
pathname: "", pathname: "",
loading: false, loading: false,
init: false,
}; };
type MLocal = typeof local_default & { render: () => void }; type MLocal = typeof local_default & { render: () => void };
@ -22,11 +23,15 @@ export const Menu: FC<MenuProp> = (props) => {
role = props.on_init(); role = props.on_init();
let menu = get(imenu, role) || []; let menu = get(imenu, role) || [];
const local = useLocal(local_default); const local = useLocal({ ...local_default });
if (local.pathname !== getPathname()) { if (local.pathname !== getPathname()) {
local.pathname = getPathname(); local.pathname = getPathname();
} }
if (!local.init) {
local.active = "";
local.init = true;
}
useEffect(() => { useEffect(() => {
local.mode = props.mode; local.mode = props.mode;
@ -142,7 +147,6 @@ export const SideBar: FC<{
local.loading = true; local.loading = true;
if (typeof menu.value === "string") { if (typeof menu.value === "string") {
local.active = menu.hash; local.active = menu.hash;
clearTimeout(local.nav_timeout); clearTimeout(local.nav_timeout);
local.nav_timeout = setTimeout(() => { local.nav_timeout = setTimeout(() => {
if ( if (