import { Button, Group, Space, TextInput, Notification, Modal, Switch, SegmentedControl, Box } from '@mantine/core'; import { useForm } from '@mantine/form'; import { useState } from 'react'; import { okNotify, regex_ipv4, regex_ipv6 } from '../../js/utils'; import { ImCross } from "react-icons/im" import { nfregex } from './utils'; import PortAndInterface from '../PortAndInterface'; type ServiceAddForm = { name:string, port:number, proto:string, ip_int:string, autostart: boolean, } function AddNewService({ opened, onClose }:{ opened:boolean, onClose:()=>void }) { const form = useForm({ initialValues: { name:"", port:8080, ip_int:"", proto:"tcp", autostart: true }, validate:{ name: (value) => value !== "" ? null : "Service name is required", port: (value) => (value>0 && value<65536) ? null : "Invalid port", proto: (value) => ["tcp","udp"].includes(value) ? null : "Invalid protocol", ip_int: (value) => (value.match(regex_ipv6) || value.match(regex_ipv4)) ? null : "Invalid IP address", } }) const close = () =>{ onClose() form.reset() setError(null) } const [submitLoading, setSubmitLoading] = useState(false) const [error, setError] = useState(null) const submitRequest = ({ name, port, autostart, proto, ip_int }:ServiceAddForm) =>{ setSubmitLoading(true) nfregex.servicesadd({name, port, proto, ip_int }).then( res => { if (res.status === "ok" && res.service_id){ setSubmitLoading(false) close(); if (autostart) nfregex.servicestart(res.service_id) okNotify(`Service ${name} has been added`, `Successfully added service with port ${port}`) }else{ setSubmitLoading(false) setError("Invalid request! [ "+res.status+" ]") } }).catch( err => { setSubmitLoading(false) setError("Request Failed! [ "+err+" ]") }) } return
{error?<> } color="red" onClose={()=>{setError(null)}}> Error: {error} :null}
} export default AddNewService;