diff options
| author | jackyzha0 <[email protected]> | 2021-04-11 11:40:44 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2021-04-11 11:40:44 -0700 |
| commit | 77c061bc0b8aecce7311ce820b3401c95797a589 (patch) | |
| tree | 6ec62987330f74b4dfca95dfaaa8cd5c723d1b58 | |
| parent | safely remove style.css (diff) | |
| download | ctrl-v-77c061bc0b8aecce7311ce820b3401c95797a589.tar.xz ctrl-v-77c061bc0b8aecce7311ce820b3401c95797a589.zip | |
working raw paste fetch
| -rw-r--r-- | frontend/src/http/useFetchPaste.js | 96 | ||||
| -rw-r--r-- | frontend/src/pages/[hash].js | 3 | ||||
| -rw-r--r-- | frontend/src/pages/_app.js | 2 | ||||
| -rw-r--r-- | frontend/src/pages/index.js | 2 | ||||
| -rw-r--r-- | frontend/src/pages/raw/[hash].js | 24 | ||||
| -rw-r--r-- | frontend/src/pages/raw/index.js | 14 |
6 files changed, 64 insertions, 77 deletions
diff --git a/frontend/src/http/useFetchPaste.js b/frontend/src/http/useFetchPaste.js index ba482f9..8d40cbe 100644 --- a/frontend/src/http/useFetchPaste.js +++ b/frontend/src/http/useFetchPaste.js @@ -2,68 +2,42 @@ import {useEffect, useState} from 'react' import {fetchPaste, fmtDateStr} from './shared' import {LANGS} from "../components/renderers/Code"; -const useFetchPaste = (id) => { - const [loading, setLoading] = useState(true) - const [err, setErr] = useState() - const [requiresAuth, setRequiresAuth] = useState(false) - const [validPass, setValidPass] = useState(false) - const [result, setResult] = useState({ - title: 'fetching paste...', - content: '', - language: LANGS.detect, - expiry: '', - }) - - const handleErr = error => { - const resp = error.response - - // network err - if (!resp) { - setErr(error.toString()) - return - } - - // password protected - if (resp.status === 401) { - setRequiresAuth(true) - return - } - - // catch all - const errTxt = `${resp.status}: ${resp.data}` - setErr(errTxt) - } - - // callback to try verifying with password - const getWithPassword = (password, errorCallback) => { - fetchPaste(id, password) - .then(resp => { - setValidPass(true) - setStateFromData(resp.data) - }) - .catch(e => errorCallback(e.response.data)) +const resolvePaste = (id, password = "") => { + const response = { + data: { + title: '', + content: '', + language: LANGS.detect, + expiry: '', + }, + unauthorized: false, + error: '', } - - - const setStateFromData = (data) => { - document.title = data.title - setResult({ - title: data.title, - content: data.content, - language: data.language, - expiry: fmtDateStr(data.expiry) + return fetchPaste(id, password) + .then(resp => { + const data = resp.data + response.data = { + ...data, + expiry: fmtDateStr(data.expiry) + } + return response + }) + .catch(error => { + const resp = error.response + if (!resp) { + response.error = 'network error' + return + } + + if (resp.status === 401) { + response.error = 'unauthorized' + response.unauthorized = true + return + } + + response.error = `${resp.status}: ${resp.data}` + return response }) - } - - // initial fetch - useEffect(() => { - fetchPaste(id) - .then(resp => setStateFromData(resp.data)) - .catch(handleErr) - .finally(() => setLoading(false)) - }, [id]) - - return { loading, err, requiresAuth, validPass, getWithPassword, result } } -export default useFetchPaste
\ No newline at end of file +export default resolvePaste
\ No newline at end of file diff --git a/frontend/src/pages/[hash].js b/frontend/src/pages/[hash].js index 27f808b..5a04e31 100644 --- a/frontend/src/pages/[hash].js +++ b/frontend/src/pages/[hash].js @@ -6,6 +6,8 @@ import PasteInfo from '../components/PasteInfo'; import PasswordModal from '../components/modals/PasswordModal' import RenderDispatch from '../components/renderers/RenderDispatch' import useFetchPaste from "../http/useFetchPaste"; +import {Watermark} from "../components/Watermark"; +import ThemeProvider from "../theme/ThemeProvider"; const ViewPaste = (props) => { const { err, requiresAuth, validPass, getWithPassword, result } = useFetchPaste(props.hash) @@ -58,6 +60,7 @@ const ViewPaste = (props) => { onChange={(e) => setTheme(e.target.value)} err={<Error ref={ErrorLabelRef} />} /> + <Watermark/> </div> ); } diff --git a/frontend/src/pages/_app.js b/frontend/src/pages/_app.js index eb205e3..e013c64 100644 --- a/frontend/src/pages/_app.js +++ b/frontend/src/pages/_app.js @@ -1,7 +1,6 @@ import React from 'react' import ThemeProvider from "../theme/ThemeProvider"; import GlobalStyle from "../theme/GlobalStyle"; -import {Watermark} from "../components/Watermark"; import styled from "styled-components"; const Main = styled.div` @@ -12,7 +11,6 @@ const Main = styled.div` const App = ({ Component, pageProps }) => ( <ThemeProvider> <GlobalStyle /> - <Watermark/> <Main id="appElement"> <Component {...pageProps} /> </Main> diff --git a/frontend/src/pages/index.js b/frontend/src/pages/index.js index 36bcad7..1b550e8 100644 --- a/frontend/src/pages/index.js +++ b/frontend/src/pages/index.js @@ -9,6 +9,7 @@ import Latex from '../components/renderers/Latex' import Markdown from '../components/renderers/Markdown' import {Button, SubmitButton} from "../components/Common/Button"; import {newPaste} from "../http/shared"; +import {Watermark} from "../components/Watermark"; const Container = styled.form` width: 100%; @@ -147,6 +148,7 @@ const NewPaste = () => { </div> <br /> <Error ref={ErrorLabel} /> + <Watermark/> </Container> ); } diff --git a/frontend/src/pages/raw/[hash].js b/frontend/src/pages/raw/[hash].js new file mode 100644 index 0000000..d0a66a7 --- /dev/null +++ b/frontend/src/pages/raw/[hash].js @@ -0,0 +1,24 @@ +import React from 'react'; +import resolvePaste from "../../http/useFetchPaste"; +import {CodeLike} from "../../components/Common/mixins"; +import styled from 'styled-components' + +const RawText = styled.pre` + ${CodeLike} + padding: 0 1em; +` + +export async function getServerSideProps(ctx) { + const data = await resolvePaste(ctx.params.hash) + + // Pass data to the page via props + return { props: { ...data } } +} + +const Raw = ({error, data}) => { + return <RawText> + {data?.content || error} + </RawText> +} + +export default Raw
\ No newline at end of file diff --git a/frontend/src/pages/raw/index.js b/frontend/src/pages/raw/index.js deleted file mode 100644 index 86db3d4..0000000 --- a/frontend/src/pages/raw/index.js +++ /dev/null @@ -1,14 +0,0 @@ -import React from 'react'; -import useFetchPaste from "../../http/useFetchPaste"; -import { useRouter } from 'next/router' - - -export default (req, res) => { - const router = useRouter() - const { hash } = router.query - const { err, result } = useFetchPaste(hash) - res.statusCode = 200 - res.json({ - text: 'Hello World! This is the Next.js starter kit :D', - }) -}
\ No newline at end of file |