import { Button, Group, Space, TextInput, Notification, Modal, Switch } from '@mantine/core'; import { useForm } from '@mantine/form'; import { useState } from 'react'; import { okNotify } from '../../js/utils'; import { ImCross } from "react-icons/im" import { regexproxy } from './utils'; import PortInput from '../PortInput'; type ServiceAddForm = { name:string, port:number, autostart: boolean, chosenInternalPort: boolean, internalPort: number } function AddNewService({ opened, onClose }:{ opened:boolean, onClose:()=>void }) { const form = useForm({ initialValues: { name:"", port:8080, internalPort:30001, chosenInternalPort:false, autostart: true }, validate:{ name: (value) => value !== ""? null : "Service name is required", port: (value) => (value>0 && value<65536) ? null : "Invalid port", internalPort: (value) => (value>0 && value<65536) ? null : "Invalid internal port", } }) const close = () =>{ onClose() form.reset() setError(null) } const [submitLoading, setSubmitLoading] = useState(false) const [error, setError] = useState(null) const submitRequest = ({ name, port, autostart, chosenInternalPort, internalPort }:ServiceAddForm) =>{ setSubmitLoading(true) regexproxy.servicesadd(chosenInternalPort?{ internalPort, name, port }:{ name, port }).then( res => { if (res.status === "ok"){ setSubmitLoading(false) close(); if (autostart) regexproxy.servicestart(res.id) okNotify(`Service ${name} has been added`, `Successfully added ${res.id} with port ${port}`) }else{ setSubmitLoading(false) setError("Invalid request! [ "+res.status+" ]") } }).catch( err => { setSubmitLoading(false) setError("Request Failed! [ "+err+" ]") }) } return
{form.values.chosenInternalPort?<> :null} {error?<> } color="red" onClose={()=>{setError(null)}}> Error: {error} :null}
} export default AddNewService;