Skip to content

Commit

Permalink
Merge pull request #20 from techx/evan/delete-emails
Browse files Browse the repository at this point in the history
deleting emails supported
  • Loading branch information
azliu0 committed May 4, 2024
2 parents 8b9fab8 + f9d5637 commit 785adff
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
71 changes: 71 additions & 0 deletions client/src/routes/inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
IconFolderOff,
IconCheck,
IconX,
IconTrash,
} from "@tabler/icons-react";

interface Thread {
Expand Down Expand Up @@ -384,6 +385,66 @@ export default function InboxPage() {
});
};

const deleteThread = () => {
notifications.show({
id: "loading",
title: "Loading",
color: "red",
message: "Deleting thread...",
loading: true,
autoClose: false,
});
const formData = new FormData();
formData.append("id", active.toString());
fetch("/api/emails/delete", {
method: "POST",
body: formData,
})
.then((res) => {
if (res.ok) return res.json();
notifications.update({
id: "loading",
title: "Error!",
color: "red",
loading: false,
message: "Something went wrong!",
});
})
.then(() => {
setThreads((oldThreads) => {
const updatedThreads = oldThreads.filter(
(thread) => thread.id !== active,
);

let newActive = -1;

//setting to next email hopefully
if (updatedThreads.length > 0) {
const currentIndex = oldThreads.findIndex(
(thread) => thread.id === active,
);
if (currentIndex >= 0 && currentIndex < updatedThreads.length) {
newActive = updatedThreads[currentIndex].id;
} else if (currentIndex >= updatedThreads.length) {
newActive = updatedThreads[updatedThreads.length - 1].id;
}
}
setActive(newActive);
return updatedThreads;
});
notifications.update({
id: "loading",
title: "Success!",
color: "green",
message: "Deleted thread",
icon: <IconCheck />,
autoClose: 2000,
withCloseButton: false,
loading: false,
});
});
};

useEffect(() => {
if (activeThread && activeThread.emailList.length > threadSize) {
if (viewport && viewport.current)
Expand Down Expand Up @@ -698,6 +759,16 @@ export default function InboxPage() {
Unresolve
</Button>
)}

{!activeThread.resolved && (
<Button
leftSection={<IconTrash />}
color="red"
onClick={() => deleteThread()}
>
Delete
</Button>
)}
</Group>
</Stack>
</Box>
Expand Down
18 changes: 18 additions & 0 deletions server/controllers/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,24 @@ def unresolve():
return {"message": "Successfully updated"}, 200


@emails.route("/delete", methods=["POST"])
def delete():
"""POST /delete
Delete an email thread.
"""
data = request.form
thread = db.session.execute(select(Thread).where(Thread.id == data["id"])).scalar()
if not thread:
return {"message": "Thread not found"}, 400
db.session.delete(thread)
db.session.commit()

print("deleted thread", flush=True)

return {"message": "Successfully deleted"}, 200


@emails.route("/get_threads", methods=["GET"])
def get_threads():
"""GET /get_threads
Expand Down
8 changes: 7 additions & 1 deletion server/models/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ class Email(db.Model):
message_id: Mapped[str] = mapped_column(nullable=False)

response: Mapped[Optional["Response"]] = relationship(
"Response", back_populates="email", init=False
"Response",
back_populates="email",
cascade="all, delete-orphan",
single_parent=True,
uselist=False,
passive_deletes=True,
init=False,
)

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

0 comments on commit 785adff

Please sign in to comment.