Skip to content

Commit 785adff

Browse files
authored
Merge pull request #20 from techx/evan/delete-emails
deleting emails supported
2 parents 8b9fab8 + f9d5637 commit 785adff

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

client/src/routes/inbox.tsx

+71
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
IconFolderOff,
3131
IconCheck,
3232
IconX,
33+
IconTrash,
3334
} from "@tabler/icons-react";
3435

3536
interface Thread {
@@ -384,6 +385,66 @@ export default function InboxPage() {
384385
});
385386
};
386387

388+
const deleteThread = () => {
389+
notifications.show({
390+
id: "loading",
391+
title: "Loading",
392+
color: "red",
393+
message: "Deleting thread...",
394+
loading: true,
395+
autoClose: false,
396+
});
397+
const formData = new FormData();
398+
formData.append("id", active.toString());
399+
fetch("/api/emails/delete", {
400+
method: "POST",
401+
body: formData,
402+
})
403+
.then((res) => {
404+
if (res.ok) return res.json();
405+
notifications.update({
406+
id: "loading",
407+
title: "Error!",
408+
color: "red",
409+
loading: false,
410+
message: "Something went wrong!",
411+
});
412+
})
413+
.then(() => {
414+
setThreads((oldThreads) => {
415+
const updatedThreads = oldThreads.filter(
416+
(thread) => thread.id !== active,
417+
);
418+
419+
let newActive = -1;
420+
421+
//setting to next email hopefully
422+
if (updatedThreads.length > 0) {
423+
const currentIndex = oldThreads.findIndex(
424+
(thread) => thread.id === active,
425+
);
426+
if (currentIndex >= 0 && currentIndex < updatedThreads.length) {
427+
newActive = updatedThreads[currentIndex].id;
428+
} else if (currentIndex >= updatedThreads.length) {
429+
newActive = updatedThreads[updatedThreads.length - 1].id;
430+
}
431+
}
432+
setActive(newActive);
433+
return updatedThreads;
434+
});
435+
notifications.update({
436+
id: "loading",
437+
title: "Success!",
438+
color: "green",
439+
message: "Deleted thread",
440+
icon: <IconCheck />,
441+
autoClose: 2000,
442+
withCloseButton: false,
443+
loading: false,
444+
});
445+
});
446+
};
447+
387448
useEffect(() => {
388449
if (activeThread && activeThread.emailList.length > threadSize) {
389450
if (viewport && viewport.current)
@@ -698,6 +759,16 @@ export default function InboxPage() {
698759
Unresolve
699760
</Button>
700761
)}
762+
763+
{!activeThread.resolved && (
764+
<Button
765+
leftSection={<IconTrash />}
766+
color="red"
767+
onClick={() => deleteThread()}
768+
>
769+
Delete
770+
</Button>
771+
)}
701772
</Group>
702773
</Stack>
703774
</Box>

server/controllers/emails.py

+18
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,24 @@ def unresolve():
522522
return {"message": "Successfully updated"}, 200
523523

524524

525+
@emails.route("/delete", methods=["POST"])
526+
def delete():
527+
"""POST /delete
528+
529+
Delete an email thread.
530+
"""
531+
data = request.form
532+
thread = db.session.execute(select(Thread).where(Thread.id == data["id"])).scalar()
533+
if not thread:
534+
return {"message": "Thread not found"}, 400
535+
db.session.delete(thread)
536+
db.session.commit()
537+
538+
print("deleted thread", flush=True)
539+
540+
return {"message": "Successfully deleted"}, 200
541+
542+
525543
@emails.route("/get_threads", methods=["GET"])
526544
def get_threads():
527545
"""GET /get_threads

server/models/email.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ class Email(db.Model):
4141
message_id: Mapped[str] = mapped_column(nullable=False)
4242

4343
response: Mapped[Optional["Response"]] = relationship(
44-
"Response", back_populates="email", init=False
44+
"Response",
45+
back_populates="email",
46+
cascade="all, delete-orphan",
47+
single_parent=True,
48+
uselist=False,
49+
passive_deletes=True,
50+
init=False,
4551
)
4652

4753
is_reply: Mapped[bool] = mapped_column(nullable=False)

0 commit comments

Comments
 (0)