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/components/NewPaste.js | |
| 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/components/NewPaste.js')
| -rw-r--r-- | frontend/src/components/NewPaste.js | 75 |
1 files changed, 17 insertions, 58 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() { |