Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 200 additions & 35 deletions client/src/containers/Admin/B2BMessages/B2BMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,105 @@

// export default B2BMessages;

// import React, { useEffect, useState } from "react";
// import axios from "axios";

// type Message = {
// updatedAt: string;
// createdAt: string;
// sender: string;
// receiver: string;
// content: string;
// };

// const B2BMessages: React.FC = () => {
// const [messages, setMessages] = useState<Message[]>([]);
// const [loading, setLoading] = useState(true);
// const [error, setError] = useState<string | null>(null);

// useEffect(() => {
// const fetchMessages = async () => {
// try {
// const res = await axios.get<Message[]>("/api/message/cheat");
// setMessages(res.data);
// setError(null);
// } catch (err) {
// console.error("Error fetching messages:", err);
// setError("Failed to fetch messages. Please try again later.");
// } finally {
// setLoading(false);
// }
// };

// fetchMessages();
// }, []);

// const formatDate = (dateStr: string) => {
// const d = new Date(dateStr);
// return d.toLocaleString();
// };

// return (
// <div style={{ padding: "20px" }}>
// <h1 style={{ marginBottom: "20px" }}>Admin Dashboard</h1>

// {/* Loading Indicator */}
// {loading && (
// <p style={{ fontWeight: "bold" }}>
// <span className="spinner-border spinner-border-sm"></span> Loading
// messages…
// </p>
// )}

// {/* Error Message */}
// {error && <p style={{ color: "red", fontWeight: "bold" }}>{error}</p>}

// {/* No Messages */}
// {!loading && !error && messages.length === 0 && (
// <p>No messages found.</p>
// )}

// {/* Message Table */}
// {!loading && messages.length > 0 && (
// <table style={{ width: "100%", borderCollapse: "collapse" }}>
// <thead>
// <tr style={{ background: "#f0f0f0" }}>
// <th style={cellStyle}>Sender</th>
// <th style={cellStyle}>Receiver</th>
// <th style={cellStyle}>Content</th>
// <th style={cellStyle}>Created</th>
// <th style={cellStyle}>Updated</th>
// </tr>
// </thead>
// <tbody>
// {messages.map((msg, i) => (
// <tr key={i}>
// <td style={cellStyle}>{msg.sender}</td>
// <td style={cellStyle}>{msg.receiver}</td>
// <td style={cellStyle}>{msg.content}</td>
// <td style={cellStyle}>{formatDate(msg.createdAt)}</td>
// <td style={cellStyle}>{formatDate(msg.updatedAt)}</td>
// </tr>
// ))}
// </tbody>
// </table>
// )}
// </div>
// );
// };

// const cellStyle = {
// border: "1px solid #ddd",
// padding: "8px",
// fontSize: "14px",
// };

// export default B2BMessages;

import React, { useEffect, useState } from "react";
import axios from "axios";

// Define the message type
type Message = {
updatedAt: string;
createdAt: string;
Expand Down Expand Up @@ -116,58 +212,127 @@ const B2BMessages: React.FC = () => {
};

return (
<div style={{ padding: "20px" }}>
<h1 style={{ marginBottom: "20px" }}>Admin Dashboard</h1>
<div style={styles.container}>
<h1 style={styles.header}>Admin B2B Messages</h1>

{/* Loading Indicator */}
{/* Loading State */}
{loading && (
<p style={{ fontWeight: "bold" }}>
<span className="spinner-border spinner-border-sm"></span> Loading
messages
</p>
<div style={styles.loadingContainer}>
<div className="spinner-border text-primary" role="status"></div>
<span style={{ marginLeft: 10 }}>Loading messages...</span>
</div>
)}

{/* Error Message */}
{error && <p style={{ color: "red", fontWeight: "bold" }}>{error}</p>}
{error && <div style={styles.error}>{error}</div>}

{/* No Messages */}
{!loading && !error && messages.length === 0 && (
<p>No messages found.</p>
<div style={styles.noMessages}>No messages found.</div>
)}

{/* Message Table */}
{/* Messages Table */}
{!loading && messages.length > 0 && (
<table style={{ width: "100%", borderCollapse: "collapse" }}>
<thead>
<tr style={{ background: "#f0f0f0" }}>
<th style={cellStyle}>Sender</th>
<th style={cellStyle}>Receiver</th>
<th style={cellStyle}>Content</th>
<th style={cellStyle}>Created</th>
<th style={cellStyle}>Updated</th>
</tr>
</thead>
<tbody>
{messages.map((msg, i) => (
<tr key={i}>
<td style={cellStyle}>{msg.sender}</td>
<td style={cellStyle}>{msg.receiver}</td>
<td style={cellStyle}>{msg.content}</td>
<td style={cellStyle}>{formatDate(msg.createdAt)}</td>
<td style={cellStyle}>{formatDate(msg.updatedAt)}</td>
<div style={styles.tableContainer}>
<table style={styles.table}>
<thead>
<tr style={styles.tableHeaderRow}>
<th style={styles.tableHeader}>Sender</th>
<th style={styles.tableHeader}>Receiver</th>
<th style={styles.tableHeader}>Content</th>
<th style={styles.tableHeader}>Created</th>
<th style={styles.tableHeader}>Updated</th>
</tr>
))}
</tbody>
</table>
</thead>
<tbody>
{messages.map((msg, i) => (
<tr
key={i}
style={{
...styles.tableRow,
backgroundColor: i % 2 === 0 ? "#fafafa" : "#fff",
}}
>
<td style={styles.tableCell}>{msg.sender}</td>
<td style={styles.tableCell}>{msg.receiver}</td>
<td style={styles.tableCell}>{msg.content}</td>
<td style={styles.tableCell}>{formatDate(msg.createdAt)}</td>
<td style={styles.tableCell}>{formatDate(msg.updatedAt)}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
};

const cellStyle = {
border: "1px solid #ddd",
padding: "8px",
fontSize: "14px",
// Styles
const styles: { [key: string]: React.CSSProperties } = {
container: {
padding: "20px",
fontFamily: "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
maxWidth: "1200px",
margin: "0 auto",
},
header: {
marginBottom: "20px",
fontSize: "2rem",
fontWeight: "bold",
color: "#333",
},
loadingContainer: {
display: "flex",
alignItems: "center",
fontSize: "1.1rem",
fontWeight: 500,
color: "#555",
},
error: {
color: "red",
fontWeight: "bold",
marginBottom: "10px",
},
noMessages: {
fontStyle: "italic",
color: "#777",
marginTop: "10px",
},
tableContainer: {
maxHeight: "500px",
overflowY: "auto",
border: "1px solid #ddd",
borderRadius: "8px",
},
table: {
width: "100%",
borderCollapse: "collapse",
minWidth: "800px",
},
tableHeaderRow: {
position: "sticky",
top: 0,
backgroundColor: "#007BFF",
color: "#fff",
zIndex: 1,
},
tableHeader: {
padding: "12px",
textAlign: "left",
fontWeight: "bold",
borderBottom: "2px solid #0056b3",
},
tableRow: {
transition: "background-color 0.2s",
cursor: "default",
},
tableCell: {
padding: "10px",
borderBottom: "1px solid #ddd",
fontSize: "0.95rem",
wordBreak: "break-word",
},
};

export default B2BMessages;
Loading
Loading