Skip to content

Commit

Permalink
feat: subsctiption onSuccess
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 13, 2024
1 parent 3cd33ea commit b4da0bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/renderer/src/components/follow/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ import { Button } from "@renderer/components/ui/button"
import { Dialog, DialogTrigger } from "@renderer/components/ui/dialog"
import { FollowDialog } from "./dialog"
import { FeedResponse } from "@renderer/lib/types"
import { useState } from "react"

export function FollowButton({ feed }: { feed: Partial<FeedResponse> }) {
const [open, setOpen] = useState(false)
const [isSuccessful, setIsSuccessful] = useState(false)

return (
<Dialog>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger>
<Button>Follow</Button>
{isSuccessful ? (
<Button variant="outline" disabled>
Followed
</Button>
) : (
<Button>Follow</Button>
)}
</DialogTrigger>
<FollowDialog feed={feed} />
<FollowDialog
feed={feed}
onSuccess={() => {
setOpen(false)
setIsSuccessful(true)
}}
/>
</Dialog>
)
}
13 changes: 10 additions & 3 deletions src/renderer/src/components/follow/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ import { FollowSummary } from "../feed-summary"
const formSchema = z.object({
view: z.enum(["0", "1", "2", "3", "4", "5"]),
category: z.string().optional(),
private: z.boolean(),
private: z.boolean().optional(),
})

export function FollowDialog({ feed }: { feed: Partial<FeedResponse> }) {
export function FollowDialog({
feed,
onSuccess,
}: {
feed: Partial<FeedResponse>
onSuccess?: (values: z.infer<typeof formSchema>) => void
}) {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
Expand Down Expand Up @@ -70,8 +76,9 @@ export function FollowDialog({ feed }: { feed: Partial<FeedResponse> }) {
},
onSuccess: (_, variables) => {
queryClient.invalidateQueries({
queryKey: ["subscriptions", variables.view],
queryKey: ["subscriptions", parseInt(variables.view)],
})
onSuccess?.(variables)
},
})

Expand Down

0 comments on commit b4da0bf

Please sign in to comment.