diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9022e05..82a8bf6 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,6 +6,7 @@ + - { - "keyToString": { - "RunOnceActivity.ShowReadmeOnStart": "true", - "RunOnceActivity.git.unshallow": "true", - "git-widget-placeholder": "main", - "last_opened_file_path": "/home/exhq/codeshit/ether-frontend", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "ts.external.directory.path": "/home/exhq/codeshit/ether-frontend/node_modules/typescript/lib", - "vue.rearranger.settings.migration": "true" + +}]]> @@ -76,6 +77,7 @@ + diff --git a/src/app/helpers.tsx b/src/app/helpers.tsx index f37d913..cd92273 100644 --- a/src/app/helpers.tsx +++ b/src/app/helpers.tsx @@ -2,4 +2,6 @@ export const getCookie = (name:string) => { const cookieString = document.cookie; const cookies = cookieString.split('; ').find(row => row.startsWith(name + '=')); return cookies ? cookies.split('=')[1] : null; -}; \ No newline at end of file +}; + +export const API_URL = process.env.NEXT_PUBLIC_API_URL as string; \ No newline at end of file diff --git a/src/app/images/upload/page.tsx b/src/app/images/upload/page.tsx index 997a379..30ed191 100644 --- a/src/app/images/upload/page.tsx +++ b/src/app/images/upload/page.tsx @@ -2,14 +2,32 @@ import "@radix-ui/themes/styles.css"; import {useState} from "react"; import './page.css' -import {getCookie} from "@/app/helpers"; +import {API_URL, getCookie} from "@/app/helpers"; import {Button, Heading, Theme} from "@radix-ui/themes"; +function useApiKey(): ()=>Promise { + const [apiKeyP, setApiKeyP] = useState>(null); + + const h: {p?: undefined | Promise} = {}; + + return async () => { + if (apiKeyP) return await apiKeyP; + if (h.p) return await h.p; + h.p = fetch(`${API_URL}/api/token`, { + headers: { + 'Authorization': getCookie("access_token")!, + } + }).then(it =>it.json()).then(it=>it.apiKey); + setApiKeyP(h.p); + return await h.p; + }; +} + + export default function Page() { - const API_URL = process.env.NEXT_PUBLIC_API_URL as string; const [file, setFile] = useState(null); - const [apiKey, setApiKey] = useState(''); + const apiKey = useApiKey(); const [message, setMessage] = useState(''); //@ts-expect-error the thing breaks idfk @@ -18,6 +36,7 @@ export default function Page() { setFile(e.target.files[0]); }; + const dropFile = (e: DragEvent) => { e.preventDefault(); const files = e.dataTransfer?.files ?? [] @@ -40,23 +59,14 @@ export default function Page() { const formData = new FormData(); formData.append('file', file); - await fetch(`${API_URL}/api/token`, { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - //@ts-expect-error - headers: { - 'Authorization': getCookie("access_token"), - } - }).then(res => { - res.json().then(data => { - setApiKey(data.apiToken); - }) - }); - console.log(apiKey); + + const actualapikey = await apiKey() + console.log(actualapikey) await fetch(`${API_URL}/api/upload`, { method: 'POST', body: formData, headers: { - 'Authorization': apiKey, + 'Authorization': actualapikey, } }).then(async (res) => { if (res.status === 201) {