From e58b453f24c8b4081361112862d29c34eb22009d Mon Sep 17 00:00:00 2001 From: Melonai Date: Wed, 20 May 2020 19:52:18 +0200 Subject: port to react and better error handling in backend --- client/src/Components/Response.js | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 client/src/Components/Response.js (limited to 'client/src/Components/Response.js') diff --git a/client/src/Components/Response.js b/client/src/Components/Response.js new file mode 100644 index 0000000..83c6ff1 --- /dev/null +++ b/client/src/Components/Response.js @@ -0,0 +1,50 @@ +import React, {useEffect, useState} from 'react'; +import axios from "axios"; +import Loader from "./Loader"; +import CopyButton from "./CopyButton"; + +function Response(props){ + const CancelToken = axios.CancelToken; + const [requestState, setRequestState] = useState({loading: true, cancel: CancelToken.source()}); + + useEffect(() => { + axios.post('/', {url: "https://" + props.url}, {cancelToken: requestState.cancel.token}) + .then((r) => { + setRequestState({loading: false, hash: r.data.hash, cancel: requestState.cancel}); + }).catch((e) => { + if (!axios.isCancel(e)) { + setRequestState({loading: false, error: true, cancel: requestState.cancel}); + } + }); + + return () => { + requestState.cancel.cancel(); + }; + }, [props.url, requestState.cancel]) + + let text; + if (!requestState.loading) { + if (!requestState.error) { + if (props.url.length < 20) { + text = + The short link for {props.url} is
sho.rest/{requestState.hash}
; + } else { + text = + The short link for your URL is
sho.rest/{requestState.hash}
; + } + } else { + text = There was an error. + } + } else { + text = + } + + return ( +
+
{text}
+ {requestState.error || requestState.loading ? "" : } +
+ ) +} + +export default Response; \ No newline at end of file -- cgit 1.4.1