import * as React from "react"; import Alert from "../elements/alert"; import {Link} from "react-router-dom"; import Icon from "../elements/icon"; import ReactTooltip from "react-tooltip"; import Select from 'react-select'; export default class PageOverview extends React.Component { constructor(props) { super(props); this.parent = { api: props.api }; this.state = { routes: [], errors: [], isResetting: false, isSaving: false }; this.optionMap = { "redirect_temporary": "Redirect Temporary", "redirect_permanently": "Redirect Permanently", "static": "Serve Static", "dynamic": "Load Dynamic", }; this.options = []; for (let key in this.optionMap) { this.options.push(this.buildOption(key)); } } buildOption(key) { if (typeof key === 'object' && key.hasOwnProperty("key") && key.hasOwnProperty("label")) { return key; } else if (typeof key === 'string') { return { value: key, label: this.optionMap[key] }; } else { return this.options[key]; } } removeError(i) { if (i >= 0 && i < this.state.errors.length) { let errors = this.state.errors.slice(); errors.splice(i, 1); this.setState({...this.state, errors: errors}); } } componentDidMount() { this.fetchRoutes() } render() { let errors = []; let rows = []; for (let i = 0; i < this.state.errors.length; i++) { errors.push( this.removeError(i)} {...this.state.errors[i]}/>) } const inputStyle = { fontFamily: "Courier", paddingTop: "14px" }; for (let i = 0; i < this.state.routes.length; i++) { let route = this.state.routes[i]; rows.push( this.changeRequest(i, e)} /> this.changeTarget(i, e)} /> this.changeExtra(i, e)} /> this.changeActive(i, e)} /> this.removeRoute(i)}/> ); } return <>

Routes & Pages

  1. Home
  2. Pages
{errors}
{rows}
Request  Action  Target  Extra  Active 
} onResetRoutes() { this.setState({ ...this.state, isResetting: true }); this.fetchRoutes(); } onSaveRoutes() { this.setState({ ...this.state, isSaving: true }); let routes = []; for (let i = 0; i < this.state.routes.length; i++) { let route = this.state.routes[i]; routes.push({ request: route.request, action: typeof route.action === 'object' ? route.action.value : route.action, target: route.target, extra: route.extra ?? "", active: route.active === 1 }); } this.parent.api.saveRoutes(routes).then((res) => { if (res.success) { this.setState({...this.state, isSaving: false}); } else { let errors = this.state.errors.slice(); errors.push({ title: "Error saving routes", message: res.msg }); this.setState({...this.state, errors: errors, isSaving: false}); } }); } changeRoute(index, key, value) { if (index < 0 || index >= this.state.routes.length) return; let routes = this.state.routes.slice(); routes[index][key] = value; this.setState({ ...this.state, routes: routes }); } removeRoute(index) { if (index < 0 || index >= this.state.routes.length) return; let routes = this.state.routes.slice(); routes.splice(index, 1); this.setState({ ...this.state, routes: routes }); } onAddRoute() { let routes = this.state.routes.slice(); routes.push({ request: "", action: "dynamic", target: "", extra: "", active: 1 }); this.setState({ ...this.state, routes: routes }); } changeAction(index, selectedOption) { this.changeRoute(index, "action", selectedOption); } changeActive(index, e) { this.changeRoute(index, "active", e.target.checked ? 1 : 0); } changeRequest(index, e) { this.changeRoute(index, "request", e.target.value); } changeTarget(index, e) { this.changeRoute(index, "target", e.target.value); } changeExtra(index, e) { this.changeRoute(index, "extra", e.target.value); } fetchRoutes() { this.parent.api.getRoutes().then((res) => { if (res.success) { this.setState({...this.state, routes: res.routes, isResetting: false}); ReactTooltip.rebuild(); } else { let errors = this.state.errors.slice(); errors.push({ title: "Error fetching routes", message: res.msg }); this.setState({...this.state, errors: errors, isResetting: false}); } }); } }