From ec4aae01114555adfc3d2b87f2e854b1920de052 Mon Sep 17 00:00:00 2001 From: Rizky Date: Wed, 25 Oct 2023 13:22:56 +0700 Subject: [PATCH] fix ws --- app/web/src/render/ed/panel/popup/site.tsx | 259 +++++++++++++++------ app/web/src/utils/sync/ws-client.ts | 2 +- 2 files changed, 184 insertions(+), 77 deletions(-) diff --git a/app/web/src/render/ed/panel/popup/site.tsx b/app/web/src/render/ed/panel/popup/site.tsx index 77cc6859..ff38bfd4 100644 --- a/app/web/src/render/ed/panel/popup/site.tsx +++ b/app/web/src/render/ed/panel/popup/site.tsx @@ -17,7 +17,9 @@ type GItem = { } & ( | { type: "group"; + site_len: number; users: { id: string; username: string }[]; + renaming?: boolean; } | { type: "site"; domain: string } ); @@ -34,88 +36,105 @@ export const EdPopSite = () => { p.render(); } ); + + const reload = async () => { + local.status = "loading"; + local.render(); + + const res = await p.sync.site.group(); + + const group: NodeModel[] = []; + for (const item of res) { + group.push({ + id: item.id, + parent: "site-root", + text: item.name, + data: { + id: item.id, + type: "group", + name: item.name, + site_len: item.site.length, + users: item.org_user.map((e) => ({ + id: e.user.id, + username: e.user.username, + })), + }, + }); + + for (const site of item.site) { + group.push({ + id: site.id, + parent: item.id, + text: site.name, + droppable: false, + data: { + id: site.id, + type: "site", + name: site.name, + domain: site.domain, + }, + }); + } + + group.push({ + id: `new-${item.id}`, + parent: item.id, + text: "new", + droppable: false, + }); + } + local.group = group; + + local.status = "ready"; + local.render(); + }; useEffect(() => { if (p.ui.popup.site && local.status !== "loading") { - (async () => { - local.status = "loading"; - local.render(); - - const res = await p.sync.site.group(); - - const group: NodeModel[] = []; - for (const item of res) { - group.push({ - id: item.id, - parent: "site-root", - text: item.name, - data: { - id: item.id, - type: "group", - name: item.name, - users: item.org_user.map((e) => ({ - id: e.user.id, - username: e.user.username, - })), - }, - }); - - for (const site of item.site) { - group.push({ - id: site.id, - parent: item.id, - text: site.name, - droppable: false, - data: { - id: site.id, - type: "site", - name: site.name, - domain: site.domain, - }, - }); - } - - group.push({ - id: `new-${item.id}`, - parent: item.id, - text: "new", - droppable: false, - }); - } - local.group = group; - - local.status = "ready"; - local.render(); - })(); + reload(); } }, [p.ui.popup.site]); if (!p.ui.popup.site) return null; return ( - { - if (!open) { - p.ui.popup.site = null; - p.render(); - } - }} - > -
-
- {local.status === "loading" ? ( - - ) : ( - - )} + <> + {local.status === "loading" && } + { + if (!open) { + p.ui.popup.site = null; + p.render(); + } + }} + > +
+
+ {(local.status === "ready" || local.group.length > 0) && ( + + )} +
-
- + + ); }; -const SitePicker = ({ group }: { group: NodeModel[] }) => { +const SitePicker = ({ + group, + reload, + status, +}: { + status: any; + group: NodeModel[]; + reload: () => void; +}) => { const p = useGlobal(EDGlobal, "EDITOR"); + const local = useLocal({}); const TypedTree = Tree; const orglen = group.filter((e) => e.parent === "site-root").length; return ( @@ -137,7 +156,7 @@ const SitePicker = ({ group }: { group: NodeModel[] }) => { }, }, }); - console.log(res); + reload(); } }} > @@ -154,7 +173,28 @@ const SitePicker = ({ group }: { group: NodeModel[] }) => { {}} + onDrop={async (_, { dragSource, dropTarget }) => { + const target = dropTarget?.data; + const from = dragSource?.data; + if (target && from) { + if (target.type === "group") { + await db.site.update({ + where: { + id: from.id, + }, + data: { + org: { + connect: { + id: target.id, + }, + }, + }, + select: { id: true }, + }); + reload(); + } + } + }} initialOpen={true} canDrag={(node) => { if (node && node?.data?.type === "site") return true; @@ -206,22 +246,89 @@ const SitePicker = ({ group }: { group: NodeModel[] }) => { } if (!item) return <>; + if (item.type === "group") { return (
-
{node.text}
-
+ {item.renaming ? ( + { + item.name = e.currentTarget.value; + local.render(); + }} + onBlur={() => { + item.name = node.text; + item.renaming = false; + local.render(); + }} + onKeyDown={async (e) => { + if (e.key === "Enter") { + await db.org.update({ + where: { id: item.id }, + data: { name: item.name }, + }); + reload(); + } + }} + /> + ) : ( + <> +
{node.text}
+
{ + item.renaming = true; + local.render(); + }} + > +
`, + }} + >
+
+ + )} +
Team: {item.users.length} user {item.users.length > 1 ? "s" : ""}
{isDropTarget && ( -
- Drop to move here... +
+ Drop here... +
+ )} + {item.site_len === 0 && ( +
{ + if (confirm("Remove this organization ?")) { + await db.org_user.deleteMany({ + where: { id_org: item.id }, + }); + await db.org.delete({ + where: { + id: item.id, + }, + }); + reload(); + } + }} + > +
`, + }} + >
)}
diff --git a/app/web/src/utils/sync/ws-client.ts b/app/web/src/utils/sync/ws-client.ts index 86938145..0d12e387 100644 --- a/app/web/src/utils/sync/ws-client.ts +++ b/app/web/src/utils/sync/ws-client.ts @@ -16,7 +16,7 @@ import { initIDB } from "./idb"; const packr = new Packr({ structuredClone: true }); /** CONSTANT */ -const WS_DEBUG = true; +const WS_DEBUG = false; const RECONNECT_TIMEOUT = 1000; const conf = {