Skip to content

Commit

Permalink
Sort emails most recent on admin page (#117)
Browse files Browse the repository at this point in the history
* updated URL

* updated env name

* added alert after sending email

* sort email by added time

* added indication while adding and sending email
  • Loading branch information
mai-vu authored May 29, 2024
1 parent 2ab3a71 commit b482829
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/app/admin/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@ export default function Home() {
const userData = await response.json();
if (userData.data && userData.data.admin === true) {
setUser(userData);
console.log('User is admin');
} else {
console.log('User is not admin');
setUnauthorized(true);
setTimeout(() => {
window.location.href = '/';
}, 3000); // Redirect after 3 seconds
}, 2000); // Redirect after 2 seconds
}
} else {
console.error('Failed to fetch user:', response.statusText);
setUnauthorized(true);
setTimeout(() => {
window.location.href = '/';
}, 3000); // Redirect after 3 seconds
}, 2000); // Redirect after 2 seconds
}
} catch (error) {
console.error('Error fetching user:', error);
setUnauthorized(true);
setTimeout(() => {
window.location.href = '/';
}, 3000); // Redirect after 3 seconds
}, 2000); // Redirect after 2 seconds
} finally {
setLoading(false);
}
Expand All @@ -67,7 +65,7 @@ export default function Home() {

const updateEmails = (emailObj, isAdd) => {
if (isAdd) {
setEmails(prevEmails => [...prevEmails, emailObj]);
setEmails(prevEmails => [emailObj, ...prevEmails]);
} else if (!isAdd) {
const updatedEmails = emails.filter(
existingEmail => existingEmail.email !== emailObj.email
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/contact-stat/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function GET() {
mongoose.models['contact-stats'] ||
mongoose.model('contact-stats', contactStat);

const contactStats = await ContactStat.find();
const contactStats = await ContactStat.find().sort({ _id: -1 });

return NextResponse.json({ contactStats }, { status: 200 });
} catch (error) {
Expand Down
19 changes: 17 additions & 2 deletions src/components/component/addEmailModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const CloseButton = ({ onClick }) => (
export function AddEmailModal({ open, onClose, updateEmails }) {
const [email, setEmail] = useState('');
const [emailsAdded, setEmailsAdded] = useState(1);
const [isAdding, setIsAdding] = useState(false);
const [isAddingAndSending, setIsAddingAndSending] = useState(false);

// Reset email state when the modal is opened
useEffect(() => {
Expand All @@ -47,6 +49,9 @@ export function AddEmailModal({ open, onClose, updateEmails }) {
};

const handleAddEmail = async () => {
// Set isAdding to true to indicate that the email is being added
setIsAdding(true);

try {
if (!validateEmail(email)) {
alert('Please enter a valid email address');
Expand All @@ -55,6 +60,7 @@ export function AddEmailModal({ open, onClose, updateEmails }) {
// Call the addEmailObjects function with an array of length 1 containing the email object
const emailsAdded = await emailHandler.addEmailObjects([
{ email, sent: false },
//indicate to UI that email is being added
]);
setEmailsAdded(emailsAdded);
setEmail('');
Expand All @@ -64,10 +70,15 @@ export function AddEmailModal({ open, onClose, updateEmails }) {
updateEmails({ email, sent: false }, isAdd);
} catch (error) {
console.error('Error adding email objects:', error);
} finally {
// Set isAdding to false after adding the email
setIsAdding(false);
}
};

const handleAddAndSendEmail = async () => {
setIsAddingAndSending(true);

try {
if (!validateEmail(email)) {
alert('Please enter a valid email address');
Expand All @@ -83,6 +94,8 @@ export function AddEmailModal({ open, onClose, updateEmails }) {
updateEmails({ email, sent: true }, isAdd);
} catch (error) {
console.error('Error adding email objects:', error);
} finally {
setIsAddingAndSending(false);
}
};

Expand All @@ -109,9 +122,11 @@ export function AddEmailModal({ open, onClose, updateEmails }) {
/>
</div>
<div className="flex gap-2">
<Button onClick={handleAddEmail}>Add Email</Button>
<Button onClick={handleAddEmail}>
{isAdding ? 'Adding...' : 'Add Email'}
</Button>
<Button variant="outline" onClick={handleAddAndSendEmail}>
Add and Send Email
{isAddingAndSending ? 'Sending...' : 'Add and Send Email'}
</Button>
</div>
{/* Display warning/notification field if emailsAdded is less than 1 */}
Expand Down

0 comments on commit b482829

Please sign in to comment.