aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/views/Services.js
blob: 22d61b394e97dfcd075681ee2abb5ae5a31d92c8 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, {Component} from 'react';
import './Services.scss';
import {Button, ButtonGroup, Col, Container, Form, FormControl, InputGroup, Modal, Row, Table} from "react-bootstrap";
import {createCurlCommand} from '../utils';
import backend from "../backend";

class Services extends Component {

    constructor(props) {
        super(props);
        this.alphabet = 'abcdefghijklmnopqrstuvwxyz';
        this.colors = ["#E53935", "#D81B60", "#8E24AA", "#5E35B1", "#3949AB", "#1E88E5", "#039BE5", "#00ACC1",
            "#00897B", "#43A047", "#7CB342", "#9E9D24", "#F9A825", "#FB8C00", "#F4511E", "#6D4C41"];

        this.state = {
            services: {},
            port: 0,
            portValid: false,
            name: "",
            nameValid: false,
            color: this.colors[0],
            colorValid: false,
            notes: ""
        };

        this.portChanged = this.portChanged.bind(this);
        this.nameChanged = this.nameChanged.bind(this);
        this.notesChanged = this.notesChanged.bind(this);
        this.newService = this.newService.bind(this);
        this.editService = this.editService.bind(this);
        this.saveService = this.saveService.bind(this);
        this.loadServices = this.loadServices.bind(this);
    }

    componentDidMount() {
        this.loadServices();
    }

    portChanged(event) {
        let value = event.target.value.replace(/[^\d]/gi, '');
        let port = 0;
        if (value !== "") {
            port = parseInt(value);
        }
        this.setState({port: port});
    }

    nameChanged(event) {
        let value = event.target.value.replace(/[\s]/gi, '_').replace(/[^\w]/gi, '').toLowerCase();
        this.setState({name: value});
    }

    notesChanged(event) {
        this.setState({notes: event.target.value});
    }

    newService() {
        this.setState({name: "", port: 0, notes: ""});
    }

    editService(service) {
        this.setState({name: service.name, port: service.port, color: service.color, notes: service.notes});
    }

    saveService() {
        if (this.state.portValid && this.state.nameValid) {
			backend.put("/api/services", {
                color: this.state.color,
                name: this.state.name,
                notes: this.state.notes,
                port: this.state.port,
            }).then(_ => {
                this.newService();
                this.loadServices();
            });
        }
    }

    loadServices() {
        backend.get("/api/services").then(res => this.setState({services: res}));
    }

    componentDidUpdate(prevProps, prevState, snapshot) {
        if (this.state.name != null && prevState.name !== this.state.name) {
            this.setState({
                nameValid: this.state.name.length >= 3
            });
        }
        if (prevState.port !== this.state.port) {
            this.setState({
                portValid: this.state.port > 0 && this.state.port <= 65565
            });
        }
    }

    render() {
        let output = "";
        if (!this.state.portValid) {
            output += "assert(1 <= port <= 65565)\n";
        }
        if (!this.state.nameValid) {
            output += "assert(len(name) >= 3)\n";
        }
        if (output === "") {
            output = createCurlCommand("/services", {
                "port": this.state.port,
                "name": this.state.name,
                "color": this.state.color,
                "notes": this.state.notes
            });
        }
        let rows = Object.values(this.state.services).map(s =>
            <tr>
                <td><Button variant="btn-edit" size="sm"
                            onClick={() => this.editService(s)} style={{"backgroundColor": s.color}}>edit</Button></td>
                <td>{s.port}</td>
                <td>{s.name}</td>
            </tr>
        );

        let colorButtons = this.colors.map((color, i) =>
            <Button size="sm" className="btn-color" key={"button" + this.alphabet[i]}
                    style={{"backgroundColor": color, "borderColor": this.state.color === color ? "#fff" : color}}
                    onClick={() => this.setState({color: color})}>{this.alphabet[i]}</Button>);

        return (
            <Modal
                {...this.props}
                show="true"
                size="lg"
                aria-labelledby="services-dialog"
                centered
            >
                <Modal.Header>
                    <Modal.Title id="services-dialog">
                        ~/services
                    </Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <Container>
                        <Row>
                            <Col md={7}>
                                <Table borderless size="sm" className="services-list">
                                    <thead>
                                    <tr>
                                        <th><Button size="sm" onClick={this.newService}>new</Button></th>
                                        <th>port</th>
                                        <th>name</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {rows}
                                    </tbody>
                                </Table>
                            </Col>
                            <Col md={5}>
                                <Form>
                                    <Form.Group controlId="servicePort">
                                        <Form.Label>port:</Form.Label>
                                        <Form.Control type="text" onChange={this.portChanged} value={this.state.port}/>
                                    </Form.Group>

                                    <Form.Group controlId="serviceName">
                                        <Form.Label>name:</Form.Label>
                                        <Form.Control type="text" onChange={this.nameChanged} value={this.state.name}/>
                                    </Form.Group>

                                    <Form.Group controlId="serviceColor">
                                        <Form.Label>color:</Form.Label>
                                        <ButtonGroup aria-label="Basic example">
                                            {colorButtons.slice(0, 8)}
                                        </ButtonGroup>
                                        <ButtonGroup aria-label="Basic example">
                                            {colorButtons.slice(8, 18)}
                                        </ButtonGroup>
                                    </Form.Group>

                                    <Form.Group controlId="serviceNotes">
                                        <Form.Label>notes:</Form.Label>
                                        <Form.Control as="textarea" rows={3} onChange={this.notesChanged}
                                                      value={this.state.notes}/>
                                    </Form.Group>
                                </Form>


                            </Col>

                        </Row>

                        <Row>
                            <Col md={12}>
                                <InputGroup>
                                    <FormControl as="textarea" rows={4} className="curl-output" readOnly={true}
                                                 value={output}/>
                                </InputGroup>

                            </Col>
                        </Row>
                    </Container>
                </Modal.Body>
                <Modal.Footer className="dialog-footer">
                    <Button variant="green" onClick={this.saveService}>save</Button>
                    <Button variant="red" onClick={this.props.onHide}>close</Button>
                </Modal.Footer>
            </Modal>
        );
    }
}

export default Services;