Show state, add mgmt url

This commit is contained in:
Eric Bailey
2024-11-19 20:02:36 -06:00
parent 5e4bf58811
commit 4db49edca6
6 changed files with 141 additions and 14 deletions
@@ -34,3 +34,12 @@ export type Offering =
priceId: string
}
}
export type Subscription = {
group: SubscriptionGroupId
platform: PlatformId
status: string
startsAt: number
endsAt: number
renewalStatus: string
}
@@ -0,0 +1,17 @@
import {Linking} from 'react-native'
import {useMutation} from '@tanstack/react-query'
import Purchases from 'react-native-purchases'
export function useManageSubscription() {
return useMutation({
async mutationFn() {
const customer = await Purchases.getCustomerInfo()
if (customer.managementURL) {
Linking.openURL(customer.managementURL)
} else {
// TODO
}
},
})
}
@@ -0,0 +1,30 @@
import {Linking} from 'react-native'
import {useMutation} from '@tanstack/react-query'
import {IS_DEV} from '#/env'
import {useSession} from '#/state/session'
import {api} from '#/state/purchases/subscriptions/api'
export function useManageSubscription() {
const {currentAccount} = useSession()
return useMutation({
async mutationFn() {
const {data, error} = await api<{
managementUrl: string
}>('/account/create-management-url', {
method: 'POST',
json: {
did: currentAccount!.did,
redirecturl: IS_DEV ? 'http://localhost:19006/subscriptions' : 'https://bsky.app/subscriptions',
}
}).json()
if (error || !data) {
throw new Error('Failed to fetch subscriptions state')
}
Linking.openURL(data.managementUrl)
},
})
}
@@ -0,0 +1,28 @@
import {useQuery} from '@tanstack/react-query'
import {useSession} from '#/state/session'
import {api} from '#/state/purchases/subscriptions/api'
import {SubscriptionGroupId, PlatformId, Subscription} from '#/state/purchases/subscriptions/types'
export function useSubscriptionsState() {
const {currentAccount} = useSession()
return useQuery({
enabled: !!currentAccount,
queryKey: ['subscriptions-state', currentAccount?.did],
refetchOnWindowFocus: true,
async queryFn() {
const url = `/account?did=${currentAccount!.did}`
const {data, error} = await api<{
subscriptions: Subscription[]
entitlements: {id: 'core'; platform: 'web'}[]
}>(url).json()
if (error || !data) {
throw new Error('Failed to fetch subscriptions state')
}
return data
},
})
}