From 1ebd7755737895cf0735b4bbd4bdf728858fffae Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Fri, 17 Jul 2020 22:07:26 -0700 Subject: refactor decorators --- frontend/src/components/decorators/CharLimit.js | 10 ++++------ .../src/components/decorators/FloatingLabel.js | 22 ++++++++++------------ 2 files changed, 14 insertions(+), 18 deletions(-) (limited to 'frontend/src/components') diff --git a/frontend/src/components/decorators/CharLimit.js b/frontend/src/components/decorators/CharLimit.js index 623b378..5a6fdca 100644 --- a/frontend/src/components/decorators/CharLimit.js +++ b/frontend/src/components/decorators/CharLimit.js @@ -26,12 +26,10 @@ const Chars = styled.p` `}; `; -class CharLimit extends React.Component { - render() { - return ( - {this.props.maxLength - this.props.content.length}/{this.props.maxLength} - ); - } +const CharLimit = (props) => { + return ( + {props.maxLength - props.content.length}/{props.maxLength} + ); } export default CharLimit \ No newline at end of file diff --git a/frontend/src/components/decorators/FloatingLabel.js b/frontend/src/components/decorators/FloatingLabel.js index e1fc0ba..ef56b44 100644 --- a/frontend/src/components/decorators/FloatingLabel.js +++ b/frontend/src/components/decorators/FloatingLabel.js @@ -17,18 +17,16 @@ const StyledLabel = styled.label` `}; ` -class FloatingLabel extends React.Component { - render() { - return ( - - {this.props.label} - - ); - } +const FloatingLabel = (props) => { + return ( + + {props.label} + + ); } export default FloatingLabel \ No newline at end of file -- cgit v1.2.3 From 1519c913241669b532d13351331e7a8d512e1438 Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Fri, 17 Jul 2020 22:29:38 -0700 Subject: refactor renderers --- frontend/src/components/App.js | 2 +- frontend/src/components/ViewPaste.js | 10 ++-- frontend/src/components/modals/PasswordModal.js | 72 ++++++++++-------------- frontend/src/components/renderers/Latex.js | 56 +++++++++--------- frontend/src/components/renderers/Raw.js | 75 +++++++++++-------------- 5 files changed, 95 insertions(+), 120 deletions(-) (limited to 'frontend/src/components') diff --git a/frontend/src/components/App.js b/frontend/src/components/App.js index ae95dcb..0a5fb8b 100644 --- a/frontend/src/components/App.js +++ b/frontend/src/components/App.js @@ -38,7 +38,7 @@ const GetRawWithParam = () => { ); } -function App() { +const App = () => { return ( diff --git a/frontend/src/components/ViewPaste.js b/frontend/src/components/ViewPaste.js index 117bb18..6bff4ae 100644 --- a/frontend/src/components/ViewPaste.js +++ b/frontend/src/components/ViewPaste.js @@ -52,7 +52,7 @@ class ViewPaste extends React.Component { this.setState({ isRenderMode: !this.state.isRenderMode }); } - validatePass(pass) { + validatePass(pass, onErrorCallBack) { FetchPasswordPaste(this.props.hash, pass) .then((response) => { this.setState({ validPass: true }) @@ -62,19 +62,17 @@ class ViewPaste extends React.Component { // 401 unauth (bad pass) if (resp.status === 401) { - this.PasswordModal.current - .ErrorLabel.current - .showMessage("incorrect pass") + onErrorCallBack("incorrect pass") return } // otherwise, just log it lmao if (resp !== undefined) { const errTxt = `${resp.status}: ${resp.data}` - this.ErrorLabel.current.showMessage(errTxt) + onErrorCallBack(errTxt) } else { // some weird err (e.g. network) - this.ErrorLabel.current.showMessage(error) + onErrorCallBack(error) } }); } diff --git a/frontend/src/components/modals/PasswordModal.js b/frontend/src/components/modals/PasswordModal.js index 527fc54..e250e84 100644 --- a/frontend/src/components/modals/PasswordModal.js +++ b/frontend/src/components/modals/PasswordModal.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useRef } from 'react'; import Modal from 'react-modal'; import { PassInput } from '../Inputs' import { RightPad, LeftPad, ModalHeader, Padding } from './shared' @@ -15,49 +15,39 @@ const modalStyles = { } }; -class PasswordModal extends React.Component { +const PasswordModal = (props) => { + const ErrorLabel = useRef(); + Modal.setAppElement('body'); - componentWillMount() { - Modal.setAppElement('body'); + function submitPassword(e) { + e.preventDefault(); + const password = props.value + props.validateCallback(password, ErrorLabel.current.showMessage) } - constructor(props) { - super(props); - this.submitPassword = this.submitPassword.bind(this); - this.ErrorLabel = React.createRef(); - } - - submitPassword(event) { - const password = this.props.value - this.props.validateCallback(password) - event.preventDefault(); - } - - render() { - return( - -
- - 🚧 err: password protected - - - - - - - - - -
-
- ); - } + return( + +
+ + 🚧 err: password protected + + + + + + + + + +
+
+ ); } export default PasswordModal \ No newline at end of file diff --git a/frontend/src/components/renderers/Latex.js b/frontend/src/components/renderers/Latex.js index dcb9ea3..fd3d4e2 100644 --- a/frontend/src/components/renderers/Latex.js +++ b/frontend/src/components/renderers/Latex.js @@ -8,37 +8,35 @@ const StyledInlineLatex = styled.div` margin-bottom: 1em; ` -class Latex extends React.Component { - render() { - // split by \begin{...} and \end{...} flags - const els = this.props.content.split(/(\\begin\{.*\}[\s\S]*?\\end\{.*\})/gm).map(line => { - // line doesnt start with \begin{...}, safe to split on \\ - if (!line.match(/^(\\begin\{.*\})/)) { - return line.split("\\\\") - } else { - return line - } - }).flat() - - // if <=1 lines, just render block - if (els.length <= 1) { - return ( - - {this.props.content} - - ); +const Latex = (props) => { + // split by \begin{...} and \end{...} flags + const els = props.content.split(/(\\begin\{.*\}[\s\S]*?\\end\{.*\})/gm).map(line => { + // line doesnt start with \begin{...}, safe to split on \\ + if (!line.match(/^(\\begin\{.*\})/)) { + return line.split("\\\\") } else { - // new inline block for every line - const blocks = els.map(line => - - - {line} - - - ) - - return blocks; + return line } + }).flat() + + // if <=1 lines, just render block + if (els.length <= 1) { + return ( + + {props.content} + + ); + } else { + // new inline block for every line + const blocks = els.map(line => + + + {line} + + + ) + + return blocks; } } diff --git a/frontend/src/components/renderers/Raw.js b/frontend/src/components/renderers/Raw.js index 7f8e7c1..d4dc830 100644 --- a/frontend/src/components/renderers/Raw.js +++ b/frontend/src/components/renderers/Raw.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import styled from 'styled-components' import { FetchPaste } from '../../helpers/httpHelper' @@ -10,48 +10,37 @@ const RawText = styled.pre` padding: 0 1em; ` -class Raw extends React.Component { - - constructor(props) { - super(props); - this.state = { - content: '', - }; - } - - render() { - return ( - - {this.state.content} - - ); - } - - componentDidMount() { - FetchPaste(this.props.hash) - .then((response) => { - const data = response.data - this.setState({ content: data.content }) - }).catch((error) => { - const resp = error.response - - // catch 401 unauth (password protected) - if (resp.status === 401) { - this.setState({ content: 'err: password protected' }) - return - } - - // some weird err - if (resp !== undefined) { - const errTxt = `${resp.statusText}: ${resp.data}` - this.setState({ content: errTxt }) - return - } - - // some weird err (e.g. network) - this.setState({ content: error }) - }) - } +const Raw = ({hash}) => { + const [content, setContent] = useState(''); + + useEffect(() => { + FetchPaste(hash) + .then((response) => { + const data = response.data + setContent(data.content) + }).catch((error) => { + const resp = error.response + + // catch 401 unauth (password protected) + if (resp.status === 401) { + setContent('err: password protected') + return + } + + // some weird err + if (resp !== undefined) { + const errTxt = `${resp.statusText}: ${resp.data}` + setContent(errTxt) + return + } + + // some weird err (e.g. network) + setContent(error) + })}, [hash]) + + return ( + {content} + ); } export default Raw \ No newline at end of file -- cgit v1.2.3 From d80bc9f45c4ab06a8a8bc38fc436bb5345225514 Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Sat, 18 Jul 2020 14:51:54 -0700 Subject: refactor newpaste --- frontend/src/components/Inputs.js | 233 ++++++++++++++++-------------------- frontend/src/components/NewPaste.js | 187 ++++++++++++----------------- frontend/src/components/Options.js | 36 +++--- 3 files changed, 200 insertions(+), 256 deletions(-) (limited to 'frontend/src/components') diff --git a/frontend/src/components/Inputs.js b/frontend/src/components/Inputs.js index 08cf3fc..a9b08b7 100644 --- a/frontend/src/components/Inputs.js +++ b/frontend/src/components/Inputs.js @@ -15,43 +15,33 @@ const FlexChild = styled.div` margin-left: 2em; ` -class TitleInput extends React.Component { - render() { - return ( - - - - - - ); - } +const TitleInput = (props) => { + return ( + + + + + + ); } -class PasteInput extends React.Component { - - constructor(props) { - super(props) - - this.textArea = React.createRef() - this.handleKeyDown = this.handleKeyDown.bind(this) - } - - handleKeyDown(e) { +const PasteInput = (props) => { + function handleKeyDown(e) { if (e.keyCode === 9) { // tab was pressed // prevent autofocus on next intput @@ -61,96 +51,83 @@ class PasteInput extends React.Component { const start = e.target.selectionStart const end = e.target.selectionEnd - this.props.insertTabCallback(start, end) + props.insertTabCallback(start, end) // set cursor position to be at start e.target.selectionEnd = end + 4; } } - render() { - return ( + return ( + + +