From b8118ba07534cdcd01788c6c508be267da1fa87e Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Tue, 12 May 2020 22:28:19 -0700 Subject: abstract http funcs into helper --- frontend/src/components/NewPaste.js | 75 +++++++++---------------------------- 1 file changed, 17 insertions(+), 58 deletions(-) (limited to 'frontend/src/components/NewPaste.js') 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() { -- cgit v1.2.3 From 93a5b395effd88a783ef4bcd4162de281f6281ee Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Tue, 12 May 2020 22:30:23 -0700 Subject: fix dir --- frontend/src/components/NewPaste.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'frontend/src/components/NewPaste.js') diff --git a/frontend/src/components/NewPaste.js b/frontend/src/components/NewPaste.js index dbcc5a7..1409c22 100644 --- a/frontend/src/components/NewPaste.js +++ b/frontend/src/components/NewPaste.js @@ -3,7 +3,7 @@ import { TitleInput, PasteInput } from './Inputs' import OptionsContainer from './Options' import { Redirect } from 'react-router-dom' import Error from './Err' -import { PostNewPaste } from './httpHelper' +import { PostNewPaste } from '../helpers/httpHelper' class NewPaste extends React.Component { constructor(props) { -- cgit v1.2.3 From 94a9b304546dd1213ef2f15e57f5966ab88b80b7 Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Tue, 12 May 2020 23:08:20 -0700 Subject: abstract err show logic --- frontend/src/components/NewPaste.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'frontend/src/components/NewPaste.js') diff --git a/frontend/src/components/NewPaste.js b/frontend/src/components/NewPaste.js index 1409c22..f48f48b 100644 --- a/frontend/src/components/NewPaste.js +++ b/frontend/src/components/NewPaste.js @@ -19,13 +19,7 @@ class NewPaste extends React.Component { this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); - } - - newErr(msg, duration = 5000) { - this.setState({ error: msg }) - setTimeout(() => { - this.setState({ error: '' }) - }, duration); + this.ErrorLabel = React.createRef(); } renderRedirect = () => { @@ -64,10 +58,10 @@ class NewPaste extends React.Component { // some weird err if (resp !== undefined) { const errTxt = `${resp.statusText}: ${resp.data}` - this.newErr(errTxt) + this.ErrorLabel.current.showMessage(errTxt) } else { // some weird err (e.g. network) - this.newErr(error) + this.ErrorLabel.current.showMessage(error) } }); } @@ -87,7 +81,7 @@ class NewPaste extends React.Component { maxLength="100000" id="pasteInput" /> - +