This commit is contained in:
NotII
2025-03-03 21:30:36 +00:00
parent 3282051671
commit c70828ddcf
4 changed files with 53 additions and 26 deletions

View File

@@ -24,16 +24,14 @@ export default function NewChatForm() {
useEffect(() => {
const fetchVendorStores = async () => {
try {
// Get auth token from cookies
const authToken = getCookie("Authorization");
if (!authToken) {
toast.error("You need to be logged in");
toast.error("You must be logged in to start a chat");
router.push("/auth/login");
return;
}
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
@@ -41,16 +39,24 @@ export default function NewChatForm() {
}
});
// Get vendor ID from profile
const vendorResponse = await authAxios.get('/api/auth/me');
const vendorId = vendorResponse.data._id;
// Get vendor profile first
const vendorResponse = await authAxios.get('/auth/me');
// Fetch vendor's stores using storefront endpoint
const response = await authAxios.get(`/api/storefront`);
setVendorStores(response.data);
// Extract vendor ID properly
const vendorId = vendorResponse.data.vendor?._id;
if (response.data.length > 0) {
setSelectedStore(response.data[0]._id);
if (!vendorId) {
console.error("Vendor ID not found in profile response:", vendorResponse.data);
toast.error("Could not retrieve vendor information");
return;
}
// Fetch stores
const storesResponse = await authAxios.get(`/storefront`);
setVendorStores(storesResponse.data);
if (storesResponse.data.length > 0) {
setSelectedStore(storesResponse.data[0]._id);
}
} catch (error) {
console.error("Error fetching stores:", error);
@@ -59,7 +65,7 @@ export default function NewChatForm() {
};
fetchVendorStores();
}, [router]);
}, []);
const handleBackClick = () => {
router.push("/dashboard/chats");