aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/ViewPaste.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/components/ViewPaste.js')
-rw-r--r--frontend/src/components/ViewPaste.js89
1 files changed, 77 insertions, 12 deletions
diff --git a/frontend/src/components/ViewPaste.js b/frontend/src/components/ViewPaste.js
index 79b1840..d7bd355 100644
--- a/frontend/src/components/ViewPaste.js
+++ b/frontend/src/components/ViewPaste.js
@@ -1,8 +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 '../helpers/httpHelper'
const RENDER_MODES = Object.freeze({
RAW: 'raw text',
@@ -19,10 +20,20 @@ class ViewPaste extends React.Component {
title: 'untitled paste',
content: '',
hasPass: false,
+ enteredPass: '',
+ validPass: false,
expiry: 'no expiry',
error: '',
+ passError: '',
mode: RENDER_MODES.RAW,
};
+
+ this.handleChange = this.handleChange.bind(this);
+ this.validatePass = this.validatePass.bind(this);
+ }
+
+ handleChange(event) {
+ this.setState({ enteredPass: event.target.value });
}
newErr(msg, duration = 5000) {
@@ -36,6 +47,18 @@ class ViewPaste extends React.Component {
}
}
+ newPassErr() {
+ // shake thing
+ // set err field and clear input
+ this.setState({
+ passError: "incorrect pass",
+ enteredPass: "",
+ })
+ setTimeout(() => {
+ this.setState({ passError: '' })
+ }, 3000);
+ }
+
drawRightMode() {
switch (this.state.mode) {
// TODO: add other renderers
@@ -50,9 +73,41 @@ class ViewPaste extends React.Component {
}
}
+ validatePass(pass) {
+ 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() {
return (
<div>
+ <PasswordModal
+ hasPass={this.state.hasPass}
+ validPass={this.state.validPass}
+ value={this.state.enteredPass}
+ onChange={this.handleChange}
+ error={this.state.passError}
+ validateCallback={this.validatePass} />
<TitleInput
value={this.state.title}
id="titleInput"
@@ -74,28 +129,38 @@ class ViewPaste extends React.Component {
return d.toLocaleDateString("en-US", options).toLocaleLowerCase()
}
- componentDidMount() {
- const serverURL = `http://localhost:8080/api/${this.props.hash}`
+ setStateFromData(data) {
+ console.log(data)
+ this.setState({
+ title: data.title,
+ content: data.content,
+ expiry: this.fmtDateStr(data.expiry),
+ })
+ }
- axios.get(serverURL)
+ componentDidMount() {
+ FetchPaste(this.props.hash)
.then((response) => {
const data = response.data
- this.setState({
- title: data.title,
- content: data.content,
- expiry: this.fmtDateStr(data.expiry),
- })
+ this.setStateFromData(data)
}).catch((error) => {
const resp = error.response
+ // catch 401 unauth (password protected)
+ if (resp.status === 401) {
+ this.setState({hasPass: true})
+ return
+ }
+
// some weird err
if (resp !== undefined) {
const errTxt = `${resp.statusText}: ${resp.data}`
this.newErr(errTxt, -1)
- } else {
- // some weird err (e.g. network)
- this.newErr(error, -1)
+ return
}
+
+ // some weird err (e.g. network)
+ this.newErr(error, -1)
})
}
}