tsk-9: View sent messages (#19)

Closes #9

Reviewed-on: phoenix/textsender#19
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2026-01-02 23:36:26 +00:00
committed by phoenix
parent c2ade93c3f
commit a1a8b44da3
3 changed files with 748 additions and 1 deletions
+17 -1
View File
@@ -2,12 +2,14 @@ import { useState } from 'react';
import AddContact from './AddContact';
import ViewContact from './ViewContact';
import ViewSentMessages from './ViewSentMessages';
import './Dashboard.css';
const Dashboard = () => {
const [activeView, setActiveView] = useState('dashboard');
const [showContactForm, setShowContactForm] = useState(false);
const [isViewSentMessagesOpen, setViewSentMessagesOpen] = useState(false);
const [isPopupOpen, setIsPopupOpen] = useState(false);
const handleAddContactClick = () => {
@@ -75,6 +77,19 @@ const Dashboard = () => {
<span className="btn-hint">Click for action 1</span>
</button>
<button
className="dashboard-btn primary"
onClick={() => setViewSentMessagesOpen(true)}
>
<span className="btn-text">Sent messages</span>
<span className="btn-hint">View sent messages</span>
</button>
<ViewSentMessages
isOpen={isViewSentMessagesOpen}
onClose={() => setViewSentMessagesOpen(false)}
/>
<button
className="dashboard-btn secondary"
onClick={() => handleAddContactClick()}
@@ -105,7 +120,8 @@ const Dashboard = () => {
fontSize: '16px',
}}
>
View Items
<span className="btn-text">View Contact</span>
<span className="btn-hint">View created contacts</span>
</button>
<ViewContact
+357
View File
@@ -0,0 +1,357 @@
/* ViewSentMessages.css */
.view-sent-message-modal-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
padding: 20px;
}
.view-sent-message-modal {
background: transparent;
width: 100%;
max-width: 500px;
max-height: 90vh;
overflow-y: auto;
}
.view-sent-message-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.view-sent-message-title {
color: white;
font-size: 24px;
font-weight: 600;
margin: 0;
}
.close-btn {
background: none;
border: none;
color: white;
font-size: 32px;
cursor: pointer;
line-height: 1;
padding: 0;
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
transition: background-color 0.3s ease;
}
.close-btn:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.view-sent-message-card {
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 40px;
width: 100%;
}
.view-sent-message-form {
margin-bottom: 0;
}
.required {
color: #ef4444;
}
.form-actions {
display: flex;
gap: 12px;
margin-top: 30px;
}
.cancel-btn {
flex: 1;
background: #f3f4f6;
color: #4b5563;
border: 2px solid #e5e7eb;
border-radius: 8px;
padding: 14px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
}
.cancel-btn:hover:not(:disabled) {
background: #e5e7eb;
transform: translateY(-1px);
}
.cancel-btn:active:not(:disabled) {
transform: translateY(0);
}
.cancel-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.submit-btn {
flex: 1;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 8px;
padding: 14px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
}
.submit-btn:hover:not(:disabled) {
transform: translateY(-1px);
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
}
.submit-btn:active:not(:disabled) {
transform: translateY(0);
}
.submit-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.submit-error {
background-color: #fee;
border: 1px solid #fcc;
border-radius: 6px;
padding: 12px;
margin-bottom: 20px;
text-align: center;
color: #ef4444;
font-size: 14px;
}
/* Responsive adjustments */
@media (max-width: 480px) {
.view-sent-message-card {
padding: 30px 20px;
}
.view-sent-message-title {
font-size: 20px;
}
.form-actions {
flex-direction: column;
}
}
/* Loading animation */
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* Scrollbar styling */
.view-sent-message-modal::-webkit-scrollbar {
width: 8px;
}
.view-sent-message-modal::-webkit-scrollbar-track {
background: transparent;
}
.view-sent-message-modal::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.3);
border-radius: 4px;
}
.view-sent-message-modal::-webkit-scrollbar-thumb:hover {
background: rgba(255, 255, 255, 0.5);
}
/* ViewContact.css */
/* Add to existing ViewContact.css */
/* Loading spinner */
.loading-spinner {
border: 4px solid #f3f4f6;
border-top: 4px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* Table styles */
.view-sent-message-table {
width: 100%;
border-collapse: collapse;
}
.view-sent-message-table th {
background: #f9fafb;
padding: 12px 16px;
text-align: left;
border-bottom: 2px solid #e5e7eb;
color: #374151;
font-weight: 600;
font-size: 14px;
position: sticky;
top: 0;
z-index: 1;
}
.view-sent-message-table td {
padding: 12px 16px;
border-bottom: 1px solid #e5e7eb;
font-size: 14px;
}
.view-sent-message-table tr:last-child td {
border-bottom: none;
}
.view-sent-message-table tr:hover {
background-color: #f9fafb;
}
/* Contact item styles */
.view-sent-message-item {
display: flex;
align-items: flex-start;
gap: 12px;
padding: 16px;
border-bottom: 1px solid #e5e7eb;
}
.view-sent-message-item:last-child {
border-bottom: none;
}
.view-sent-message-avatar {
width: 40px;
height: 40px;
border-radius: 50%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
display: flex;
align-items: center;
justify-content: center;
font-weight: 600;
font-size: 16px;
}
.view-sent-message-info {
flex: 1;
}
.view-sent-message-name {
font-weight: 600;
color: #111827;
margin-bottom: 4px;
}
.view-sent-message-phone {
color: #6b7280;
font-size: 14px;
}
.view-sent-message-id {
font-size: 12px;
color: #9ca3af;
background: #f3f4f6;
padding: 2px 8px;
border-radius: 4px;
}
/* Search input */
.search-input {
width: 100%;
padding: 10px 12px;
border: 1px solid #e5e7eb;
border-radius: 6px;
font-size: 14px;
transition: border-color 0.3s ease;
}
.search-input:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
/* Empty state */
.empty-state {
text-align: center;
padding: 60px 20px;
}
.empty-state-icon {
font-size: 48px;
margin-bottom: 16px;
opacity: 0.5;
}
.empty-state-title {
font-size: 18px;
font-weight: 500;
color: #374151;
margin-bottom: 8px;
}
.empty-state-description {
color: #6b7280;
font-size: 14px;
margin-bottom: 20px;
}
/* Scrollbar for view-sent-message list */
.view-sent-message-list-container {
max-height: 400px;
overflow-y: auto;
}
.view-sent-message-list-container::-webkit-scrollbar {
width: 6px;
}
.view-sent-message-list-container::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 3px;
}
.view-sent-message-list-container::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 3px;
}
.view-sent-message-list-container::-webkit-scrollbar-thumb:hover {
background: #a1a1a1;
}
+374
View File
@@ -0,0 +1,374 @@
import { useState, useEffect, useCallback } from 'react';
import { API_BASE_URL } from '../constants/api';
import { DATA_KEY_ACCESS_TOKEN, DATA_KEY_USER_ID } from '../constants/app';
import './ViewSentMessages.css';
const ViewSentMessages = ({
isOpen,
onClose,
title = 'View Sent Messages ',
}) => {
const [sentMessages, setSentMessages] = useState([]);
const [lastUpdated, setLastUpdated] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [searchTerm, setSearchTerm] = useState('');
// Fetch the sent messages or Message Event Responses
const fetchSentMessages = useCallback(async () => {
try {
setLoading(true);
setError(null);
const accessToken = localStorage.getItem(DATA_KEY_ACCESS_TOKEN);
const userId = localStorage.getItem(DATA_KEY_USER_ID);
if (!accessToken) {
throw new Error('Authentication required. Please log in again.');
}
const response = await fetch(
`${API_BASE_URL}/api/v1/schedule/message/event/response?user_id=${userId}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
}
);
if (!response.ok) {
if (response.status === 401) {
throw new Error('Session expired. Please log in again.');
} else if (response.status === 404) {
console.log('No messages sent associated with user');
setLoading(false);
return;
} else {
throw new Error(`Failed to fetch sent messages: ${response.status}`);
}
}
const result = await response.json();
if (result.data) {
setLastUpdated(new Date());
setSentMessages(result.data);
} else {
throw new Error(result.message || 'Failed to load sent messages');
}
} catch (err) {
setError(err.message || 'Failed to load sent messages.');
setSentMessages([]);
} finally {
setLoading(false);
}
}, []);
const handleRefresh = () => {
fetchSentMessages();
};
useEffect(() => {
if (isOpen) {
console.log('Fetching sent messages');
fetchSentMessages();
}
}, [isOpen, fetchSentMessages]);
const filteredSentMessages = sentMessages.filter((sentMessage) => {
const searchLower = searchTerm.toLowerCase();
return (
(sentMessage.status &&
sentMessage.status.toLowerCase().includes(searchLower)) ||
(sentMessage.sent &&
sentMessage.sent.toLowerCase().includes(searchLower)) ||
(sentMessage.id && sentMessage.id.toString().includes(searchTerm))
);
});
const formatLastUpdated = () => {
if (!lastUpdated) return 'Never';
return lastUpdated.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
});
};
if (!isOpen) return null;
return (
<div className="view-sent-message-modal-overlay" onClick={onClose}>
<div
className="view-sent-message-modal"
onClick={(e) => e.stopPropagation()}
>
<div className="view-sent-message-card">
<div className="view-sent-message-header">
<h2 className="view-sent-message-title">{title}</h2>
<button className="close-btn" onClick={onClose} aria-label="Close">
×
</button>
</div>
<div className="view-sent-message-form">
{error && (
<div className="submit-error">
<div style={{ marginBottom: '8px' }}>{error}</div>
<button
onClick={handleRefresh}
onStype={{
background: '#ef4444',
color: 'white',
border: 'none',
borderRadius: '4px',
padding: '6px 12px',
cursor: 'pointer',
fontSize: '12px',
}}
>
Retry Now
</button>
</div>
)}
{loading ? (
<div style={{ textAlign: 'center', padding: '40px' }}>
<div className="loafing-spinner" />
<p style={{ marginTop: '20px', color: '#4b5563' }}>
Loading sent messages
</p>
</div>
) : (
<>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: '20px',
flexWrap: 'wrap',
gap: '10px',
}}
>
<button
onClick={handleRefresh}
style={{
background: '#f3f4f6',
color: '#4b5563',
border: '1px solid #e5e7eb',
borderRadius: '6px',
padding: '8px 16px',
cursor: 'pointer',
fontSize: '14px',
display: 'flex',
alignItems: 'center',
gap: '8px',
}}
>
<span></span> Refresh
</button>
<input
type="text"
placeholder="Search sent messages..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
style={{
flex: 1,
minWidth: '200px',
maxWidth: '300px',
padding: '8px 12px',
border: '1px solid #e5e7eb',
borderRadius: '6px',
fontSize: '14px',
}}
/>
<div
style={{
fontSize: '12px',
color: '#6b7280',
display: 'flex',
alignItems: 'center',
gap: '8px',
}}
>
<span>Updated: {formatLastUpdated()}</span>
<span
style={{
display: 'inline-block',
width: '8px',
height: '8px',
borderRadius: '50%',
background: lastUpdated ? '#10b981' : '#9ca3af',
}}
/>
</div>
</div>
{/* Sent Messages List */}
{filteredSentMessages.length === 0 ? (
<div
style={{
textAlign: 'center',
padding: '40px',
color: '#4b5563',
}}
>
<div style={{ fontSize: '48px', marginBottom: '16px' }}>
X
</div>
<p
style={{
fontSize: '18px',
fontWeight: '500',
marginBottom: '8px',
}}
>
{searchTerm
? 'No matching sent messages found'
: 'No sent messages available'}
</p>
<p style={{ marginBottom: '20px' }}>
{searchTerm
? 'Try a different search term'
: 'Create a contact and send a message to get started'}
</p>
{searchTerm && (
<button
onClick={() => setSearchTerm('')}
style={{
background: '#f3f4f6',
color: '#4b5563',
border: '1px solid #e5e7eb',
borderRadius: '6px',
padding: '8px 16px',
cursor: 'pointer',
fontSize: '14px',
marginBottom: '10px',
}}
>
Clear Search
</button>
)}
</div>
) : (
<>
<div
style={{
maxHeight: '400px',
overflowY: 'auto',
border: '1px solid #e5e7eb',
borderRadius: '8px',
background: '#fafafa',
}}
>
<div style={{ padding: '4px' }}>
{filteredSentMessages.map((sentMessage, index) => (
<div
key={sentMessage.id}
style={{
padding: '16px',
borderBottom:
index < filteredSentMessages.length - 1
? '1px solid #e5e7eb'
: 'none',
background: 'white',
transition: 'all 0.2s',
}}
>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'flex-start',
marginBottom: '8px',
}}
>
<div
style={{
display: 'flex',
alignItems: 'center',
gap: '12px',
}}
>
<span
style={{
background: '#667eea',
color: 'white',
width: '28px',
height: '28px',
borderRadius: '50%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontSize: '12px',
fontWeight: '600',
}}
>
{index + 1}
</span>
<div>
<h4
style={{
margin: 0,
color: '#111827',
fontSize: '16px',
fontWeight: '600',
}}
>
{sentMessage.status}
</h4>
<h5>Sent: {sentMessage.sent}</h5>
</div>
</div>
<span
style={{
fontSize: '12px',
color: '#9ca3af',
background: '#f3f4f6',
padding: '2px 8px',
borderRadius: '4px',
}}
>
Id: {sentMessage.id}
</span>
</div>
</div>
))}
</div>
</div>
</>
)}
</>
)}
<div className="form-actions">
<button
type="button"
className="cancel-btn"
onClick={onClose}
disabled={loading}
>
Close
</button>
<button
type="button"
className="submit-btn"
onClick={handleRefresh}
disabled={loading}
>
{loading ? 'Refreshing...' : 'Refresh List'}
</button>
</div>
</div>
</div>
</div>
</div>
);
};
export default ViewSentMessages;