/* * Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors. * SPDX-License-Identifier: GPL-2.0-or-later */ import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { toast } from "react-hot-toast"; import { useFormikContext } from "formik"; import { APIClient } from "@api/APIClient"; import { FeedKeys } from "@api/query_keys"; import Toast from "@components/notifications/Toast"; import { SlideOver } from "@components/panels"; import { NumberFieldWide, PasswordFieldWide, SwitchGroupWide, TextFieldWide } from "@components/inputs"; import { SelectFieldBasic } from "@components/inputs/select_wide"; import { componentMapType } from "./DownloadClientForms"; import { sleep } from "@utils"; import { ImplementationBadges } from "@screens/settings/Indexer"; import { FeedDownloadTypeOptions } from "@domain/constants"; interface UpdateProps { isOpen: boolean; toggle: () => void; feed: Feed; } interface InitialValues { id: number; indexer: string; enabled: boolean; type: FeedType; name: string; url: string; api_key: string; cookie: string; interval: number; timeout: number; max_age: number; settings: FeedSettings; } export function FeedUpdateForm({ isOpen, toggle, feed }: UpdateProps) { const [isTesting, setIsTesting] = useState(false); const [isTestSuccessful, setIsSuccessfulTest] = useState(false); const [isTestError, setIsErrorTest] = useState(false); const queryClient = useQueryClient(); const mutation = useMutation({ mutationFn: (feed: Feed) => APIClient.feeds.update(feed), onSuccess: () => { queryClient.invalidateQueries({ queryKey: FeedKeys.lists() }); toast.custom((t) => ); toggle(); } }); const onSubmit = (formData: unknown) => mutation.mutate(formData as Feed); const deleteMutation = useMutation({ mutationFn: (feedID: number) => APIClient.feeds.delete(feedID), onSuccess: () => { queryClient.invalidateQueries({ queryKey: FeedKeys.lists() }); toast.custom((t) => ); } }); const deleteAction = () => deleteMutation.mutate(feed.id); const testFeedMutation = useMutation({ mutationFn: (feed: Feed) => APIClient.feeds.test(feed), onMutate: () => { setIsTesting(true); setIsErrorTest(false); setIsSuccessfulTest(false); }, onSuccess: () => { toast.custom((t) => ); sleep(1000) .then(() => { setIsTesting(false); setIsSuccessfulTest(true); }) .then(() => { sleep(2500).then(() => { setIsSuccessfulTest(false); }); }); }, onError: () => { setIsTesting(false); setIsErrorTest(true); sleep(2500).then(() => { setIsErrorTest(false); }); } }); const testFeed = (data: unknown) => testFeedMutation.mutate(data as Feed); const initialValues: InitialValues = { id: feed.id, indexer: feed.indexer, enabled: feed.enabled, type: feed.type, name: feed.name, url: feed.url, api_key: feed.api_key, cookie: feed.cookie || "", interval: feed.interval, timeout: feed.timeout, max_age: feed.max_age, settings: feed.settings }; return ( type="UPDATE" title="Feed" isOpen={isOpen} toggle={toggle} onSubmit={onSubmit} deleteAction={deleteAction} initialValues={initialValues} testFn={testFeed} isTesting={isTesting} isTestSuccessful={isTestSuccessful} isTestError={isTestError} > {(values) => (
{ImplementationBadges[feed.type.toLowerCase()]}
{componentMap[values.type]}
)} ); } function WarningLabel() { return (
Warning: Indexers might ban you for too low interval! Read the indexer rules.
); } function FormFieldsTorznab() { const { values: { interval } } = useFormikContext(); return (
{interval < 15 && }
); } function FormFieldsNewznab() { const { values: { interval } } = useFormikContext(); return (
{interval < 15 && }
); } function FormFieldsRSS() { const { values: { interval } } = useFormikContext(); return (
{interval < 15 && }
); } const componentMap: componentMapType = { TORZNAB: , NEWZNAB: , RSS: };