Update queries

This commit is contained in:
Eric Bailey
2024-11-19 14:57:08 -06:00
parent 0f15abc246
commit df9101774c
9 changed files with 427 additions and 4 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
import {create} from 'gretchen'
import {PLUS_SERVICE_URL} from '#/env'
export const api = create({
baseURL: process.env.PLUS_SERVICE_URL,
baseURL: PLUS_SERVICE_URL,
})
@@ -0,0 +1,34 @@
export enum EntitlementId {
Core = 'core',
}
export enum PlatformId {
Android = 'android',
Ios = 'ios',
Web = 'web',
}
export enum SubscriptionGroupId {
Core = 'core',
}
export enum OfferingId {
CoreMonthly = 'coreMonthly',
CoreAnnual = 'coreAnnual',
}
export type Offering =
| {
id: OfferingId
platform: PlatformId.Ios | PlatformId.Android
price: number
package: undefined
}
| {
id: OfferingId
platform: PlatformId.Web
price: number
package: {
priceId: string
}
}
@@ -13,7 +13,7 @@ export function useEntitlements() {
async queryFn() {
const params = new URLSearchParams('/entitlements')
params.set('did', currentAccount!.did)
const url = `/entitlements?${params.toString()}`
const url = `/account?${params.toString()}`
const {data, error} = await api<{
entitlements: {id: 'core'; platform: 'web'}[]
}>(url).json()
@@ -0,0 +1,40 @@
import React from 'react'
import {useQuery, useMutation} from '@tanstack/react-query'
import {isIOS} from '#/platform/detection'
import {OfferingId, PlatformId, Offering, SubscriptionGroupId} from '#/state/purchases/subscriptions/types'
export function useSubscriptionGroup(group: SubscriptionGroupId) {
return useQuery<{ offerings: Offering[] }>({
queryKey: ['subscriptions', 'core'],
queryFn() {
return {
offerings: [
{
id: OfferingId.CoreMonthly,
platform: isIOS ? PlatformId.Ios : PlatformId.Android,
price: 800,
package: undefined,
},
{
id: OfferingId.CoreAnnual,
platform: isIOS ? PlatformId.Ios : PlatformId.Android,
price: 8000,
package: undefined,
},
]
}
}
})
}
export function usePurchaseOffering() {
return useMutation({
async mutationFn({
did,
email,
offering,
}: { did: string, email: string, offering: Offering }) {
}
})
}
@@ -0,0 +1,64 @@
import {Linking} from 'react-native'
import {useQuery, useMutation} from '@tanstack/react-query'
import {IS_DEV} from '#/env'
import {api} from '#/state/purchases/subscriptions/api'
import {OfferingId, PlatformId, Offering, SubscriptionGroupId} from '#/state/purchases/subscriptions/types'
export function useSubscriptionGroup(group: SubscriptionGroupId) {
return useQuery<{ offerings: Offering[] }>({
queryKey: ['subscription-group', group],
async queryFn() {
const { data, error } = await api(`/subscriptions/${group}?platform=web`).json()
if (error || !data) {
throw new Error('Failed to fetch subscription group')
}
const { offerings } = data
return {
offerings: offerings.map((o: any) => ({
id: o.id as OfferingId,
platform: PlatformId.Web,
package: {
priceId: o.productId,
},
})),
}
}
})
}
export function usePurchaseOffering() {
return useMutation({
async mutationFn({
did,
email,
offering,
}: { did: string, email: string, offering: Offering }) {
if (offering.platform !== PlatformId.Web) {
throw new Error('Unsupported platform')
}
const {data, error} = await api<{
checkoutUrl: string
}>('/checkout/create', {
method: 'POST',
json: {
did,
email,
price: offering.package.priceId,
redirectUrl: IS_DEV
? `http://localhost:19006/subscriptions`
: `https://bsky.app/subscriptions`,
},
}).json()
if (error || !data) {
throw error
}
Linking.openURL(data.checkoutUrl)
}
})
}
+12
View File
@@ -0,0 +1,12 @@
import {OfferingId} from '#/state/purchases/subscriptions/types';
export function parseOfferingId(id: string): OfferingId {
switch (id) {
case 'coreMonthly':
return OfferingId.CoreMonthly;
case 'coreAnnual':
return OfferingId.CoreAnnual;
default:
throw new Error(`Unknown offering id: ${id}`);
}
}