import {Fragment} from "react"; import {useMutation} from "react-query"; import {Network} from "../../domain/interfaces"; import {Dialog, Transition} from "@headlessui/react"; import {XIcon} from "@heroicons/react/solid"; import {Field, Form} from "react-final-form"; import DEBUG from "../../components/debug"; import {SwitchGroup, TextFieldWide} from "../../components/inputs"; import {queryClient} from "../../App"; import arrayMutators from "final-form-arrays"; import { FieldArray } from "react-final-form-arrays"; import {classNames} from "../../styles/utils"; import APIClient from "../../api/APIClient"; import { NumberFieldWide, PasswordFieldWide } from "../../components/inputs/wide"; import { toast } from 'react-hot-toast'; import Toast from '../../components/notifications/Toast'; type FormValues = { name: string server: string nickserv: { account: string } port: number } function IrcNetworkAddForm({isOpen, toggle}: any) { const mutation = useMutation((network: Network) => APIClient.irc.createNetwork(network), { onSuccess: (data) => { queryClient.invalidateQueries(['networks']); toast.custom((t) => ) toggle() }, onError: () => { toast.custom((t) => ) }, }) const onSubmit = (data: any) => { // easy way to split textarea lines into array of strings for each newline. // parse on the field didn't really work. let cmds = data.connect_commands && data.connect_commands.length > 0 ? data.connect_commands.replace(/\r\n/g,"\n").split("\n") : []; data.connect_commands = cmds console.log("formated", data) mutation.mutate(data) }; const validate = (values: any) => { const errors = { nickserv: { account: null, } } as any; if (!values.name) { errors.name = "Required"; } if (!values.port) { errors.port = "Required"; } if (!values.server) { errors.server = "Required"; } if(!values.nickserv?.account) { errors.nickserv.account = "Required"; } return errors; } return ( {({handleSubmit, values, pristine, invalid}) => { return ( Add network Add irc network. Close panel {({ fields }) => ( {fields && (fields.length as any) > 0 ? ( fields.map((name, index) => ( fields.remove(index)} > Remove )) ) : ( No channels! )} fields.push({ name: "", password: "" })} > Add Channel )} Cancel Create ) }} ) } export default IrcNetworkAddForm;
Add irc network.