diff options
| author | jackyzha0 <[email protected]> | 2020-05-11 21:11:16 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2020-05-11 21:11:16 -0700 |
| commit | 433466a3947e75a36b811795bc21be1fff10b5e8 (patch) | |
| tree | 9e42dad77e7b8bdb3eefbbcfdc3eb7fa90817344 /frontend/src/components/NewPaste.js | |
| parent | add basic link + route (diff) | |
| download | ctrl-v-433466a3947e75a36b811795bc21be1fff10b5e8.tar.xz ctrl-v-433466a3947e75a36b811795bc21be1fff10b5e8.zip | |
backend redir
Diffstat (limited to 'frontend/src/components/NewPaste.js')
| -rw-r--r-- | frontend/src/components/NewPaste.js | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/frontend/src/components/NewPaste.js b/frontend/src/components/NewPaste.js new file mode 100644 index 0000000..2ad9533 --- /dev/null +++ b/frontend/src/components/NewPaste.js @@ -0,0 +1,124 @@ +import React from 'react'; +import { TitleInput, PasteInput } from './Inputs' +import OptionsContainer from './Options' +import axios from 'axios'; +import { Redirect } from 'react-router-dom' + +class NewPaste extends React.Component { + constructor(props) { + super(props); + this.state = { + title: '', + content: '', + pass: '', + expiry: '', + hash: '', + }; + + this.handleChange = this.handleChange.bind(this); + this.handleSubmit = this.handleSubmit.bind(this); + } + + renderRedirect = () => { + if (this.state.hash !== '') { + const redirUrl = `/${this.state.hash}` + return <Redirect to={redirUrl} /> + } + } + + componentDidUpdate() { + if (this.state.title === "") { + document.title = `ctrl-v`; + } else { + document.title = `ctrl-v | ${this.state.title}`; + } + } + + handleChange(event) { + const target = event.target; + const name = target.name; + + this.setState({ + [name]: target.value + }); + } + + 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((response) => { + // TODO: handle error + console.log(response); + }); + + event.preventDefault(); + } + + render() { + return ( + <form onSubmit={this.handleSubmit}> + {this.renderRedirect()} + <TitleInput + onChange={this.handleChange} + value={this.state.title} + maxLength="100" + id="titleInput" /> + <PasteInput + onChange={this.handleChange} + content={this.state.content} + maxLength="100000" + id="pasteInput" /> + <input className="lt-button lt-shadow lt-hover" type="submit" value="new paste" /> + <OptionsContainer + pass={this.state.pass} + expiry={this.state.expiry} + onChange={this.handleChange} /> + </form> + ); + } +} + +export default NewPaste
\ No newline at end of file |