fix toast problem
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import { useSession } from "next-auth/react"
|
||||
import { useRouter } from 'next/router';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import React, { useState, useEffect, use } from 'react';
|
||||
import Layout from "../components/layout"
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
@ -66,18 +66,28 @@ export default function DashboardPage({ initialItems, initialUserId, cartEvents,
|
||||
// }, []);// show the message every time we load the page
|
||||
|
||||
|
||||
// const [processedMessages, setProcessedMessages] = useState(new Set());
|
||||
// useEffect(() => {
|
||||
// if (messages && messages.length > 0) {
|
||||
// const unprocessedMessages = messages.filter(message => !processedMessages.has(message.id));
|
||||
// if (unprocessedMessages.length > 0) {
|
||||
// showMessageToasts(unprocessedMessages);
|
||||
// setProcessedMessages(new Set([...processedMessages, ...unprocessedMessages.map(msg => msg.id)]));
|
||||
// }
|
||||
// }
|
||||
// }, [messages, processedMessages]);
|
||||
|
||||
useEffect(() => {
|
||||
if (messages && messages.length > 0) {
|
||||
console.log("user has ", messages.length, " messages.");
|
||||
showMessageToasts(messages);
|
||||
}
|
||||
}, []);
|
||||
}, [messages]);
|
||||
|
||||
const showMessageToasts = (messages, handleMessageOptionAnswer) => {
|
||||
const showMessageToasts = (messages) => {
|
||||
const handleOptionClick = async (messageId, option, toastId) => {
|
||||
try {
|
||||
await axiosInstance.put(`/api/data/messages/${messageId}`, { answer: option });
|
||||
toast.dismiss(toastId);
|
||||
handleClose(toastId);
|
||||
} catch (error) {
|
||||
console.error("Error updating message:", error);
|
||||
toast.error("Error updating message. Please try again.");
|
||||
@ -88,8 +98,8 @@ export default function DashboardPage({ initialItems, initialUserId, cartEvents,
|
||||
toast.dismiss(toastId);
|
||||
};
|
||||
|
||||
messages.forEach((message) => {
|
||||
const toastId = `message-${message.id}`;
|
||||
messages.forEach((message, messageIndex) => {
|
||||
const toastId = `message-${message.id}-${messageIndex}`;
|
||||
const content = (
|
||||
<div>
|
||||
<div>{message.content.message}</div>
|
||||
@ -109,13 +119,17 @@ export default function DashboardPage({ initialItems, initialUserId, cartEvents,
|
||||
|
||||
toast(content, {
|
||||
toastId,
|
||||
autoClose: false, // Keep the toast open until manually closed
|
||||
closeButton: false,
|
||||
autoClose: false,
|
||||
closeButton: true,
|
||||
onClose: () => handleClose(toastId),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// const showMessageToastNewModal = (messages, handleMessageOptionAnswer) => {
|
||||
// let currentMessageIndex = 0;
|
||||
|
||||
|
Reference in New Issue
Block a user