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"; const classNames = require('classnames'); const _ = require('lodash'); class ServicePane extends Component { constructor(props) { super(props); this.state = { services: [], currentService: this.emptyService, }; } componentDidMount() { this.reset(); this.loadServices(); } emptyService = { "port": 0, "name": "", "color": "", "notes": "" }; 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 =>
port | name | color | notes |
---|