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 { toast } from "sonner";
import { ArrowLeft, Send, RefreshCw } from "lucide-react";
import { getCookie } from "@/lib/client-utils";
interface Message {
_id: string;
@@ -44,7 +45,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
// Fetch chat data
const fetchChat = async () => {
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);
setLoading(false);
} catch (error) {
@@ -76,7 +94,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
setSending(true);
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
});

View File

@@ -37,17 +37,30 @@ export default function ChatList() {
useEffect(() => {
const fetchVendorData = async () => {
try {
// Get vendor info from cookies
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 to view chats");
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 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
const storesResponse = await axios.get(`/api/stores/vendor/${vendorId}`);
const storesResponse = await authAxios.get(`/stores/vendor/${vendorId}`);
setVendorStores(storesResponse.data);
if (storesResponse.data.length > 0) {
@@ -69,10 +82,28 @@ export default function ChatList() {
setLoading(true);
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
const chatsResponse = await axios.get(`/api/chats/vendor/${vendorId}`);
const chatsResponse = await authAxios.get(`/chats/vendor/${vendorId}`);
// Filter chats by selected store
const filteredChats = chatsResponse.data.filter(
@@ -82,7 +113,7 @@ export default function ChatList() {
setChats(filteredChats);
// 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);
} catch (error) {
console.error("Error fetching chats:", error);

View File

@@ -29,11 +29,23 @@ export default function ChatNotifications() {
useEffect(() => {
const fetchUnreadCounts = async () => {
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);
// If there are unread messages, fetch chat metadata
@@ -48,7 +60,7 @@ export default function ChatNotifications() {
await Promise.all(
chatIds.map(async (chatId) => {
try {
const chatResponse = await axios.get(`/api/chats/${chatId}`);
const chatResponse = await authAxios.get(`/chats/${chatId}`);
metadata[chatId] = {
buyerId: chatResponse.data.buyerId,
};

View File

@@ -24,15 +24,27 @@ export default function NewChatForm() {
useEffect(() => {
const fetchVendorStores = async () => {
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");
router.push("/login");
router.push("/auth/login");
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);
if (response.data.length > 0) {
@@ -61,7 +73,24 @@ export default function NewChatForm() {
setLoading(true);
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,
storeId: selectedStore,
initialMessage