From 61173c119a5754bd062d45e716e467faefa1feba Mon Sep 17 00:00:00 2001 From: Rizky Date: Sat, 29 Jun 2024 20:24:46 -0700 Subject: [PATCH] fix --- comps/list/TableList.tsx | 14 ++++++++++---- utils/format-value.tsx | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/comps/list/TableList.tsx b/comps/list/TableList.tsx index 5d6b0cc..f7d1b4d 100755 --- a/comps/list/TableList.tsx +++ b/comps/list/TableList.tsx @@ -460,7 +460,6 @@ export const TableList: FC = ({ }); } } - if (mode === "list") { if (columns.length > 1) columns = columns.slice(0, 0 + 1); } @@ -519,7 +518,14 @@ export const TableList: FC = ({ }; if (id_parent) load_args.paging = {}; if (typeof on_load === "function") { - local.data = on_load({ ...load_args, mode: "query" }) as any; + let res = on_load({ ...load_args, mode: "query" }) as any; + if(typeof res === "object" && res instanceof Promise){ + res.then((e) => { + local.data = e + }) + }else{ + local.data = e + } } } local.status = "ready"; @@ -709,7 +715,7 @@ export const TableList: FC = ({ className="w-full h-full overflow-y-auto" onScroll={local.paging.scroll} > - {data.map((e, idx) => { + {Array.isArray(data) ? data.map((e, idx) => { return (
= ({
); - })} + }) : <>No Data} )} diff --git a/utils/format-value.tsx b/utils/format-value.tsx index c33bfd8..d7a876c 100755 --- a/utils/format-value.tsx +++ b/utils/format-value.tsx @@ -12,7 +12,7 @@ export const FormatValue: FC<{ name: string; gen_fields: string[]; tree_depth?: number; - mode?: "money" | "datetime"; + mode?: "money" | "datetime" | "timeago"; }> = (prop) => { const { value, gen_fields, name, tree_depth, mode } = prop; if (gen_fields) { @@ -54,6 +54,13 @@ export const FormatValue: FC<{ } catch (ex: any) { return "-"; } + } else if (mode === "timeago") { + if (!value || isEmptyString(value)) return "-"; + try { + return timeAgo(dayjs(value)); + } catch (ex: any) { + return "-"; + } } // await db._batch.update({ // table: "goal", @@ -134,3 +141,30 @@ export const FormatValue: FC<{ ); }; +const timeAgo = (date: any) => { + try { + const now: any = new Date(); + const secondsPast = Math.floor((now - date) / 1000); + + if (secondsPast < 60) { + return `${secondsPast} seconds ago`; + } else if (secondsPast < 3600) { + const minutesPast = Math.floor(secondsPast / 60); + return `${minutesPast} minutes ago`; + } else if (secondsPast < 86400) { + const hoursPast = Math.floor(secondsPast / 3600); + return `${hoursPast} hours ago`; + } else if (secondsPast < 604800) { + // 7 hari + const daysPast = Math.floor(secondsPast / 86400); + return `${daysPast} days ago`; + } else { + const year = date.getFullYear(); + const month = (date.getMonth() + 1).toString().padStart(2, "0"); + const day = date.getDate().toString().padStart(2, "0"); + return `${day}-${month}-${year}`; + } + } catch (e) { + return null; + } +};