From 4c6d5b49deb330200cbedc76334e6aa9cd848c94 Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Tue, 12 May 2020 21:39:57 -0700 Subject: working password render --- frontend/src/components/PasswordModal.js | 2 ++ frontend/src/components/ViewPaste.js | 50 ++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) (limited to 'frontend/src/components') diff --git a/frontend/src/components/PasswordModal.js b/frontend/src/components/PasswordModal.js index ef96f9d..4b37dfe 100644 --- a/frontend/src/components/PasswordModal.js +++ b/frontend/src/components/PasswordModal.js @@ -2,6 +2,7 @@ import React from 'react'; import Modal from 'react-modal'; import styled from 'styled-components' import { PassInput } from './Inputs' +import Error from './Err'; const modalStyles = { content: { @@ -63,6 +64,7 @@ class PasswordModal extends React.Component { + diff --git a/frontend/src/components/ViewPaste.js b/frontend/src/components/ViewPaste.js index 4b7c46c..d591d19 100644 --- a/frontend/src/components/ViewPaste.js +++ b/frontend/src/components/ViewPaste.js @@ -24,10 +24,12 @@ class ViewPaste extends React.Component { 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) { @@ -45,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 @@ -60,9 +74,35 @@ class ViewPaste extends React.Component { } validatePass(pass) { - // stub - console.log(pass) - // need to toggle validPass + 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) + } + }); } render() { @@ -73,6 +113,7 @@ class ViewPaste extends React.Component { validPass={this.state.validPass} value={this.state.enteredPass} onChange={this.handleChange} + error={this.state.passError} validateCallback={this.validatePass} /> { const resp = error.response - console.log(resp.status) - // catch 401 unauth (password protected) if (resp.status === 401) { this.setState({hasPass: true}) -- cgit v1.2.3