|
1 | | -import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; |
2 | | -import { apiRequest } from "../../lib/api/client"; |
3 | | - |
4 | | -interface ProviderLink { |
5 | | - provider: string; |
6 | | - connectedAt: string; |
7 | | - updateReason?: string; |
8 | | - hasToken: boolean; |
9 | | -} |
| 1 | +import { useState } from "react"; |
| 2 | +import { useProviderLinks } from "../../lib/api/queries"; |
| 3 | +import { useDisconnectProvider, useResyncProvider } from "../../lib/api/mutations"; |
| 4 | +import { useToast } from "../../lib/hooks/useToast"; |
| 5 | +import { ConfirmDialog } from "../ui/ConfirmDialog"; |
10 | 6 |
|
11 | 7 | export function ConnectedAccountsSection() { |
12 | | - const queryClient = useQueryClient(); |
13 | | - |
14 | | - const { data: providerLinks, isLoading } = useQuery({ |
15 | | - queryKey: ["providerLinks"], |
16 | | - queryFn: () => apiRequest<ProviderLink[]>("/providers/links"), |
17 | | - }); |
| 8 | + const { data: providerLinks, isLoading } = useProviderLinks(); |
| 9 | + const { showToast } = useToast(); |
| 10 | + const [disconnectProvider, setDisconnectProvider] = useState<{ id: string; name: string } | null>(null); |
18 | 11 |
|
19 | | - const disconnectMutation = useMutation({ |
20 | | - mutationFn: (provider: string) => apiRequest(`/providers/${provider}`, { method: "DELETE" }), |
21 | | - onSuccess: () => { |
22 | | - queryClient.invalidateQueries({ queryKey: ["providerLinks"] }); |
23 | | - }, |
24 | | - }); |
25 | | - |
26 | | - const resyncMutation = useMutation({ |
27 | | - mutationFn: (provider: string) => apiRequest(`/providers/${provider}/resync`, { method: "POST" }), |
28 | | - }); |
| 12 | + const disconnectMutation = useDisconnectProvider(); |
| 13 | + const resyncMutation = useResyncProvider(); |
29 | 14 |
|
30 | 15 | const providers = [ |
31 | 16 | { |
@@ -54,61 +39,112 @@ export function ConnectedAccountsSection() { |
54 | 39 | const connectedProviders = new Set(providerLinks?.map((link) => link.provider) || []); |
55 | 40 |
|
56 | 41 | return ( |
57 | | - <div className="p-6"> |
58 | | - <h2 className="text-xl font-semibold mb-4">Connected Accounts</h2> |
| 42 | + <> |
| 43 | + <div className="p-6"> |
| 44 | + <h2 className="text-xl font-semibold mb-4">Connected Accounts</h2> |
59 | 45 |
|
60 | | - <div className="space-y-4"> |
61 | | - {providers.map((provider) => { |
62 | | - const isConnected = connectedProviders.has(provider.id); |
63 | | - const providerLink = providerLinks?.find((link) => link.provider === provider.id); |
| 46 | + <div className="space-y-4"> |
| 47 | + {providers.map((provider) => { |
| 48 | + const isConnected = connectedProviders.has(provider.id); |
| 49 | + const providerLink = providerLinks?.find((link) => link.provider === provider.id); |
64 | 50 |
|
65 | | - return ( |
66 | | - <div key={provider.id} className="flex items-center justify-between p-4 border border-gray-200 rounded-lg"> |
67 | | - <div className="flex items-center space-x-4"> |
68 | | - <div className="text-2xl">{provider.icon}</div> |
69 | | - <div> |
70 | | - <h3 className="font-medium text-gray-900">{provider.name}</h3> |
71 | | - <p className="text-sm text-gray-600">{provider.description}</p> |
72 | | - {isConnected && providerLink && ( |
73 | | - <p className="text-xs text-gray-500 mt-1">Connected {new Date(providerLink.connectedAt).toLocaleDateString()}</p> |
74 | | - )} |
| 51 | + return ( |
| 52 | + <div key={provider.id} className="flex flex-col sm:flex-row sm:items-center sm:justify-between p-4 border border-gray-200 rounded-lg space-y-3 sm:space-y-0"> |
| 53 | + <div className="flex items-center space-x-4"> |
| 54 | + <div className="text-2xl">{provider.icon}</div> |
| 55 | + <div> |
| 56 | + <h3 className="font-medium text-gray-900">{provider.name}</h3> |
| 57 | + <p className="text-sm text-gray-600">{provider.description}</p> |
| 58 | + {isConnected && providerLink && ( |
| 59 | + <p className="text-xs text-gray-500 mt-1">Connected {new Date(providerLink.connectedAt).toLocaleDateString()}</p> |
| 60 | + )} |
| 61 | + </div> |
75 | 62 | </div> |
76 | | - </div> |
77 | 63 |
|
78 | | - <div className="flex items-center space-x-2"> |
79 | | - {isConnected ? ( |
80 | | - <> |
81 | | - <button |
82 | | - type="button" |
83 | | - onClick={() => resyncMutation.mutate(provider.id)} |
84 | | - disabled={resyncMutation.isPending} |
85 | | - className="px-3 py-1 text-sm font-medium text-blue-600 hover:text-blue-700 disabled:text-gray-400" |
86 | | - > |
87 | | - {resyncMutation.isPending ? "Syncing..." : "Resync"} |
88 | | - </button> |
89 | | - <button |
90 | | - type="button" |
91 | | - onClick={() => { |
92 | | - if (confirm(`Are you sure you want to disconnect ${provider.name}? This will remove all weight data from this provider.`)) { |
93 | | - disconnectMutation.mutate(provider.id); |
94 | | - } |
95 | | - }} |
96 | | - disabled={disconnectMutation.isPending} |
97 | | - className="px-3 py-1 text-sm font-medium text-red-600 hover:text-red-700 disabled:text-gray-400" |
98 | | - > |
99 | | - {disconnectMutation.isPending ? "Disconnecting..." : "Disconnect"} |
100 | | - </button> |
101 | | - </> |
102 | | - ) : ( |
103 | | - <a href={`/link?provider=${provider.id}`} className="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-md hover:bg-blue-700"> |
104 | | - Connect |
105 | | - </a> |
106 | | - )} |
| 64 | + <div className="flex items-center space-x-2 self-end sm:self-auto"> |
| 65 | + {isConnected ? ( |
| 66 | + <> |
| 67 | + <button |
| 68 | + type="button" |
| 69 | + onClick={() => { |
| 70 | + resyncMutation.mutate(provider.id, { |
| 71 | + onSuccess: () => { |
| 72 | + showToast({ |
| 73 | + title: "Resync Complete", |
| 74 | + description: `${provider.name} data has been resynced successfully.`, |
| 75 | + variant: "success", |
| 76 | + }); |
| 77 | + }, |
| 78 | + onError: () => { |
| 79 | + showToast({ |
| 80 | + title: "Resync Failed", |
| 81 | + description: `Failed to resync ${provider.name} data. Please try again.`, |
| 82 | + variant: "error", |
| 83 | + }); |
| 84 | + }, |
| 85 | + }); |
| 86 | + }} |
| 87 | + disabled={resyncMutation.isPending} |
| 88 | + className="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-md hover:bg-blue-700 disabled:bg-gray-300 disabled:text-gray-500 disabled:cursor-not-allowed transition-colors" |
| 89 | + > |
| 90 | + {resyncMutation.isPending ? "Syncing..." : "Resync"} |
| 91 | + </button> |
| 92 | + <button |
| 93 | + type="button" |
| 94 | + onClick={() => setDisconnectProvider({ id: provider.id, name: provider.name })} |
| 95 | + disabled={disconnectMutation.isPending} |
| 96 | + className="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded-md hover:bg-red-700 disabled:bg-gray-300 disabled:text-gray-500 disabled:cursor-not-allowed transition-colors" |
| 97 | + > |
| 98 | + {disconnectMutation.isPending ? "Disconnecting..." : "Disconnect"} |
| 99 | + </button> |
| 100 | + </> |
| 101 | + ) : ( |
| 102 | + <a href={`/link?provider=${provider.id}`} className="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-md hover:bg-blue-700"> |
| 103 | + Connect |
| 104 | + </a> |
| 105 | + )} |
| 106 | + </div> |
107 | 107 | </div> |
108 | | - </div> |
109 | | - ); |
110 | | - })} |
| 108 | + ); |
| 109 | + })} |
| 110 | + </div> |
111 | 111 | </div> |
112 | | - </div> |
| 112 | + |
| 113 | + <ConfirmDialog |
| 114 | + open={!!disconnectProvider} |
| 115 | + onOpenChange={(open) => !open && setDisconnectProvider(null)} |
| 116 | + title={`Disconnect ${disconnectProvider?.name}?`} |
| 117 | + description={ |
| 118 | + <div className="space-y-2"> |
| 119 | + <p>Are you sure you want to disconnect {disconnectProvider?.name}?</p> |
| 120 | + <p>This will remove all weight data from this provider.</p> |
| 121 | + </div> |
| 122 | + } |
| 123 | + confirmText="Disconnect" |
| 124 | + destructive |
| 125 | + onConfirm={() => { |
| 126 | + if (disconnectProvider) { |
| 127 | + disconnectMutation.mutate(disconnectProvider.id, { |
| 128 | + onSuccess: () => { |
| 129 | + showToast({ |
| 130 | + title: "Disconnected", |
| 131 | + description: `${disconnectProvider.name} has been disconnected successfully.`, |
| 132 | + variant: "success", |
| 133 | + }); |
| 134 | + setDisconnectProvider(null); |
| 135 | + }, |
| 136 | + onError: () => { |
| 137 | + showToast({ |
| 138 | + title: "Disconnect Failed", |
| 139 | + description: `Failed to disconnect ${disconnectProvider.name}. Please try again.`, |
| 140 | + variant: "error", |
| 141 | + }); |
| 142 | + setDisconnectProvider(null); |
| 143 | + }, |
| 144 | + }); |
| 145 | + } |
| 146 | + }} |
| 147 | + /> |
| 148 | + </> |
113 | 149 | ); |
114 | 150 | } |
0 commit comments