useEmail.ts
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type { Email } from "@db/dstipro/email/types";
import type { PartialDeep } from "type-fest";
export type MoveEmailToAction = "inbox" | "spam" | "trash";
export const useEmail = () => {
const route = useRoute("dstipro-email-filter");
const updateEmails = async (ids: Email["id"][], data: PartialDeep<Email>) => {
await $api("dstipro/email", {
method: "POST",
body: JSON.stringify({ ids, data }),
});
};
const updateEmailLabels = async (
ids: Email["id"][],
label: Email["labels"][number]
) => {
await $api("/dstipro/email", {
method: "POST",
body: { ids, label },
});
};
const emailMoveToFolderActions: {
action: MoveEmailToAction;
icon: string;
}[] = [
{ action: "inbox", icon: "ri-mail-line" },
{ action: "spam", icon: "ri-spam-2-line" },
{ action: "trash", icon: "ri-delete-bin-line" },
];
const labels: { title: Email["labels"][number]; color: string }[] = [
{
title: "personal",
color: "success",
},
{
title: "company",
color: "primary",
},
{
title: "important",
color: "warning",
},
{
title: "private",
color: "error",
},
];
const resolveLabelColor = (label: Email["labels"][number]) => {
if (label === "personal") return "success";
if (label === "company") return "primary";
if (label === "important") return "warning";
if (label === "private") return "error";
return "secondary";
};
const shallShowMoveToActionFor = (action: MoveEmailToAction) => {
if (action === "trash") return route.params.filter !== "trashed";
else if (action === "inbox")
return !(
route.params.filter === undefined ||
route.params.filter === "sent" ||
route.params.filter === "draft"
);
else if (action === "spam")
return !(
route.params.filter === "spam" ||
route.params.filter === "sent" ||
route.params.filter === "draft"
);
return false;
};
const moveSelectedEmailTo = async (
action: MoveEmailToAction,
selectedEmails: number[]
) => {
const dataToUpdate: PartialDeep<Email> = {};
if (action === "inbox") {
if (route.params.filter === "trashed") dataToUpdate.isDeleted = false;
dataToUpdate.folder = "inbox";
} else if (action === "spam") {
if (route.params.filter === "trashed") dataToUpdate.isDeleted = false;
dataToUpdate.folder = "spam";
} else if (action === "trash") {
dataToUpdate.isDeleted = true;
}
await updateEmails(selectedEmails, dataToUpdate);
};
return {
labels,
resolveLabelColor,
shallShowMoveToActionFor,
emailMoveToFolderActions,
moveSelectedEmailTo,
updateEmails,
updateEmailLabels,
};
};