I'm creating an email - gmail like Single Page Application, I'm trying to add an Archive and Unarchive button, the archive button works as the mail moves to the Archive mailbox, however, if I click the button to unarchive the mail from the Archive Mailbox, I'm unable to as it will just reload.
'When viewing an Inbox email, the user should be presented with a button that lets them archive the email. When viewing an Archive email, the user should be presented with a button that lets them unarchive the email. This requirement does not apply to emails in the Sent mailbox.'
This is what I've tried but still the same result
// only show this archive icon in inbox and archive
if (mailbox != 'sent') {
// add the archive button
let archiveBtn = document.createElement("archiveDiv");
archiveBtn.innerHTML = `<div onclick="archiveEmail(${email.id})"><i class="fa fa-archive" aria-hidden="true"></i></div>`
// archiveBtn.onclick = () => {
// addArchive(id)
// }
buttonsDiv.appendChild(archiveBtn);
}
function archiveEmail(id, mailbox) {
if (mailbox == 'archive' && email.archived == true) {
fetch(`/emails/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
} else {
fetch(`/emails/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
}
console.log("Email Unarchived");
// reload the inbox
load_mailbox('inbox');
}
Also, tried this:
if (mailbox != 'sent') {
// add the archive button
let archiveBtn = document.createElement("archiveDiv");
if (mailbox == 'archive' && email.archived == true) {
archiveBtn.innerHTML = `<div onclick="Unarchived(${email.id})"><i class="fa fa-archive" aria-hidden="true"></i></div>`
} else {
archiveBtn.innerHTML = `<div onclick="archiveEmail(${email.id})"><i class="fa fa-archive" aria-hidden="true"></i></div>`
}
// archiveBtn.onclick = () => {
// addArchive(id)
// }
buttonsDiv.appendChild(archiveBtn);
}
function archiveEmail(id) {
fetch(`/emails/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: true
})
})
}
function Unarchived(id) {
fetch(`/emails/${id}`, {
method: 'PUT',
body: JSON.stringify({
archived: false
})
})
}