-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrough.tsx
More file actions
197 lines (175 loc) · 5.7 KB
/
rough.tsx
File metadata and controls
197 lines (175 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use client';
import { MessageCard } from '@/components/MessageCard';
import { Button } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { Switch } from '@/components/ui/switch';
import { useToast } from '@/components/ui/use-toast';
import { Message } from '@/model/User';
import { ApiResponse } from '@/types/ApiResponse';
import { zodResolver } from '@hookform/resolvers/zod';
import axios, { AxiosError } from 'axios';
import { Loader2, RefreshCcw } from 'lucide-react';
import { User } from 'next-auth';
import { useSession } from 'next-auth/react';
import React, { useCallback, useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { AcceptMessageSchema } from '@/schemas/acceptMessageSchema';
function UserDashboard() {
const [messages, setMessages] = useState<Message[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isSwitchLoading, setIsSwitchLoading] = useState(false);
const { toast } = useToast();
const handleDeleteMessage = (messageId: string) => {
setMessages(messages.filter((message) => message._id !== messageId));
};
const { data: session } = useSession();
const form = useForm({
resolver: zodResolver(AcceptMessageSchema),
});
const { register, watch, setValue } = form;
const acceptMessages = watch('acceptMessages');
const fetchAcceptMessages = useCallback(async () => {
setIsSwitchLoading(true);
try {
const response = await axios.get<ApiResponse>('/api/accept-messages');
setValue('acceptMessages', response.data.isAcceptingMessages);
} catch (error) {
const axiosError = error as AxiosError<ApiResponse>;
toast({
title: 'Error',
description:
axiosError.response?.data.message ??
'Failed to fetch message settings',
variant: 'destructive',
});
} finally {
setIsSwitchLoading(false);
}
}, [setValue, toast]);
const fetchMessages = useCallback(
async (refresh: boolean = false) => {
setIsLoading(true);
setIsSwitchLoading(false);
try {
const response = await axios.get<ApiResponse>('/api/get-messages');
setMessages(response.data.messages || []);
if (refresh) {
toast({
title: 'Refreshed Messages',
description: 'Showing latest messages',
});
}
} catch (error) {
const axiosError = error as AxiosError<ApiResponse>;
toast({
title: 'Error',
description:
axiosError.response?.data.message ?? 'Failed to fetch messages',
variant: 'destructive',
});
} finally {
setIsLoading(false);
setIsSwitchLoading(false);
}
},
[setIsLoading, setMessages, toast]
);
// Fetch initial state from the server
useEffect(() => {
if (!session || !session.user) return;
fetchMessages();
fetchAcceptMessages();
}, [session, setValue, toast, fetchAcceptMessages, fetchMessages]);
// Handle switch change
const handleSwitchChange = async () => {
try {
const response = await axios.post<ApiResponse>('/api/accept-messages', {
acceptMessages: !acceptMessages,
});
setValue('acceptMessages', !acceptMessages);
toast({
title: response.data.message,
variant: 'default',
});
} catch (error) {
const axiosError = error as AxiosError<ApiResponse>;
toast({
title: 'Error',
description:
axiosError.response?.data.message ??
'Failed to update message settings',
variant: 'destructive',
});
}
};
if (!session || !session.user) {
return <div></div>;
}
const { username } = session.user as User;
const baseUrl = `${window.location.protocol}//${window.location.host}`;
const profileUrl = `${baseUrl}/u/${username}`;
const copyToClipboard = () => {
navigator.clipboard.writeText(profileUrl);
toast({
title: 'URL Copied!',
description: 'Profile URL has been copied to clipboard.',
});
};
return (
<div className="my-8 mx-4 md:mx-8 lg:mx-auto p-6 bg-white rounded w-full max-w-6xl">
<h1 className="text-4xl font-bold mb-4">User Dashboard</h1>
<div className="mb-4">
<h2 className="text-lg font-semibold mb-2">Copy Your Unique Link</h2>{' '}
<div className="flex items-center">
<input
type="text"
value={profileUrl}
disabled
className="input input-bordered w-full p-2 mr-2"
/>
<Button onClick={copyToClipboard}>Copy</Button>
</div>
</div>
<div className="mb-4">
<Switch
{...register('acceptMessages')}
checked={acceptMessages}
onCheckedChange={handleSwitchChange}
disabled={isSwitchLoading}
/>
<span className="ml-2">
Accept Messages: {acceptMessages ? 'On' : 'Off'}
</span>
</div>
<Separator />
<Button
className="mt-4"
variant="outline"
onClick={(e) => {
e.preventDefault();
fetchMessages(true);
}}
>
{isLoading ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<RefreshCcw className="h-4 w-4" />
)}
</Button>
<div className="mt-4 grid grid-cols-1 md:grid-cols-2 gap-6">
{messages.length > 0 ? (
messages.map((message, index) => (
<MessageCard
key={message._id as string}
message={message}
onMessageDelete={handleDeleteMessage}
/>
))
) : (
<p>No messages to display.</p>
)}
</div>
</div>
);
}
export default UserDashboard;