aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/hooks/useFetchPaste.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/components/hooks/useFetchPaste.js')
-rw-r--r--frontend/src/components/hooks/useFetchPaste.js66
1 files changed, 64 insertions, 2 deletions
diff --git a/frontend/src/components/hooks/useFetchPaste.js b/frontend/src/components/hooks/useFetchPaste.js
index a61ca1e..c394f5b 100644
--- a/frontend/src/components/hooks/useFetchPaste.js
+++ b/frontend/src/components/hooks/useFetchPaste.js
@@ -1,5 +1,67 @@
-import React from 'react'
+import {useEffect, useState} from 'react'
+import {fetchPaste, fmtDateStr} from './shared'
+import {LANGS} from "../renderers/Code";
-export default () => {
+export default (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 setStateFromData = (data) => {
+ document.title = data.title
+ setResult({
+ title: data.title,
+ content: data.content,
+ language: data.language,
+ expiry: fmtDateStr(data.expiry)
+ })
+ }
+
+ // initial fetch
+ useEffect(() => {
+ fetchPaste(id)
+ .then(resp => setStateFromData(resp.data))
+ .catch(handleErr)
+ .finally(() => setLoading(false))
+ }, [id])
+
+ return { loading, err, requiresAuth, validPass, getWithPassword, result }
} \ No newline at end of file