This commit is contained in:
NotII
2025-03-03 21:13:07 +00:00
parent 5260978cb8
commit f5382d82f6
4 changed files with 125 additions and 18 deletions

View File

@@ -11,6 +11,7 @@ import { formatDistanceToNow } from "date-fns";
import axios from "axios"; import axios from "axios";
import { toast } from "sonner"; import { toast } from "sonner";
import { ArrowLeft, Send, RefreshCw } from "lucide-react"; import { ArrowLeft, Send, RefreshCw } from "lucide-react";
import { getCookie } from "@/lib/client-utils";
interface Message { interface Message {
_id: string; _id: string;
@@ -44,7 +45,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
// Fetch chat data // Fetch chat data
const fetchChat = async () => { const fetchChat = async () => {
try { try {
const response = await axios.get(`/api/chats/${chatId}`); // Get auth token from cookies
const authToken = getCookie("Authorization");
if (!authToken) {
toast.error("You need to be logged in");
router.push("/auth/login");
return;
}
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
const response = await authAxios.get(`/chats/${chatId}`);
setChat(response.data); setChat(response.data);
setLoading(false); setLoading(false);
} catch (error) { } catch (error) {
@@ -76,7 +94,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
setSending(true); setSending(true);
try { try {
await axios.post(`/api/chats/${chatId}/message`, { // Get auth token from cookies
const authToken = getCookie("Authorization");
if (!authToken) {
toast.error("You need to be logged in");
router.push("/auth/login");
return;
}
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
await authAxios.post(`/chats/${chatId}/message`, {
content: message content: message
}); });

View File

@@ -37,17 +37,30 @@ export default function ChatList() {
useEffect(() => { useEffect(() => {
const fetchVendorData = async () => { const fetchVendorData = async () => {
try { try {
// Get vendor info from cookies // Get auth token from cookies
const vendorId = getCookie("vendorId"); const authToken = getCookie("Authorization");
if (!vendorId) { if (!authToken) {
toast.error("You need to be logged in to view chats"); toast.error("You need to be logged in to view chats");
router.push("/auth/login"); router.push("/auth/login");
return; return;
} }
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
// Get vendor ID from token (assuming JWT with vendorId in the payload)
// If you can't extract from token, you might need a profile endpoint
// For now, we'll use a placeholder ID until proper JWT decoding is implemented
const vendorId = "current"; // Replace with actual ID extraction
// Fetch vendor's stores // Fetch vendor's stores
const storesResponse = await axios.get(`/api/stores/vendor/${vendorId}`); const storesResponse = await authAxios.get(`/stores/vendor/${vendorId}`);
setVendorStores(storesResponse.data); setVendorStores(storesResponse.data);
if (storesResponse.data.length > 0) { if (storesResponse.data.length > 0) {
@@ -69,10 +82,28 @@ export default function ChatList() {
setLoading(true); setLoading(true);
try { try {
const vendorId = getCookie("vendorId"); // Get auth token from cookies
const authToken = getCookie("Authorization");
if (!authToken) {
toast.error("You need to be logged in");
router.push("/auth/login");
return;
}
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
// Get vendor ID (as above, assuming we have a way to get it)
const vendorId = "current"; // Replace with actual ID extraction
// Fetch chats // Fetch chats
const chatsResponse = await axios.get(`/api/chats/vendor/${vendorId}`); const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`);
// Filter chats by selected store // Filter chats by selected store
const filteredChats = chatsResponse.data.filter( const filteredChats = chatsResponse.data.filter(
@@ -82,7 +113,7 @@ export default function ChatList() {
setChats(filteredChats); setChats(filteredChats);
// Fetch unread counts // Fetch unread counts
const unreadResponse = await axios.get(`/api/chats/vendor/${vendorId}/unread`); const unreadResponse = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
setUnreadCounts(unreadResponse.data); setUnreadCounts(unreadResponse.data);
} catch (error) { } catch (error) {
console.error("Error fetching chats:", error); console.error("Error fetching chats:", error);

View File

@@ -29,11 +29,23 @@ export default function ChatNotifications() {
useEffect(() => { useEffect(() => {
const fetchUnreadCounts = async () => { const fetchUnreadCounts = async () => {
try { try {
const vendorId = getCookie("vendorId"); // Get auth token from cookies
const authToken = getCookie("Authorization");
if (!vendorId) return; if (!authToken) return;
const response = await axios.get(`/api/chats/vendor/${vendorId}/unread`); // Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
// Get vendor ID (placeholder until proper JWT decoding)
const vendorId = "current"; // Replace with actual ID extraction
const response = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
setUnreadCounts(response.data); setUnreadCounts(response.data);
// If there are unread messages, fetch chat metadata // If there are unread messages, fetch chat metadata
@@ -48,7 +60,7 @@ export default function ChatNotifications() {
await Promise.all( await Promise.all(
chatIds.map(async (chatId) => { chatIds.map(async (chatId) => {
try { try {
const chatResponse = await axios.get(`/api/chats/${chatId}`); const chatResponse = await authAxios.get(`/chats/${chatId}`);
metadata[chatId] = { metadata[chatId] = {
buyerId: chatResponse.data.buyerId, buyerId: chatResponse.data.buyerId,
}; };

View File

@@ -24,15 +24,27 @@ export default function NewChatForm() {
useEffect(() => { useEffect(() => {
const fetchVendorStores = async () => { const fetchVendorStores = async () => {
try { try {
const vendorId = getCookie("vendorId"); // Get auth token from cookies
const authToken = getCookie("Authorization");
if (!vendorId) { if (!authToken) {
toast.error("You need to be logged in"); toast.error("You need to be logged in");
router.push("/login"); router.push("/auth/login");
return; return;
} }
const response = await axios.get(`/api/stores/vendor/${vendorId}`); // Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
// Get vendor ID (placeholder until proper JWT decoding)
const vendorId = "current"; // Replace with actual ID extraction
const response = await authAxios.get(`/stores/vendor/${vendorId}`);
setVendorStores(response.data); setVendorStores(response.data);
if (response.data.length > 0) { if (response.data.length > 0) {
@@ -61,7 +73,24 @@ export default function NewChatForm() {
setLoading(true); setLoading(true);
try { try {
const response = await axios.post("/api/chats/create", { // Get auth token from cookies
const authToken = getCookie("Authorization");
if (!authToken) {
toast.error("You need to be logged in");
router.push("/auth/login");
return;
}
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
const response = await authAxios.post("/chats/create", {
buyerId, buyerId,
storeId: selectedStore, storeId: selectedStore,
initialMessage initialMessage