diff options
| author | jackyzha0 <[email protected]> | 2020-05-12 22:28:19 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2020-05-12 22:28:19 -0700 |
| commit | b8118ba07534cdcd01788c6c508be267da1fa87e (patch) | |
| tree | c8f1feaf1b16d89ec7d592a08f54215baf1d1a38 /frontend/src | |
| parent | working password render (diff) | |
| download | ctrl-v-b8118ba07534cdcd01788c6c508be267da1fa87e.tar.xz ctrl-v-b8118ba07534cdcd01788c6c508be267da1fa87e.zip | |
abstract http funcs into helper
Diffstat (limited to 'frontend/src')
| -rw-r--r-- | frontend/src/components/NewPaste.js | 75 | ||||
| -rw-r--r-- | frontend/src/components/ViewPaste.js | 57 | ||||
| -rw-r--r-- | frontend/src/components/httpHelper.js | 67 |
3 files changed, 108 insertions, 91 deletions
diff --git a/frontend/src/components/NewPaste.js b/frontend/src/components/NewPaste.js index 3bdd41a..dbcc5a7 100644 --- a/frontend/src/components/NewPaste.js +++ b/frontend/src/components/NewPaste.js @@ -1,9 +1,9 @@ import React from 'react'; import { TitleInput, PasteInput } from './Inputs' import OptionsContainer from './Options' -import axios from 'axios'; import { Redirect } from 'react-router-dom' import Error from './Err' +import { PostNewPaste } from './httpHelper' class NewPaste extends React.Component { constructor(props) { @@ -52,65 +52,24 @@ class NewPaste extends React.Component { }); } - 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(); - } - handleSubmit(event) { - var bodyFormData = new FormData(); - bodyFormData.set('title', this.state.title); - bodyFormData.set('content', this.state.content); - bodyFormData.set('password', this.state.pass); - bodyFormData.set('expiry', this.parseExpiry(this.state.expiry)); - - axios({ - method: 'post', - url: 'http://localhost:8080/api', - data: bodyFormData, - headers: { 'Content-Type': 'multipart/form-data' }, - }).then((response) => { - // on success, redir - this.setState({ hash: response.data.hash }) - }).catch((error) => { - const resp = error.response - - // some weird err - if (resp !== undefined) { - const errTxt = `${resp.statusText}: ${resp.data}` - this.newErr(errTxt) - } else { - // some weird err (e.g. network) - this.newErr(error) - } - }); - event.preventDefault(); + PostNewPaste(this.state) + .then((response) => { + // on success, redir + this.setState({ hash: response.data.hash }) + }).catch((error) => { + const resp = error.response + + // some weird err + if (resp !== undefined) { + const errTxt = `${resp.statusText}: ${resp.data}` + this.newErr(errTxt) + } else { + // some weird err (e.g. network) + this.newErr(error) + } + }); } render() { diff --git a/frontend/src/components/ViewPaste.js b/frontend/src/components/ViewPaste.js index d591d19..6a2be4c 100644 --- a/frontend/src/components/ViewPaste.js +++ b/frontend/src/components/ViewPaste.js @@ -1,9 +1,9 @@ import React from 'react'; -import axios from 'axios'; import Error from './Err'; import { TitleInput, PasteInput } from './Inputs'; import PasteInfo from './PasteInfo'; import PasswordModal from './PasswordModal' +import { FetchPaste, FetchPasswordPaste } from './httpHelper' const RENDER_MODES = Object.freeze({ RAW: 'raw text', @@ -74,35 +74,28 @@ class ViewPaste extends React.Component { } validatePass(pass) { - var bodyFormData = new FormData(); - bodyFormData.set('password', pass); - - axios({ - method: 'post', - url: `http://localhost:8080/api/${this.props.hash}`, - data: bodyFormData, - headers: { 'Content-Type': 'multipart/form-data' }, - }).then((response) => { - this.setState({ validPass: true }) - this.setStateFromData(response.data) - }).catch((error) => { - const resp = error.response - - // 401 unauth (bad pass) - if (resp.status === 401) { - this.newPassErr() - return - } - - // otherwise, just log it lmao - if (resp !== undefined) { - const errTxt = `${resp.statusText}: ${resp.data}` - this.newErr(errTxt) - } else { - // some weird err (e.g. network) - this.newErr(error) - } - }); + FetchPasswordPaste(this.props.hash, pass) + .then((response) => { + this.setState({ validPass: true }) + this.setStateFromData(response.data) + }).catch((error) => { + const resp = error.response + + // 401 unauth (bad pass) + if (resp.status === 401) { + this.newPassErr() + return + } + + // otherwise, just log it lmao + if (resp !== undefined) { + const errTxt = `${resp.statusText}: ${resp.data}` + this.newErr(errTxt) + } else { + // some weird err (e.g. network) + this.newErr(error) + } + }); } render() { @@ -146,9 +139,7 @@ class ViewPaste extends React.Component { } componentDidMount() { - const serverURL = `http://localhost:8080/api/${this.props.hash}` - - axios.get(serverURL) + FetchPaste(this.props.hash) .then((response) => { const data = response.data this.setStateFromData(data) diff --git a/frontend/src/components/httpHelper.js b/frontend/src/components/httpHelper.js new file mode 100644 index 0000000..696980c --- /dev/null +++ b/frontend/src/components/httpHelper.js @@ -0,0 +1,67 @@ +import axios from 'axios'; + +const base = `http://localhost:8080/api` + + +export function FetchPaste(hash) { + const serverURL = `${base}/${hash}` + console.log(serverURL) + return axios.get(serverURL) +} + +export function FetchPasswordPaste(hash, pass) { + var bodyFormData = new FormData(); + bodyFormData.set('password', pass); + + return axios({ + method: 'post', + url: `${base}/${hash}`, + data: bodyFormData, + headers: { 'Content-Type': 'multipart/form-data' }, + }) +} + +export function PostNewPaste(state) { + var bodyFormData = new FormData(); + bodyFormData.set('title', state.title); + bodyFormData.set('content', state.content); + bodyFormData.set('password', state.pass); + bodyFormData.set('expiry', parseExpiry(state.expiry)); + + return axios({ + method: 'post', + url: 'http://localhost:8080/api', + data: bodyFormData, + headers: { 'Content-Type': 'multipart/form-data' }, + }) +} + +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(); +}
\ No newline at end of file |