Files
firegex-traffic-viewer/frontend/src/components/AddNewService.tsx

84 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-06-15 01:23:54 +02:00
import { Button, Group, NumberInput, Space, TextInput, Notification, Modal } from '@mantine/core';
2022-06-12 12:09:50 +02:00
import { useForm } from '@mantine/hooks';
import React, { useState } from 'react';
import { ServiceAddForm } from '../js/models';
2022-06-13 10:59:05 +02:00
import { addservice, okNotify } from '../js/utils';
2022-06-12 12:09:50 +02:00
import { ImCross } from "react-icons/im"
2022-06-15 01:23:54 +02:00
function AddNewService({ opened, onClose }:{ opened:boolean, onClose:()=>void }) {
2022-06-12 12:09:50 +02:00
const form = useForm({
initialValues: {
name:"",
port:1,
},
validationRules:{
name: (value) => value !== ""?true:false,
port: (value) => value>0 && value<65536
}
})
2022-06-15 01:23:54 +02:00
const close = () =>{
onClose()
form.reset()
}
2022-06-12 12:09:50 +02:00
const [submitLoading, setSubmitLoading] = useState(false)
const [error, setError] = useState<string|null>(null)
const submitRequest = (values:ServiceAddForm) =>{
setSubmitLoading(true)
addservice(values).then( res => {
if (!res){
setSubmitLoading(false)
2022-06-15 01:23:54 +02:00
close();
2022-06-13 10:59:05 +02:00
okNotify(`Service ${values.name} has been added`, `Successfully added ${values.name} with port ${values.port}`)
2022-06-12 12:09:50 +02:00
}else{
setSubmitLoading(false)
setError("Invalid request! [ "+res+" ]")
}
}).catch( err => {
setSubmitLoading(false)
setError("Request Failed! [ "+err+" ]")
})
}
2022-06-15 01:23:54 +02:00
return <Modal size="xl" title="Add a new service" opened={opened} onClose={close} closeOnClickOutside={false} centered>
<form onSubmit={form.onSubmit(submitRequest)}>
<TextInput
label="Service name"
placeholder="Challenge 01"
{...form.getInputProps('name')}
/>
<Space h="md" />
2022-06-12 12:09:50 +02:00
2022-06-15 01:23:54 +02:00
<NumberInput
placeholder="8080"
min={1}
max={65535}
label="Service port"
{...form.getInputProps('port')}
/>
2022-06-12 12:09:50 +02:00
2022-06-15 01:23:54 +02:00
<Space h="md" />
2022-06-12 12:09:50 +02:00
2022-06-15 01:23:54 +02:00
<Group position="right" mt="md">
<Button loading={submitLoading} type="submit">Add Service</Button>
</Group>
2022-06-12 12:09:50 +02:00
2022-06-15 01:23:54 +02:00
<Space h="md" />
{error?<>
<Notification icon={<ImCross size={14} />} color="red" onClose={()=>{setError(null)}}>
Error: {error}
</Notification><Space h="md" /></>:null}
</form>
</Modal>
2022-06-12 12:09:50 +02:00
}
export default AddNewService;