Skip to content

Commit e09b019

Browse files
committed
Store disableAds in convex
1 parent ccc6759 commit e09b019

3 files changed

Lines changed: 35 additions & 83 deletions

File tree

convex/users.ts

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { v } from 'convex/values'
22
import { mutation, query, QueryCtx } from './_generated/server'
33
import { Capability, CapabilitySchema } from './schema'
44
import { getCurrentUserConvex } from './auth'
5+
import { Id } from './_generated/dataModel'
56

67
export const updateUserCapabilities = mutation({
78
args: {
@@ -82,53 +83,19 @@ async function requireCapability(ctx: QueryCtx, capability: Capability) {
8283
}
8384

8485
// Toggle ad preference (only for users with disableAds capability)
85-
export const toggleAdPreference = mutation({
86-
args: {},
87-
handler: async (ctx) => {
88-
const currentUser = await getCurrentUserConvex(ctx)
89-
if (!currentUser) {
90-
throw new Error('Not authenticated')
91-
}
92-
93-
// Check if user has capability to disable ads
94-
if (!currentUser.capabilities.includes('disableAds')) {
95-
throw new Error('User does not have permission to disable ads')
96-
}
97-
98-
const currentAdsDisabled = currentUser.adsDisabled ?? false
99-
100-
await ctx.db.patch(currentUser._id, {
101-
adsDisabled: !currentAdsDisabled,
102-
})
103-
104-
return {
105-
adsDisabled: !currentAdsDisabled,
106-
}
107-
},
108-
})
109-
110-
// Set ad preference (only for users with disableAds capability)
111-
export const setAdPreference = mutation({
86+
export const updateAdPreference = mutation({
11287
args: {
11388
adsDisabled: v.boolean(),
11489
},
11590
handler: async (ctx, args) => {
116-
const currentUser = await getCurrentUserConvex(ctx)
117-
if (!currentUser) {
118-
throw new Error('Not authenticated')
119-
}
120-
121-
// Check if user has capability to disable ads
122-
if (!currentUser.capabilities.includes('disableAds')) {
123-
throw new Error('User does not have permission to disable ads')
124-
}
91+
// Validate admin capability
92+
const { currentUser } = await requireCapability(ctx, 'disableAds')
12593

126-
await ctx.db.patch(currentUser._id, {
94+
// Update target user's capabilities
95+
await ctx.db.patch(currentUser.userId as Id<'users'>, {
12796
adsDisabled: args.adsDisabled,
12897
})
12998

130-
return {
131-
adsDisabled: args.adsDisabled,
132-
}
99+
return { success: true }
133100
},
134101
})

src/hooks/useAdPreference.ts

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,19 @@
1-
import { useConvexMutation } from '@convex-dev/react-query'
2-
import { useQueryClient } from '@tanstack/react-query'
3-
import { api } from 'convex/_generated/api'
41
import { useCurrentUserQuery } from './useCurrentUser'
52

6-
export function useToggleAdPreference() {
7-
const queryClient = useQueryClient()
8-
9-
return useConvexMutation(api.users.toggleAdPreference, {
10-
onSuccess: () => {
11-
// Invalidate current user query to refresh the ad preference
12-
queryClient.invalidateQueries({
13-
queryKey: ['convex', api.auth.getCurrentUser, {}],
14-
})
15-
},
16-
})
17-
}
18-
19-
export function useSetAdPreference() {
20-
const queryClient = useQueryClient()
21-
22-
return useConvexMutation(api.users.setAdPreference, {
23-
onSuccess: () => {
24-
// Invalidate current user query to refresh the ad preference
25-
queryClient.invalidateQueries({
26-
queryKey: ['convex', api.auth.getCurrentUser, {}],
27-
})
28-
},
29-
})
30-
}
31-
323
// Legacy hook for backward compatibility - now uses current user query
334
export function useAdsPreference() {
345
const userQuery = useCurrentUserQuery()
35-
6+
367
if (userQuery.isLoading || !userQuery.data) {
378
return { adsEnabled: true } // Default to showing ads while loading or not authenticated
389
}
39-
10+
4011
const user = userQuery.data
4112
const adsDisabled = user.adsDisabled ?? false
4213
const canDisableAds = user.capabilities.includes('disableAds')
43-
14+
4415
// Ads are enabled if user can't disable them OR if they haven't disabled them
4516
const adsEnabled = !canDisableAds || !adsDisabled
46-
17+
4718
return { adsEnabled }
48-
}
19+
}

src/routes/_libraries/account.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { useToggleAdPreference } from '~/hooks/useAdPreference'
21
import { FaSignOutAlt } from 'react-icons/fa'
3-
import { Authenticated, Unauthenticated } from 'convex/react'
2+
import { Authenticated, Unauthenticated, useMutation } from 'convex/react'
43
import { Link, redirect } from '@tanstack/react-router'
54
import { authClient } from '~/utils/auth.client'
65
import { useCurrentUserQuery } from '~/hooks/useCurrentUser'
6+
import { api } from 'convex/_generated/api'
77

88
export const Route = createFileRoute({
99
component: AccountPage,
@@ -12,14 +12,28 @@ export const Route = createFileRoute({
1212
function UserSettings() {
1313
const userQuery = useCurrentUserQuery()
1414
// Use current user query directly instead of separate ad preference query
15-
const toggleAdPreferenceMutation = useToggleAdPreference()
16-
15+
const updateAdPreferenceMutation = useMutation(
16+
api.users.updateAdPreference
17+
).withOptimisticUpdate((localStore, args) => {
18+
const { adsDisabled } = args
19+
const currentValue = localStore.getQuery(api.auth.getCurrentUser)
20+
if (currentValue !== undefined) {
21+
localStore.setQuery(api.auth.getCurrentUser, {}, {
22+
...currentValue,
23+
adsDisabled: adsDisabled,
24+
} as any)
25+
}
26+
})
27+
1728
// Get values directly from the current user data
1829
const adsDisabled = userQuery.data?.adsDisabled ?? false
19-
const canDisableAds = userQuery.data?.capabilities.includes('disableAds') ?? false
20-
21-
const handleToggleAds = () => {
22-
toggleAdPreferenceMutation.mutate()
30+
const canDisableAds =
31+
userQuery.data?.capabilities.includes('disableAds') ?? false
32+
33+
const handleToggleAds = (e: React.ChangeEvent<HTMLInputElement>) => {
34+
updateAdPreferenceMutation({
35+
adsDisabled: e.target.checked,
36+
})
2337
}
2438

2539
const signOut = async () => {
@@ -60,7 +74,7 @@ function UserSettings() {
6074
className="h-4 w-4 accent-blue-600 my-1"
6175
checked={adsDisabled}
6276
onChange={handleToggleAds}
63-
disabled={userQuery.isLoading || toggleAdPreferenceMutation.isPending}
77+
disabled={userQuery.isLoading}
6478
aria-label="Disable Ads"
6579
/>
6680
<div>

0 commit comments

Comments
 (0)