Handle subscription
This commit is contained in:
@@ -19,6 +19,8 @@ import {
|
|||||||
} from '#/view/com/util/forms/NativeDropdown'
|
} from '#/view/com/util/forms/NativeDropdown'
|
||||||
import {useSession} from '#/state/session'
|
import {useSession} from '#/state/session'
|
||||||
import {useModalControls} from '#/state/modals'
|
import {useModalControls} from '#/state/modals'
|
||||||
|
import {useModServiceSubscriptionMutation} from '#/state/queries/modservice'
|
||||||
|
import {UsePreferencesQueryResponse} from '#/state/queries/preferences'
|
||||||
|
|
||||||
import {useTheme, atoms as a, tokens, web, native, useBreakpoints} from '#/alf'
|
import {useTheme, atoms as a, tokens, web, native, useBreakpoints} from '#/alf'
|
||||||
import {Text} from '#/components/Typography'
|
import {Text} from '#/components/Typography'
|
||||||
@@ -30,8 +32,10 @@ import {Bars3_Stroke2_Corner0_Rounded as Bars} from '#/components/icons/Bars'
|
|||||||
import {Divider} from '#/components/Divider'
|
import {Divider} from '#/components/Divider'
|
||||||
|
|
||||||
export function Header({
|
export function Header({
|
||||||
|
preferences,
|
||||||
modservice,
|
modservice,
|
||||||
}: {
|
}: {
|
||||||
|
preferences: UsePreferencesQueryResponse
|
||||||
modservice: AppBskyModerationDefs.ModServiceViewDetailed
|
modservice: AppBskyModerationDefs.ModServiceViewDetailed
|
||||||
}) {
|
}) {
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
@@ -40,6 +44,13 @@ export function Header({
|
|||||||
const navigation = useNavigation<NavigationProp>()
|
const navigation = useNavigation<NavigationProp>()
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const canGoBack = navigation.canGoBack()
|
const canGoBack = navigation.canGoBack()
|
||||||
|
const {mutateAsync: service, variables} = useModServiceSubscriptionMutation()
|
||||||
|
const isSubscribed =
|
||||||
|
variables?.subscribe ??
|
||||||
|
preferences.moderationOpts.mods.find(
|
||||||
|
mod => mod.did === modservice.creator.did,
|
||||||
|
)
|
||||||
|
const [subscriptionError, setSubscriptionError] = React.useState<string>('')
|
||||||
|
|
||||||
const onPressBack = React.useCallback(() => {
|
const onPressBack = React.useCallback(() => {
|
||||||
if (navigation.canGoBack()) {
|
if (navigation.canGoBack()) {
|
||||||
@@ -53,8 +64,16 @@ export function Header({
|
|||||||
setDrawerOpen(true)
|
setDrawerOpen(true)
|
||||||
}, [setDrawerOpen])
|
}, [setDrawerOpen])
|
||||||
|
|
||||||
|
const toggleSubscribe = React.useCallback(async () => {
|
||||||
|
try {
|
||||||
|
await service({did: modservice.creator.did, subscribe: !isSubscribed})
|
||||||
|
} catch (e: any) {
|
||||||
|
setSubscriptionError(e.message)
|
||||||
|
}
|
||||||
|
}, [service, isSubscribed, modservice.creator.did])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={[a.pb_xl, web(a.pt_md), gtMobile && web(a.pt_4xl)]}>
|
<View style={[a.pb_xl, web(a.pt_md), gtMobile && web(a.pt_2xl)]}>
|
||||||
{!gtMobile && (
|
{!gtMobile && (
|
||||||
<View style={[a.mb_xl]}>
|
<View style={[a.mb_xl]}>
|
||||||
<View
|
<View
|
||||||
@@ -75,9 +94,14 @@ export function Header({
|
|||||||
size="small"
|
size="small"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
color="primary"
|
color="primary"
|
||||||
label={_(msg`Subscribe to moderation service`)}>
|
label={_(msg`Subscribe to this moderation service`)}
|
||||||
|
onPress={toggleSubscribe}>
|
||||||
<ButtonText>
|
<ButtonText>
|
||||||
<Trans>Subscribe</Trans>
|
{isSubscribed ? (
|
||||||
|
<Trans>Unsubscribe</Trans>
|
||||||
|
) : (
|
||||||
|
<Trans>Subscribe</Trans>
|
||||||
|
)}
|
||||||
</ButtonText>
|
</ButtonText>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
@@ -134,9 +158,14 @@ export function Header({
|
|||||||
size="small"
|
size="small"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
color="primary"
|
color="primary"
|
||||||
label={_(msg`Subscribe to moderation service`)}>
|
label={_(msg`Subscribe to this moderation service`)}
|
||||||
|
onPress={toggleSubscribe}>
|
||||||
<ButtonText>
|
<ButtonText>
|
||||||
<Trans>Subscribe</Trans>
|
{isSubscribed ? (
|
||||||
|
<Trans>Unsubscribe</Trans>
|
||||||
|
) : (
|
||||||
|
<Trans>Subscribe</Trans>
|
||||||
|
)}
|
||||||
</ButtonText>
|
</ButtonText>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
@@ -144,6 +173,30 @@ export function Header({
|
|||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
|
{subscriptionError && (
|
||||||
|
<View style={[a.pt_lg]}>
|
||||||
|
<View
|
||||||
|
style={[
|
||||||
|
a.px_lg,
|
||||||
|
a.py_md,
|
||||||
|
a.rounded_sm,
|
||||||
|
a.border,
|
||||||
|
a.gap_xs,
|
||||||
|
{
|
||||||
|
backgroundColor: t.palette.negative_50,
|
||||||
|
borderColor: t.palette.negative_100,
|
||||||
|
},
|
||||||
|
]}>
|
||||||
|
<Text style={[a.font_bold, {color: t.palette.negative_950}]}>
|
||||||
|
An error occurred while editing your subscription.
|
||||||
|
</Text>
|
||||||
|
<Text style={[{color: t.palette.negative_900}]}>
|
||||||
|
{subscriptionError}
|
||||||
|
</Text>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ function ProfileModservicecreenIntermediate({modDid}: {modDid: string}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ProfileModserviceScreenInner({
|
export function ProfileModserviceScreenInner({
|
||||||
// preferences,
|
preferences,
|
||||||
modservice,
|
modservice,
|
||||||
}: {
|
}: {
|
||||||
preferences: UsePreferencesQueryResponse
|
preferences: UsePreferencesQueryResponse
|
||||||
@@ -186,7 +186,7 @@ export function ProfileModserviceScreenInner({
|
|||||||
borderWidth: 0,
|
borderWidth: 0,
|
||||||
paddingHorizontal: a.px_xl.paddingLeft,
|
paddingHorizontal: a.px_xl.paddingLeft,
|
||||||
}}>
|
}}>
|
||||||
<Header modservice={modservice} />
|
<Header modservice={modservice} preferences={preferences} />
|
||||||
|
|
||||||
<View style={[a.gap_md]}>
|
<View style={[a.gap_md]}>
|
||||||
{modservice.description ? (
|
{modservice.description ? (
|
||||||
|
|||||||
@@ -1,14 +1,35 @@
|
|||||||
import {useQuery} from '@tanstack/react-query'
|
import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
|
||||||
import {getAgent} from '../session'
|
|
||||||
|
|
||||||
export const RQKEY = (did: string) => ['mod-service-info', did]
|
import {getAgent} from '#/state/session'
|
||||||
|
import {preferencesQueryKey} from '#/state/queries/preferences'
|
||||||
|
|
||||||
|
export const modServiceInfoQueryKey = (did: string) => ['mod-service-info', did]
|
||||||
|
|
||||||
export function useModServiceInfoQuery({did}: {did: string}) {
|
export function useModServiceInfoQuery({did}: {did: string}) {
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: RQKEY(did),
|
queryKey: modServiceInfoQueryKey(did),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const res = await getAgent().app.bsky.moderation.getService({did})
|
const res = await getAgent().app.bsky.moderation.getService({did})
|
||||||
return res.data
|
return res.data
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useModServiceSubscriptionMutation() {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
|
return useMutation({
|
||||||
|
async mutationFn({did, subscribe}: {did: string; subscribe: boolean}) {
|
||||||
|
if (subscribe) {
|
||||||
|
await getAgent().addModService(did)
|
||||||
|
} else {
|
||||||
|
await getAgent().removeModService(did)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onSuccess() {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: preferencesQueryKey,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user