2022-08-13 10:56:07 +02:00
import { t , Trans } from "@lingui/macro"
import { Box , Button , Group , Stack , Stepper , TextInput } from "@mantine/core"
import { useForm } from "@mantine/form"
2022-08-13 18:38:11 +02:00
import { client , errorToStrings } from "app/client"
2022-08-13 10:56:07 +02:00
import { Constants } from "app/constants"
2022-08-13 17:41:31 +02:00
import { redirectToFeed , redirectToSelectedSource } from "app/slices/redirect"
2022-08-13 10:56:07 +02:00
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"
2022-08-13 18:38:11 +02:00
import { useAsyncCallback } from "react-async-hook"
2022-08-13 10:56:07 +02:00
import { TbRss } from "react-icons/tb"
import { CategorySelect } from "./CategorySelect"
export function Subscribe() {
const [ activeStep , setActiveStep ] = useState ( 0 )
const dispatch = useAppDispatch ( )
const step0Form = useForm < FeedInfoRequest > ( {
initialValues : {
url : "" ,
} ,
} )
const step1Form = useForm < SubscribeRequest > ( {
initialValues : {
url : "" ,
title : "" ,
categoryId : Constants.categoryIds.all ,
} ,
} )
2022-08-13 18:38:11 +02:00
const fetchFeed = useAsyncCallback ( client . feed . fetchFeed , {
2022-08-13 10:56:07 +02:00
onSuccess : ( { data } ) = > {
2022-08-13 18:38:11 +02:00
step1Form . setFieldValue ( "url" , data . url )
step1Form . setFieldValue ( "title" , data . title )
2022-08-13 10:56:07 +02:00
setActiveStep ( step = > step + 1 )
} ,
} )
2022-08-13 18:38:11 +02:00
const subscribe = useAsyncCallback ( client . feed . subscribe , {
2022-08-13 17:41:31 +02:00
onSuccess : sub = > {
2022-08-13 10:56:07 +02:00
dispatch ( reloadTree ( ) )
2022-08-13 18:53:34 +02:00
dispatch ( redirectToFeed ( sub . data ) )
2022-08-13 10:56:07 +02:00
} ,
} )
const previousStep = ( ) = > {
if ( activeStep === 0 ) dispatch ( redirectToSelectedSource ( ) )
else setActiveStep ( activeStep - 1 )
}
const nextStep = ( e : React.FormEvent < HTMLFormElement > ) = > {
if ( activeStep === 0 ) {
2022-08-13 18:38:11 +02:00
step0Form . onSubmit ( fetchFeed . execute ) ( e )
2022-08-13 10:56:07 +02:00
} else if ( activeStep === 1 ) {
2022-08-13 18:38:11 +02:00
step1Form . onSubmit ( subscribe . execute ) ( e )
2022-08-13 10:56:07 +02:00
}
}
return (
< >
2022-08-13 18:38:11 +02:00
{ fetchFeed . error && (
2022-08-13 10:56:07 +02:00
< Box mb = "md" >
2022-08-13 18:38:11 +02:00
< Alert messages = { errorToStrings ( fetchFeed . error ) } / >
< / Box >
) }
{ subscribe . error && (
< Box mb = "md" >
< Alert messages = { errorToStrings ( subscribe . error ) } / >
2022-08-13 10:56:07 +02:00
< / Box >
) }
< form onSubmit = { nextStep } >
< Stepper active = { activeStep } onStepClick = { setActiveStep } >
< Stepper.Step
label = { t ` Analyze feed ` }
description = { t ` Check that the feed is working ` }
allowStepSelect = { activeStep === 1 }
>
< TextInput
label = { t ` Feed URL ` }
placeholder = "http://www.mysite.com/rss"
description = { t ` The URL for the feed you want to subscribe to. You can also use the website's url directly and CommaFeed will try to find the feed in the page. ` }
required
autoFocus
{ . . . step0Form . getInputProps ( "url" ) }
/ >
< / Stepper.Step >
< Stepper.Step label = { t ` Subscribe ` } description = { t ` Subscribe to the feed ` } allowStepSelect = { false } >
< Stack >
< TextInput label = { t ` Feed URL ` } { ...step1Form.getInputProps ( "url" ) } disabled / >
< TextInput label = { t ` Feed name ` } { ...step1Form.getInputProps ( "title" ) } required autoFocus / >
< CategorySelect label = { t ` Category ` } { ...step1Form.getInputProps ( "categoryId" ) } clearable / >
< / Stack >
< / Stepper.Step >
< / Stepper >
< Group position = "center" mt = "xl" >
< Button variant = "default" onClick = { previousStep } >
< Trans > Back < / Trans >
< / Button >
{ activeStep === 0 && (
2022-08-13 18:38:11 +02:00
< Button type = "submit" loading = { fetchFeed . loading } >
2022-08-13 10:56:07 +02:00
< Trans > Next < / Trans >
< / Button >
) }
{ activeStep === 1 && (
2022-08-13 18:38:11 +02:00
< Button type = "submit" leftIcon = { < TbRss size = { 16 } / > } loading = { fetchFeed . loading || subscribe . loading } >
2022-08-13 10:56:07 +02:00
< Trans > Subscribe < / Trans >
< / Button >
) }
< / Group >
< / form >
< / >
)
}