import { t, Trans } from "@lingui/macro" import { Box, Button, Group, Stack, Stepper, TextInput } from "@mantine/core" import { useForm } from "@mantine/form" import { client, errorsToStrings, errorToStrings } from "app/client" import { Constants } from "app/constants" import { redirectToFeed, redirectToSelectedSource } from "app/slices/redirect" import { reloadTree } from "app/slices/tree" import { useAppDispatch } from "app/store" import { FeedInfoRequest, SubscribeRequest } from "app/types" import { Alert } from "components/Alert" import { useState } from "react" import { TbRss } from "react-icons/tb" import useMutation from "use-mutation" import { CategorySelect } from "./CategorySelect" export function Subscribe() { const [activeStep, setActiveStep] = useState(0) const dispatch = useAppDispatch() const step0Form = useForm({ initialValues: { url: "", }, }) const step1Form = useForm({ initialValues: { url: "", title: "", categoryId: Constants.categoryIds.all, }, }) const [fetchFeed, fetchFeedResult] = useMutation(client.feed.fetchFeed, { onSuccess: ({ data }) => { step1Form.setFieldValue("url", data.data.url) step1Form.setFieldValue("title", data.data.title) setActiveStep(step => step + 1) }, }) const [subscribe, subscribeResult] = useMutation(client.feed.subscribe, { onSuccess: sub => { dispatch(reloadTree()) dispatch(redirectToFeed(sub.data.data.id)) }, }) const errors = errorsToStrings([fetchFeedResult.error, errorToStrings(subscribeResult.error)]) const previousStep = () => { if (activeStep === 0) dispatch(redirectToSelectedSource()) else setActiveStep(activeStep - 1) } const nextStep = (e: React.FormEvent) => { if (activeStep === 0) { step0Form.onSubmit(fetchFeed)(e) } else if (activeStep === 1) { step1Form.onSubmit(subscribe)(e) } } return ( <> {errors.length > 0 && ( )}
{activeStep === 0 && ( )} {activeStep === 1 && ( )}
) }