Skip to content

Commit f60524c

Browse files
committed
fix: toggle website privacy
1 parent a3548f8 commit f60524c

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

apps/dashboard/app/(main)/websites/[id]/settings/privacy/page.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,33 @@ import {
66
InfoIcon,
77
ShareIcon,
88
} from "@phosphor-icons/react";
9+
import { useMutation, useQueryClient } from "@tanstack/react-query";
910
import { useParams } from "next/navigation";
1011
import { useCallback } from "react";
1112
import { toast } from "sonner";
1213
import { Alert, AlertDescription } from "@/components/ui/alert";
1314
import { Badge } from "@/components/ui/badge";
1415
import { Button } from "@/components/ui/button";
1516
import { Switch } from "@/components/ui/switch";
16-
import { useTogglePublicWebsite, useWebsite } from "@/hooks/use-websites";
17+
import {
18+
updateWebsiteCache,
19+
useWebsite,
20+
type Website,
21+
} from "@/hooks/use-websites";
22+
import { orpc } from "@/lib/orpc";
1723

1824
export default function PrivacyPage() {
1925
const params = useParams();
2026
const websiteId = params.id as string;
21-
const { data: websiteData, refetch } = useWebsite(websiteId);
22-
const toggleMutation = useTogglePublicWebsite();
27+
const { data: websiteData } = useWebsite(websiteId);
28+
const queryClient = useQueryClient();
29+
30+
const toggleMutation = useMutation({
31+
...orpc.websites.togglePublic.mutationOptions(),
32+
onSuccess: (updatedWebsite: Website) => {
33+
updateWebsiteCache(queryClient, updatedWebsite);
34+
},
35+
});
2336

2437
const isPublic = websiteData?.isPublic ?? false;
2538
const shareableLink = websiteData
@@ -39,8 +52,7 @@ export default function PrivacyPage() {
3952
error: "Failed to update privacy settings",
4053
}
4154
);
42-
refetch();
43-
}, [websiteData, websiteId, isPublic, toggleMutation, refetch]);
55+
}, [websiteData, websiteId, isPublic, toggleMutation]);
4456

4557
const handleCopyLink = useCallback(() => {
4658
if (!shareableLink) {

apps/dashboard/hooks/use-websites.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ import type { QueryKey } from "@tanstack/react-query";
77
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
88
import { orpc } from "@/lib/orpc";
99

10-
type Website = InferSelectModel<typeof websites>;
11-
type WebsitesListData = {
10+
export type Website = InferSelectModel<typeof websites>;
11+
export type WebsitesListData = {
1212
websites: Website[];
1313
chartData: Record<string, ProcessedMiniChartData>;
1414
};
1515

16-
const getWebsiteByIdKey = (id: string): QueryKey =>
16+
export const getWebsiteByIdKey = (id: string): QueryKey =>
1717
orpc.websites.getById.queryOptions({ input: { id } }).queryKey;
1818

19-
const getWebsitesListKey = (organizationId?: string): QueryKey =>
19+
export const getWebsitesListKey = (organizationId?: string): QueryKey =>
2020
orpc.websites.listWithCharts.queryOptions({
2121
input: { organizationId },
2222
}).queryKey;
2323

24-
const updateWebsiteInList = (
24+
export const updateWebsiteInList = (
2525
old: WebsitesListData | undefined,
2626
updatedWebsite: Website
2727
): WebsitesListData | undefined => {
@@ -118,7 +118,7 @@ export function useCreateWebsite() {
118118
});
119119
}
120120

121-
const updateWebsiteCache = (
121+
export const updateWebsiteCache = (
122122
queryClient: ReturnType<typeof useQueryClient>,
123123
updatedWebsite: Website
124124
) => {
@@ -129,9 +129,6 @@ const updateWebsiteCache = (
129129
updateWebsiteInList(old, updatedWebsite)
130130
);
131131
queryClient.setQueryData(getByIdKey, updatedWebsite);
132-
133-
queryClient.invalidateQueries({ queryKey: getByIdKey });
134-
queryClient.invalidateQueries({ queryKey: listKey });
135132
};
136133

137134
export function useUpdateWebsite() {
@@ -147,19 +144,6 @@ export function useUpdateWebsite() {
147144
});
148145
}
149146

150-
export function useTogglePublicWebsite() {
151-
const queryClient = useQueryClient();
152-
return useMutation({
153-
...orpc.websites.togglePublic.mutationOptions(),
154-
onSuccess: (updatedWebsite: Website) => {
155-
updateWebsiteCache(queryClient, updatedWebsite);
156-
},
157-
onError: (error) => {
158-
console.error("Failed to toggle website privacy:", error);
159-
},
160-
});
161-
}
162-
163147
export function useDeleteWebsite() {
164148
const queryClient = useQueryClient();
165149

0 commit comments

Comments
 (0)