diff --git a/components/dashboard/ChatList.tsx b/components/dashboard/ChatList.tsx index dc800b7..290d1be 100644 --- a/components/dashboard/ChatList.tsx +++ b/components/dashboard/ChatList.tsx @@ -9,6 +9,7 @@ import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { formatDistanceToNow } from "date-fns"; import axios from "axios"; import { toast } from "sonner"; +import { getCookie } from "@/lib/client-utils"; interface Chat { _id: string; @@ -36,12 +37,12 @@ export default function ChatList() { useEffect(() => { const fetchVendorData = async () => { try { - // Get vendor info from session storage or context - const vendorId = sessionStorage.getItem("vendorId"); + // Get vendor info from cookies + const vendorId = getCookie("vendorId"); if (!vendorId) { toast.error("You need to be logged in to view chats"); - router.push("/login"); + router.push("/auth/login"); return; } @@ -68,7 +69,7 @@ export default function ChatList() { setLoading(true); try { - const vendorId = sessionStorage.getItem("vendorId"); + const vendorId = getCookie("vendorId"); // Fetch chats const chatsResponse = await axios.get(`/api/chats/vendor/${vendorId}`); diff --git a/components/dashboard/ChatNotifications.tsx b/components/dashboard/ChatNotifications.tsx index c725bd5..1e303d9 100644 --- a/components/dashboard/ChatNotifications.tsx +++ b/components/dashboard/ChatNotifications.tsx @@ -12,6 +12,7 @@ import { DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import axios from "axios"; +import { getCookie } from "@/lib/client-utils"; interface UnreadCounts { totalUnread: number; @@ -28,7 +29,7 @@ export default function ChatNotifications() { useEffect(() => { const fetchUnreadCounts = async () => { try { - const vendorId = sessionStorage.getItem("vendorId"); + const vendorId = getCookie("vendorId"); if (!vendorId) return; diff --git a/components/dashboard/NewChatForm.tsx b/components/dashboard/NewChatForm.tsx index c29e0c1..0704a35 100644 --- a/components/dashboard/NewChatForm.tsx +++ b/components/dashboard/NewChatForm.tsx @@ -10,6 +10,7 @@ import { Textarea } from "@/components/ui/textarea"; import { ArrowLeft, Send, RefreshCw } from "lucide-react"; import axios from "axios"; import { toast } from "sonner"; +import { getCookie } from "@/lib/client-utils"; export default function NewChatForm() { const router = useRouter(); @@ -23,7 +24,7 @@ export default function NewChatForm() { useEffect(() => { const fetchVendorStores = async () => { try { - const vendorId = sessionStorage.getItem("vendorId"); + const vendorId = getCookie("vendorId"); if (!vendorId) { toast.error("You need to be logged in"); diff --git a/lib/client-utils.ts b/lib/client-utils.ts index e5a6313..bb39467 100644 --- a/lib/client-utils.ts +++ b/lib/client-utils.ts @@ -26,4 +26,14 @@ export async function clientFetch(url: string, options: RequestInit = {}): Promi console.error(`Client fetch error at ${url}:`, error); throw error; } +} + +/** + * Get a cookie value by name + */ +export function getCookie(name: string): string | undefined { + return document.cookie + .split('; ') + .find(row => row.startsWith(`${name}=`)) + ?.split('=')[1]; } \ No newline at end of file