import { useMutation } from "react-query"; import { toast } from "react-hot-toast"; import { XMarkIcon } from "@heroicons/react/24/solid"; import type { FieldProps } from "formik"; import { Field, FieldArray, FormikErrors, FormikValues } from "formik"; import { queryClient } from "../../App"; import { APIClient } from "../../api/APIClient"; import { NumberFieldWide, PasswordFieldWide, SwitchGroupWide, TextFieldWide } from "../../components/inputs"; import { SlideOver } from "../../components/panels"; import Toast from "../../components/notifications/Toast"; import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; interface ChannelsFieldArrayProps { channels: IrcChannel[]; } const ChannelsFieldArray = ({ channels }: ChannelsFieldArrayProps) => (
{({ remove, push }) => (
{channels && channels.length > 0 ? ( channels.map((_channel: IrcChannel, index: number) => (
{({ field }: FieldProps) => ( )} {({ field }: FieldProps) => ( )}
)) ) : ( No channels! )}
)}
); interface IrcNetworkAddFormValues { name: string; enabled: boolean; server : string; port: number; tls: boolean; pass: string; nickserv: NickServ; channels: IrcChannel[]; } interface AddFormProps { isOpen: boolean; toggle: () => void; } export function IrcNetworkAddForm({ isOpen, toggle }: AddFormProps) { const mutation = useMutation( (network: IrcNetwork) => APIClient.irc.createNetwork(network), { onSuccess: () => { queryClient.invalidateQueries(["networks"]); toast.custom((t) => ); toggle(); }, onError: () => { toast.custom((t) => ); } } ); const onSubmit = (data: unknown) => { mutation.mutate(data as IrcNetwork); }; const validate = (values: FormikValues) => { const errors = {} as FormikErrors; if (!values.name) errors.name = "Required"; if (!values.port) errors.port = "Required"; if (!values.server) errors.server = "Required"; if (!values.nickserv || !values.nickserv.account) errors.nickserv = { account: "Required" }; return errors; }; const initialValues: IrcNetworkAddFormValues = { name: "", enabled: true, server: "", port: 6667, tls: false, pass: "", nickserv: { account: "" }, channels: [] }; return ( {(values) => (
ADD NETWORKS VIA INDEXERS! ONLY USE THIS IS YOU DELETED NETWORKS
)}
); } interface IrcNetworkUpdateFormValues { id: number; name: string; enabled: boolean; server: string; port: number; tls: boolean; nickserv?: NickServ; pass: string; invite_command: string; channels: Array; } interface IrcNetworkUpdateFormProps { isOpen: boolean; toggle: () => void; network: IrcNetwork; } export function IrcNetworkUpdateForm({ isOpen, toggle, network }: IrcNetworkUpdateFormProps) { const mutation = useMutation((network: IrcNetwork) => 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: unknown) => { mutation.mutate(data as IrcNetwork); }; const validate = (values: FormikValues) => { const errors = {} as FormikErrors; 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: IrcNetworkUpdateFormValues = { id: network.id, name: network.name, enabled: network.enabled, server: network.server, port: network.port, tls: network.tls, nickserv: network.nickserv, pass: network.pass, channels: network.channels, invite_command: network.invite_command }; return ( {(values) => (
)}
); }