aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/NewPaste.js
blob: 2ce2ecfbff7dda98e7b8a7c644b50cd117eab58a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React from 'react';
import { TitleInput, PasteInput } from './Inputs'
import OptionsContainer from './Options'
import axios from 'axios';
import { Redirect } from 'react-router-dom'
import styled from 'styled-components'

const ErrMsg = styled.p`
    display: inline-block;
    font-weight: 700;
    margin-left: 2em;
    color: #ff3333
`

const Error = (props) => {
    const msg = props.msg.toString().toLowerCase()
    return (
        <ErrMsg> { msg } </ErrMsg>
    );
}

class NewPaste extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            title: '',
            content: '',
            pass: '',
            expiry: '',
            hash: '',
            error: '',
        };

        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    newErr(msg) {
        this.setState({ error: msg })
        setTimeout(() => {
            this.setState({ error: '' })
        }, 3000);
    }

    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((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();
    }

    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" />
                <Error msg={this.state.error} />
                <OptionsContainer
                    pass={this.state.pass}
                    expiry={this.state.expiry}
                    onChange={this.handleChange} />
            </form>
        );
    }
}

export default NewPaste