From 32330d08e6dc9b7888b032d7c6b0dfde303e497f Mon Sep 17 00:00:00 2001 From: rizky Date: Thu, 15 Aug 2024 08:16:57 -0700 Subject: [PATCH] wip session --- comps/form/field/type/TypeUploadMulti.tsx | 48 ++++++++------ lang/en.ts | 7 +- lang/id.ts | 4 ++ modules.json | 1 + session/client.ts | 81 +++++++++++++++++++++++ session/server.ts | 27 ++++++++ 6 files changed, 145 insertions(+), 23 deletions(-) create mode 100755 session/client.ts create mode 100755 session/server.ts diff --git a/comps/form/field/type/TypeUploadMulti.tsx b/comps/form/field/type/TypeUploadMulti.tsx index cd08eef..5a1235c 100755 --- a/comps/form/field/type/TypeUploadMulti.tsx +++ b/comps/form/field/type/TypeUploadMulti.tsx @@ -7,6 +7,7 @@ import { ChangeEvent, FC } from "react"; import { FMLocal, FieldLocal, FieldProp } from "../../typings"; import { ThumbPreview } from "./FilePreview"; import { PropTypeInput } from "./TypeInput"; +import { translate } from "lib/lang"; export const FieldUploadMulti: FC<{ field: FieldLocal; @@ -179,30 +180,35 @@ export const FieldUploadMulti: FC<{ url={value || ""} options={
-
{ - e.preventDefault(); - e.stopPropagation(); - if (confirm("Remove this file ?")) { - list.splice(idx, 1); - fm.data[field.name] = JSON.stringify(list); - fm.render(); - } - }} - className={cx( - "c-flex c-flex-row c-items-center c-px-1 c-rounded c-bg-white c-cursor-pointer hover:c-bg-red-100 transition-all btn-del", - css` - width: 25px; - height: 25px; - ` - )} + - -
+
{ + e.preventDefault(); + e.stopPropagation(); + if (confirm(translate("Remove this file ?"))) { + list.splice(idx, 1); + fm.data[field.name] = JSON.stringify(list); + fm.render(); + } + }} + className={cx( + "c-flex c-flex-row c-items-center c-px-1 c-rounded c-bg-white c-cursor-pointer hover:c-bg-red-100 transition-all btn-del", + css` + width: 25px; + height: 25px; + ` + )} + > + +
+ {cover.field && (
- Upload Multiple Files + {translate("Upload Multiple Files")}
diff --git a/lang/en.ts b/lang/en.ts index 3bf2de0..e690229 100755 --- a/lang/en.ts +++ b/lang/en.ts @@ -3,7 +3,10 @@ export const langEn = { Save: "Save", Delete: "Delete", "Record Saved": "Record Saved", - "Search": "Search", + Search: "Search", "Add Another": "Add Another", - "Back To List": "Back To List" + "Mark as": "Mark as ", + "Remove this file ?": "Remove this file ?", + "Upload Multiple Files":"Upload Multiple Files", + "Back To List": "Back To List", }; diff --git a/lang/id.ts b/lang/id.ts index f7a7d9b..b80e683 100755 --- a/lang/id.ts +++ b/lang/id.ts @@ -7,5 +7,9 @@ export const langId: Record = { "Record Saved": "Data Tersimpan", Search: "Cari", "Add Another": "Tambah Baru", + "Mark as": "Tandai sebagai", + "Remove this file ?": "Hapus file ini ?", + "Upload Multiple Files": "Unggah Beberapa File", "Back To List": "Kembali ke List", }; + \ No newline at end of file diff --git a/modules.json b/modules.json index ce5d354..e36c107 100755 --- a/modules.json +++ b/modules.json @@ -20,6 +20,7 @@ "@types/lodash.capitalize": "^4.2.9", "@types/lodash.get": "^4.4.9", "@types/react": "^18.2.65", + "bun-types": "^1.1.24", "@types/react-dom": "^18.3.0", "@types/uuid": "^9.0.8", "@wojtekmaj/react-qr-svg": "^1.0.0", diff --git a/session/client.ts b/session/client.ts new file mode 100755 index 0000000..b928cad --- /dev/null +++ b/session/client.ts @@ -0,0 +1,81 @@ +const w = window as unknown as { + _prasi_session: any; + _prasi: { site_id: string }; +}; + +export type SessionData = Record & { role: string }; + +type SessionResponse = + | { active: false; reason: string } + | { active: true; data: T; token: string }; + +export const sessionClient = async (arg: { + editorSampleData: T; + auth: { + mode: "user-pass"; + login: (arg: { username: string; password: string }) => Promise; + }; + on?: Partial<{ + active: (arg: { token: string; data: T }) => any; + expired: () => any; + logout: () => any; + broadcast: (arg: { data: any }) => any; + }>; +}): Promise> => { + const session: Session = { + active: false, + id_site: w._prasi.site_id, + async login() {}, + async logout() {}, + token: "", + data: {} as T, + }; + if (isEditor) { + session.active = true; + session.data = arg.editorSampleData; + return session; + } + if (w._prasi_session) return w._prasi_session as Session; + + const s = localStorage.getItem(`prss-${session.id_site}`); + if (!s) { + session.active = false; + } else { + let url = siteurl("/_session"); + if ( + location.hostname === "prasi.avolut.com" || + location.host === "localhost:4550" + ) { + const newurl = new URL(location.href); + newurl.pathname = `/_proxy/${url}`; + url = newurl.toString(); + } + + const prss = await fetch(url, { method: "POST", body: s }); + try { + const resp = (await prss.json()) as SessionResponse; + if (resp) { + if (resp.active) { + session.data = resp.data; + session.active = true; + session.token = resp.token; + } else { + console.warn("Session inactive, reason:" + resp.reason); + } + } + } catch (e) { + console.warn("Failed to activate session"); + } + } + + return session; +}; + +export type Session = { + active: boolean; + id_site: string; + login: (arg: { username: string; password: string }) => Promise; + logout: () => Promise; + token: string; + data: T; +}; diff --git a/session/server.ts b/session/server.ts new file mode 100755 index 0000000..d573078 --- /dev/null +++ b/session/server.ts @@ -0,0 +1,27 @@ +/// + +type ServerSession = { + handle: (arg: { + req: Request; + handle: (req: Request) => Promise; + mode: "dev" | "prod"; + url: { + raw: URL; + pathname: string; + }; + }) => Promise; +}; + +export const sessionServer = (arg: { encrypt?: boolean }): ServerSession => { + const s: ServerSession = { + async handle({ req, handle, mode, url }) { + if (url.pathname.startsWith("/_session")) { + return new Response("marjio"); + } + + return await handle(req); + }, + }; + + return s; +};