From dee7d7dfcbec7ef4475896935873f04d4df0d40f Mon Sep 17 00:00:00 2001 From: Emiliano Ciavatta Date: Thu, 7 May 2020 12:25:41 +0200 Subject: Add colors to services dialog --- frontend/src/components/Connection.js | 24 +++--- frontend/src/index.scss | 6 +- frontend/src/utils.js | 6 +- frontend/src/views/Services.js | 142 +++++++++++++++++++++++++--------- frontend/src/views/Services.scss | 20 ++++- 5 files changed, 142 insertions(+), 56 deletions(-) (limited to 'frontend/src') diff --git a/frontend/src/components/Connection.js b/frontend/src/components/Connection.js index ce2b173..13b394a 100644 --- a/frontend/src/components/Connection.js +++ b/frontend/src/components/Connection.js @@ -4,24 +4,24 @@ import {Button, OverlayTrigger, Tooltip} from "react-bootstrap"; class Connection extends Component { render() { - let conn = this.props.data - let serviceName = "/dev/null" - let serviceColor = "#0F192E" + let conn = this.props.data; + let serviceName = "/dev/null"; + let serviceColor = "#0F192E"; if (conn.service.port !== 0) { - serviceName = conn.service.name - serviceColor = conn.service.color + serviceName = conn.service.name; + serviceColor = conn.service.color; } - let startedAt = new Date(conn.started_at) - let closedAt = new Date(conn.closed_at) - let duration = ((closedAt - startedAt) / 1000).toFixed(3) - let timeInfo = `Started at ${startedAt}\nClosed at ${closedAt}\nProcessed at ${new Date(conn.processed_at)}` + let startedAt = new Date(conn.started_at); + let closedAt = new Date(conn.closed_at); + let duration = ((closedAt - startedAt) / 1000).toFixed(3); + let timeInfo = `Started at ${startedAt}\nClosed at ${closedAt}\nProcessed at ${new Date(conn.processed_at)}`; - let classes = "connection" + let classes = "connection"; if (this.props.selected) { - classes += " connection-selected" + classes += " connection-selected"; } if (conn.marked){ - classes += " connection-marked" + classes += " connection-marked"; } return ( diff --git a/frontend/src/index.scss b/frontend/src/index.scss index def3fdf..044d150 100644 --- a/frontend/src/index.scss +++ b/frontend/src/index.scss @@ -157,4 +157,8 @@ textarea.form-control { .table { color: $color-primary-4; -} \ No newline at end of file +} + +.text-muted { + color: $color-primary-4 !important; +} diff --git a/frontend/src/utils.js b/frontend/src/utils.js index 2487f09..b72ada7 100644 --- a/frontend/src/utils.js +++ b/frontend/src/utils.js @@ -1,5 +1,5 @@ - export function createCurlCommand(subCommand, data) { - return `curl --request PUT \\\n --url ${window.location.hostname}/api${subCommand} \\\n ` + - `--header 'content-type: application/json' \\\n --data '${JSON.stringify(data)}'` + let full = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : ''); + return `curl --request PUT \\\n --url ${full}/api${subCommand} \\\n ` + + `--header 'content-type: application/json' \\\n --data '${JSON.stringify(data)}'`; } diff --git a/frontend/src/views/Services.js b/frontend/src/views/Services.js index 1b3789d..66d99c6 100644 --- a/frontend/src/views/Services.js +++ b/frontend/src/views/Services.js @@ -8,59 +8,131 @@ 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: "", - portValid: false - } + 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() { - axios.get("/api/services").then(res => this.setState({services: res.data})) + this.loadServices(); } portChanged(event) { - let value = event.target.value.replace(/[^\d]/gi, '') - let intValue = parseInt(value) - this.setState({port: value, portValid: intValue > 0 && intValue <= 65565}) + 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: ""}); + } - render() { - let curl = createCurlCommand("/services", { - "port": this.state.port, - "name": "aaaaa", - "color": "#fff", - "notes": "aaa" - }) + editService(service) { + this.setState({name: service.name, port: service.port, color: service.color, notes: service.notes}); + } + + saveService() { + if (this.state.portValid && this.state.nameValid) { + axios.put("/api/services", { + name: this.state.name, + port: this.state.port, + color: this.state.color, + notes: this.state.notes + }); + + this.newService(); + this.loadServices(); + } + } + + loadServices() { + axios.get("/api/services").then(res => this.setState({services: res.data})); + } + + 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 =>
- | name | +port | +name |
---|