diff --git a/.env b/.env new file mode 100644 index 0000000..54e001b --- /dev/null +++ b/.env @@ -0,0 +1 @@ +SERVER_URL=http://localhost:8080 \ No newline at end of file diff --git a/src/api/bin.ts b/src/api/bin.ts index c92593b..849d9b9 100644 --- a/src/api/bin.ts +++ b/src/api/bin.ts @@ -1,13 +1,25 @@ -export const setPaste = async (code: string): Promise => { - return await fetch("http://localhost:8080/bin/paste", { - method: "POST", - headers: { "Content-type": "text/plain" }, - body: code, - }); +import { SERVER_URL } from "../util"; + +export const setPaste = async (code: string) => { + try { + return await fetch(SERVER_URL + "/bin/paste", { + method: "POST", + headers: { "Content-type": "text/plain" }, + body: code, + }); + } catch (err) { + console.error(err); + return null; + } }; export const getPaste = async (pasteId: string) => { - return await fetch(`http://localhost:8080/bin/paste/${pasteId}`); + try { + const response = await fetch(`${SERVER_URL}/bin/paste/${pasteId}`); + return response; + } catch (err) { + return null; + } }; export interface SuccessResponse { diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index 0fe349d..f5ad7d1 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -6,7 +6,6 @@ import Theme from "./EditorTheme"; const Editor: FunctionComponent = props => { return ( // TODO: custom implementation? - // TODO: fix line wrap { - const res: Response = await setPaste(code); - if (res.status !== 200) { + const res: Response | null = await setPaste(code); + + if (res == null) { + alert("Couldn't save paste, something went wrong."); + return; + } + + if (!res?.ok) { alert("Something went wrong!"); console.log(res); + return; } - let data: SuccessResponse = await res.json(); + + let data: SuccessResponse = await res?.json(); navigate(`/${data.endpoint}`); }, [code]); diff --git a/src/pages/Paste.tsx b/src/pages/Paste.tsx index 234c546..c41f797 100644 --- a/src/pages/Paste.tsx +++ b/src/pages/Paste.tsx @@ -15,14 +15,18 @@ function Paste() { return navigate("/"); } - const res: Response = await getPaste(id); + const res: Response | null = await getPaste(id); + if (res == null) { + alert("Couldn't connect to the server, something went wrong."); + navigate("/"); + } - if (res.status == 404) { + if (res?.status == 404) { alert("Paste not found!"); navigate("/"); } - if (res.status == 200) { + if (res?.ok) { setCode(await res.text()); } }; diff --git a/src/pages/Raw.tsx b/src/pages/Raw.tsx index 424ec1d..76b9da7 100644 --- a/src/pages/Raw.tsx +++ b/src/pages/Raw.tsx @@ -13,14 +13,19 @@ const Raw = () => { return navigate("/"); } - const res: Response = await getPaste(id); + const res: Response | null = await getPaste(id); - if (res.status == 404) { + if (res == null) { + alert("Couldn't connect to the server, something went wrong."); + navigate("/"); + } + + if (res?.status == 404) { alert("Paste not found!"); navigate("/"); } - if (res.status == 200) { + if (res?.status == 200) { setCode(await res.text()); } }; diff --git a/src/util/index.ts b/src/util/index.ts new file mode 100644 index 0000000..b5d6e98 --- /dev/null +++ b/src/util/index.ts @@ -0,0 +1 @@ +export const SERVER_URL = import.meta.env.VITE_SERVER_URL \ No newline at end of file