Skip to content

Commit

Permalink
fix(mobile): refetch user entitlements after purchases (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdev98 committed Sep 7, 2024
1 parent 546ca1b commit 3723407
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion apps/mobile/app/(app)/paywall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { PaywallIllustration } from '@/components/svg-assets/paywall-illustratio
import { Button } from '@/components/ui/button'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Text } from '@/components/ui/text'
import { usePurchasesPackages } from '@/hooks/use-purchases'
import {
usePurchasesPackages,
useUserEntitlements,
} from '@/hooks/use-purchases'
import { cn } from '@/lib/utils'
import { t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
Expand Down Expand Up @@ -118,20 +121,24 @@ export default function PaywallScreen() {
const [plan, setPlan] = useState<'growth' | 'wealth'>('growth')
const [duration, setDuration] = useState<'monthly' | 'annually'>('annually')
const { data } = usePurchasesPackages()
const { refetch } = useUserEntitlements()
const router = useRouter()
const { mutateAsync, isPending } = useMutation({
mutationFn: Purchases.purchasePackage,
onSuccess() {
refetch()
router.back()
toast.success(t(i18n)`Thank you! You have unlocked 6pm Pro!`)
},
onError() {
refetch()
//
},
})
const { mutateAsync: mutateRestore, isPending: isRestoring } = useMutation({
mutationFn: Purchases.restorePurchases,
onSuccess(result) {
refetch()
if (Object.keys(result.entitlements.active).length) {
toast.success(t(i18n)`Purchases restored successfully!`)
router.back()
Expand All @@ -141,6 +148,7 @@ export default function PaywallScreen() {
}
},
onError(error) {
refetch()
toast.error(error.message)
},
})
Expand Down
4 changes: 3 additions & 1 deletion apps/mobile/hooks/use-purchases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export function usePurchasesPackages() {
}

export function useUserEntitlements() {
const { data: customerInfo } = useQuery({
const { data: customerInfo, refetch } = useQuery({
queryKey: ['entitlements'],
queryFn: Purchases.getCustomerInfo,
refetchInterval: 1000 * 60,
})

const isWealth = !!customerInfo?.entitlements.active.wealth?.isActive
Expand All @@ -45,5 +46,6 @@ export function useUserEntitlements() {
isWealth,
isGrowth,
isPro,
refetch,
}
}

0 comments on commit 3723407

Please sign in to comment.