|
1 | | -import { useUserSettingsStore } from '~/stores/userSettings' |
2 | 1 | import { FaSignOutAlt } from 'react-icons/fa' |
3 | | -import { Authenticated, Unauthenticated } from 'convex/react' |
| 2 | +import { Authenticated, Unauthenticated, useMutation } from 'convex/react' |
4 | 3 | import { Link, redirect } from '@tanstack/react-router' |
5 | 4 | import { authClient } from '~/utils/auth.client' |
6 | 5 | import { useCurrentUserQuery } from '~/hooks/useCurrentUser' |
| 6 | +import { api } from 'convex/_generated/api' |
7 | 7 |
|
8 | 8 | export const Route = createFileRoute({ |
9 | 9 | component: AccountPage, |
10 | 10 | }) |
11 | 11 |
|
12 | 12 | function UserSettings() { |
13 | 13 | const userQuery = useCurrentUserQuery() |
14 | | - const adsDisabled = useUserSettingsStore((s) => s.settings.adsDisabled) |
15 | | - const toggleAds = useUserSettingsStore((s) => s.toggleAds) |
| 14 | + // Use current user query directly instead of separate ad preference query |
| 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 | + }) |
16 | 27 |
|
17 | | - const canDisableAds = userQuery.data?.capabilities.includes('disableAds') |
| 28 | + // Get values directly from the current user data |
| 29 | + const adsDisabled = userQuery.data?.adsDisabled ?? false |
| 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 | + }) |
| 37 | + } |
18 | 38 |
|
19 | 39 | const signOut = async () => { |
20 | 40 | await authClient.signOut() |
@@ -53,7 +73,7 @@ function UserSettings() { |
53 | 73 | type="checkbox" |
54 | 74 | className="h-4 w-4 accent-blue-600 my-1" |
55 | 75 | checked={adsDisabled} |
56 | | - onChange={toggleAds} |
| 76 | + onChange={handleToggleAds} |
57 | 77 | disabled={userQuery.isLoading} |
58 | 78 | aria-label="Disable Ads" |
59 | 79 | /> |
|
0 commit comments