This commit is contained in:
rizky 2024-07-20 22:44:07 -07:00
parent 01d213326e
commit 96a07a79cb
8 changed files with 101 additions and 38 deletions

View File

@ -48,8 +48,7 @@ export const FilterContent: FC<{
margin-top: 0 !important; margin-top: 0 !important;
} }
.field-outer { .field-outer {
margin-top: 3px; margin: 3px;
margin-bottom: 3px;
} }
.search-all { .search-all {

View File

@ -223,11 +223,13 @@ export const Form: FC<FMProps> = (props) => {
` `
)} )}
> >
{fm.status !== "init" && fm.size.width > 0 && ( <>
<PassProp fm={fm} submit={fm.submit}> {fm.status !== "init" && fm.size.width > 0 && (
{body} <PassProp fm={fm} submit={fm.submit}>
</PassProp> {!fm.data ? <>NO DATA</> : body}
)} </PassProp>
)}
</>
<button type="submit" className="c-hidden"></button> <button type="submit" className="c-hidden"></button>
</div> </div>
</form> </form>

View File

@ -4,7 +4,7 @@ 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";
import { useLocal } from "lib/utils/use-local"; import { useLocal } from "lib/utils/use-local";
import { ArrowUpRight } from "lucide-react"; import { ArrowUpRight, Construction, Cuboid, Loader } 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";
@ -30,14 +30,47 @@ export const FieldLink: FC<{
const link_local = useLocal({ const link_local = useLocal({
navigating: false, navigating: false,
}); });
const pk = Object.values(fm.field_def).find((e) => e.is_pk);
if (!pk) {
return <>No Primary Key</>;
}
if (!fm.data[pk.name]) {
return (
<div
className={cx(
css`
color: #999;
`,
"c-flex c-space-x-2 items-center"
)}
>
<div
className={cx(
css`
border: 1px solid #ececeb;
`,
"c-rounded c-flex c-space-x-1 items-center c-px-2"
)}
>
<Construction size={12} /> <div>Unsaved</div>
</div>
<div>No Item</div>
</div>
);
}
return ( return (
<div <div
className={cx( className={cx(
"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 #bbb;
&:hover { &:hover {
background-color: #dcedfc; border: 1px solid rgb(126, 167, 203);
background: rgb(220, 237, 252);
color: #001080;
} }
` `
)} )}

View File

@ -57,14 +57,18 @@ export const generateForm = async (
pks, pks,
opt: is_md opt: is_md
? { ? {
after_load: ` after_load: `\
if (typeof md === "object") { if (typeof md === "object") {
opt.fm.status = "ready"; opt.fm.status = "ready";
md.selected = opt.fm.data; md.selected = opt.fm.data;
if (!md.selected) {
md.tab.active = "master";
alert("Data Not Found");
md.params.apply();
}
md.header.render(); md.header.render();
md.render(); md.render();
} }`,
`,
is_md: true, is_md: true,
} }
: { is_md }, : { is_md },
@ -204,12 +208,13 @@ ${
${ ${
is_md && is_md &&
`\ `if (typeof md !== "undefined") {
if (typeof md !== "undefined") { fm.status = "ready";
fm.status = "ready"; fm.data = form;
md.render(); md.selected = form;
fm.render(); md.render();
}` fm.render();
}`
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);

View File

@ -70,11 +70,16 @@ ${
${pk}: id, ${pk}: id,
}; };
const gen = generateSelect(fields); try {
item = await table?.findFirst({ const gen = generateSelect(fields);
where, item = await table?.findFirst({
select: gen.select, where,
}); select: gen.select,
});
} catch (e) {
item = null;
console.error(e);
}
setTimeout(() => { setTimeout(() => {
call_prasi_events("form", "after_load", [opt?.fm]); call_prasi_events("form", "after_load", [opt?.fm]);

View File

@ -64,13 +64,10 @@ export const formInit = (fm: FMLocal, props: FMProps) => {
} else { } else {
result = on_load_result; result = on_load_result;
} }
if (!!result) {
fm.data = result;
}
if (!fm.data) { fm.data = result;
fm.data = {};
} if (result === undefined) fm.data = {};
if (isEditor) { if (isEditor) {
const item_id = props.item.id; const item_id = props.item.id;

View File

@ -92,16 +92,31 @@ export const MasterDetail: FC<MDProps> = (arg) => {
md.params.parse(); md.params.parse();
if (pk) { if (pk) {
const value = md.params.hash[md.name]; const value = md.params.hash[md.name];
if (!value && md.selected && Object.keys(md.selected).length === 0) {
md.params.hash[md.name] = "+";
md.params.apply();
}
if (value) { if (value) {
if (md.selected && md.selected[pk.name] === value) { if (value === "+") {
} else {
md.selected = { [pk.name]: value };
}
const tab = md.params.tabs[md.name];
if (tab && md.tab.list.includes(tab)) {
md.tab.active = tab;
} else {
md.tab.active = "detail"; md.tab.active = "detail";
if (!md.selected) {
md.selected = {};
} else if (md.selected && md.selected[pk.name]) {
md.params.hash[md.name] = md.selected[pk.name];
md.params.apply();
}
} else {
if (md.selected && md.selected[pk.name] === value) {
} else {
md.selected = { [pk.name]: value };
}
const tab = md.params.tabs[md.name];
if (tab && md.tab.list.includes(tab)) {
md.tab.active = tab;
} else {
md.tab.active = "detail";
}
} }
} }
} }

View File

@ -2,6 +2,7 @@ import { FieldLocal } from "lib/comps/form/typings";
import { FMLocal } from "../.."; import { FMLocal } from "../..";
import { Prisma } from "../../typings/prisma"; import { Prisma } from "../../typings/prisma";
import { set } from "./set"; import { set } from "./set";
import { MDLocal } from "lib/comps/md/utils/typings";
const events = { const events = {
form: { form: {
@ -10,6 +11,12 @@ const events = {
after_save: async (fm: FMLocal, record: any) => {}, after_save: async (fm: FMLocal, record: any) => {},
before_load: async (fm: FMLocal) => {}, before_load: async (fm: FMLocal) => {},
after_load: async (fm: FMLocal) => {}, after_load: async (fm: FMLocal) => {},
before_delete: async (md: MDLocal, fm: FMLocal) => {
return {
preventDelete: false as boolean,
navigateBack: true as boolean,
};
},
}, },
field: { field: {
relation_load: async (fm: FMLocal, field: FieldLocal) => { relation_load: async (fm: FMLocal, field: FieldLocal) => {