aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/hooks
diff options
context:
space:
mode:
authorjackyzha0 <[email protected]>2021-04-11 10:27:27 -0700
committerjackyzha0 <[email protected]>2021-04-11 10:27:27 -0700
commit0144bfc9cc6c616a00a8171f3950a75ec948427e (patch)
tree101d6c12471d411e9266cffa8e90176aff2e6fdb /frontend/src/components/hooks
parentbase next bump (diff)
downloadctrl-v-0144bfc9cc6c616a00a8171f3950a75ec948427e.tar.xz
ctrl-v-0144bfc9cc6c616a00a8171f3950a75ec948427e.zip
base next refactor
Diffstat (limited to 'frontend/src/components/hooks')
-rw-r--r--frontend/src/components/hooks/shared.js74
-rw-r--r--frontend/src/components/hooks/useFetchPaste.js67
2 files changed, 0 insertions, 141 deletions
diff --git a/frontend/src/components/hooks/shared.js b/frontend/src/components/hooks/shared.js
deleted file mode 100644
index 00d41e9..0000000
--- a/frontend/src/components/hooks/shared.js
+++ /dev/null
@@ -1,74 +0,0 @@
-import axios from 'axios';
-
-// uncomment for local dev
-// const base = `http://localhost:8080/api`
-const base = `https://api.ctrl-v.app/api`
-export function fetchPaste(hash, pass = "") {
- const serverURL = `${base}/${hash}`
-
- if (pass === "") {
- return axios.get(serverURL)
- } else {
- const bodyFormData = new FormData();
- bodyFormData.set('password', pass);
- return axios({
- method: 'post',
- url: `${base}/${hash}`,
- data: bodyFormData,
- headers: { 'Content-Type': 'multipart/form-data' },
- })
- }
-}
-
-export function newPaste(paste) {
- const {title, content, language, pass, expiry} = paste
- const bodyFormData = new FormData();
- bodyFormData.set('title', title);
- bodyFormData.set('content', content);
- bodyFormData.set('language', language);
- bodyFormData.set('password', pass);
- bodyFormData.set('expiry', parseExpiry(expiry));
-
- return axios({
- method: 'post',
- url: base,
- data: bodyFormData,
- headers: { 'Content-Type': 'multipart/form-data' },
- })
-}
-
-export function parseExpiry(e) {
- var cur = new Date();
- var inSeconds = 0
- switch (e) {
- case '5 years':
- inSeconds = 600 * 6 * 24 * 7 * 4 * 12 * 5
- break;
- case '1 year':
- inSeconds = 600 * 6 * 24 * 7 * 4 * 12
- break;
- case '1 month':
- inSeconds = 600 * 6 * 24 * 7 * 4
- break;
- case '1 day':
- inSeconds = 600 * 6 * 24
- break;
- case '1 hour':
- inSeconds = 600 * 6
- break;
- case '10 min':
- inSeconds = 600
- break;
- case '1 week':
- default:
- inSeconds = 600 * 6 * 24 * 7
- break;
- }
- return new Date(cur.getTime() + inSeconds * 1000).toISOString();
-}
-
-export function fmtDateStr(dateString) {
- const d = new Date(dateString)
- const options = { hour: '2-digit', minute: '2-digit', year: 'numeric', month: 'long', day: 'numeric' }
- return d.toLocaleDateString("en-US", options).toLocaleLowerCase()
-} \ No newline at end of file
diff --git a/frontend/src/components/hooks/useFetchPaste.js b/frontend/src/components/hooks/useFetchPaste.js
deleted file mode 100644
index c394f5b..0000000
--- a/frontend/src/components/hooks/useFetchPaste.js
+++ /dev/null
@@ -1,67 +0,0 @@
-import {useEffect, useState} from 'react'
-import {fetchPaste, fmtDateStr} from './shared'
-import {LANGS} from "../renderers/Code";
-
-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