import { useMutation } from "react-query";
import { toast } from "react-hot-toast";
import { XIcon } from "@heroicons/react/solid";
import { Field, FieldArray } from "formik";
import type { FieldProps } from "formik";
import { queryClient } from "../../App";
import APIClient from "../../api/APIClient";
import {
TextFieldWide,
PasswordFieldWide,
SwitchGroupWide,
NumberFieldWide
} from "../../components/inputs/input_wide";
import { SlideOver } from "../../components/panels";
import Toast from '../../components/notifications/Toast';
function ChannelsFieldArray({ values }: any) {
return (
{({ remove, push }) => (
{values && values.channels.length > 0 ? (
values.channels.map((_channel: Channel, index: number) => (
))
) : (
No channels!
)}
)}
)
}
export function IrcNetworkAddForm({ isOpen, toggle }: any) {
const mutation = useMutation((network: Network) => APIClient.irc.createNetwork(network), {
onSuccess: () => {
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.
const 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;
}
const initialValues = {
name: "",
enabled: true,
server: "",
port: 6667,
tls: false,
pass: "",
nickserv: {
account: ""
},
channels: [],
}
return (
{(values) => (
<>
>
)}
)
}
export function IrcNetworkUpdateForm({ isOpen, toggle, network }: any) {
const mutation = useMutation((network: Network) => APIClient.irc.updateNetwork(network), {
onSuccess: () => {
queryClient.invalidateQueries(['networks']);
toast.custom((t) => )
toggle()
}
})
const deleteMutation = useMutation((id: number) => APIClient.irc.deleteNetwork(id), {
onSuccess: () => {
queryClient.invalidateQueries(['networks']);
toast.custom((t) => )
toggle()
}
})
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.
// TODO fix connect_commands on network update
// 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("formatted", data)
mutation.mutate(data)
};
const validate = (values: any) => {
const errors = {} as any;
if (!values.name) {
errors.name = "Required";
}
if (!values.server) {
errors.server = "Required";
}
if (!values.port) {
errors.port = "Required";
}
if (!values.nickserv?.account) {
errors.nickserv.account = "Required";
}
return errors;
}
const deleteAction = () => {
deleteMutation.mutate(network.id)
}
const initialValues = {
id: network.id,
name: network.name,
enabled: network.enabled,
server: network.server,
port: network.port,
tls: network.tls,
nickserv: network.nickserv,
pass: network.pass,
invite_command: network.invite_command,
channels: network.channels
}
return (
{(values) => (
<>
>
)}
)
}