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
|
import React, {Component} from 'react';
import './common.scss';
import './ServicePane.scss';
import Table from "react-bootstrap/Table";
import {Col, Container, Row} from "react-bootstrap";
import InputField from "../fields/InputField";
import TextField from "../fields/TextField";
import backend from "../../backend";
import NumericField from "../fields/extensions/NumericField";
import ColorField from "../fields/extensions/ColorField";
import ButtonField from "../fields/ButtonField";
import validation from "../../validation";
import LinkPopover from "../objects/LinkPopover";
import {createCurlCommand} from "../../utils";
import dispatcher from "../../dispatcher";
const classNames = require('classnames');
const _ = require('lodash');
class ServicePane extends Component {
emptyService = {
"port": 0,
"name": "",
"color": "",
"notes": ""
};
state = {
services: [],
currentService: this.emptyService,
};
componentDidMount() {
this.reset();
this.loadServices();
dispatcher.register("notifications", payload => {
if (payload.event === "services.edit") {
this.loadServices();
}
});
document.title = "caronte:~/services$";
}
loadServices = () => {
backend.get("/api/services")
.then(res => this.setState({services: Object.values(res.json), servicesStatusCode: res.status}))
.catch(res => this.setState({servicesStatusCode: res.status, servicesResponse: JSON.stringify(res.json)}));
};
updateService = () => {
const service = this.state.currentService;
if (this.validateService(service)) {
backend.put("/api/services", service).then(res => {
this.reset();
this.setState({serviceStatusCode: res.status});
this.loadServices();
}).catch(res => {
this.setState({serviceStatusCode: res.status, serviceResponse: JSON.stringify(res.json)});
});
}
};
validateService = (service) => {
let valid = true;
if (!validation.isValidPort(service.port, true)) {
this.setState({servicePortError: "port < 0 || port > 65565"});
valid = false;
}
if (service.name.length < 3) {
this.setState({serviceNameError: "name.length < 3"});
valid = false;
}
if (!validation.isValidColor(service.color)) {
this.setState({serviceColorError: "color is not hexcolor"});
valid = false;
}
return valid;
};
reset = () => {
this.setState({
isUpdate: false,
currentService: _.cloneDeep(this.emptyService),
servicePortError: null,
serviceNameError: null,
serviceColorError: null,
serviceStatusCode: null,
servicesStatusCode: null,
serviceResponse: null,
servicesResponse: null
});
};
updateParam = (callback) => {
callback(this.state.currentService);
this.setState({currentService: this.state.currentService});
};
render() {
const isUpdate = this.state.isUpdate;
const service = this.state.currentService;
let services = this.state.services.map(s =>
<tr key={s.port} onClick={() => {
this.reset();
this.setState({isUpdate: true, currentService: _.cloneDeep(s)});
}} className={classNames("row-small", "row-clickable", {"row-selected": service.port === s.port })}>
<td>{s["port"]}</td>
<td>{s["name"]}</td>
<td><ButtonField name={s["color"]} color={s["color"]} small /></td>
<td>{s["notes"]}</td>
</tr>
);
const curlCommand = createCurlCommand("/services", "PUT", service);
return (
<div className="pane-container service-pane">
<div className="pane-section services-list">
<div className="section-header">
<span className="api-request">GET /api/services</span>
{this.state.servicesStatusCode &&
<span className="api-response"><LinkPopover text={this.state.servicesStatusCode}
content={this.state.servicesResponse}
placement="left" /></span>}
</div>
<div className="section-content">
<div className="section-table">
<Table borderless size="sm">
<thead>
<tr>
<th>port</th>
<th>name</th>
<th>color</th>
<th>notes</th>
</tr>
</thead>
<tbody>
{services}
</tbody>
</Table>
</div>
</div>
</div>
<div className="pane-section service-edit">
<div className="section-header">
<span className="api-request">PUT /api/services</span>
<span className="api-response"><LinkPopover text={this.state.serviceStatusCode}
content={this.state.serviceResponse}
placement="left" /></span>
</div>
<div className="section-content">
<Container className="p-0">
<Row>
<Col>
<NumericField name="port" value={service.port}
onChange={(v) => this.updateParam((s) => s.port = v)}
min={0} max={65565} error={this.state.servicePortError} />
<InputField name="name" value={service.name}
onChange={(v) => this.updateParam((s) => s.name = v)}
error={this.state.serviceNameError} />
<ColorField value={service.color} error={this.state.serviceColorError}
onChange={(v) => this.updateParam((s) => s.color = v)} />
</Col>
<Col>
<TextField name="notes" rows={7} value={service.notes}
onChange={(v) => this.updateParam((s) => s.notes = v)} />
</Col>
</Row>
</Container>
<TextField value={curlCommand} rows={3} readonly small={true}/>
</div>
<div className="section-footer">
{<ButtonField variant="red" name="cancel" bordered onClick={this.reset}/>}
<ButtonField variant={isUpdate ? "blue" : "green"} name={isUpdate ? "update_service" : "add_service"}
bordered onClick={this.updateService} />
</div>
</div>
</div>
);
}
}
export default ServicePane;
|