Skip to content

Commit 1a3d700

Browse files
Erika Perugachinot-gabriel
authored andcommitted
Fix badge. Fix #424 (#432)
1 parent 0fad2d7 commit 1a3d700

File tree

20 files changed

+405
-435
lines changed

20 files changed

+405
-435
lines changed

electron_app/src/windows/composer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const destroy = async ({ composerId, emailId }) => {
146146
oldDraftEmail.id,
147147
undefined
148148
);
149-
sendEventToMailbox('update-drafts', { operation: 'less', value: 1 });
149+
sendEventToMailbox('update-drafts', true);
150150
} else if (
151151
type === composerEvents.REPLY ||
152152
type === composerEvents.REPLY_ALL
@@ -175,10 +175,10 @@ const sendEventToMailbox = (eventName, data) => {
175175
const saveDraftToDatabase = async (composerId, dataDraft) => {
176176
const emailToEdit = globalManager.emailToEdit.get(composerId);
177177
const { type, key } = emailToEdit || {};
178-
let badgeOperation = {};
178+
let shouldUpdateBadge = false;
179179
if ((!type && !key) || type !== composerEvents.EDIT_DRAFT) {
180180
await dbManager.createEmail(dataDraft);
181-
badgeOperation = { operation: 'add', value: 1 };
181+
shouldUpdateBadge = true;
182182
} else {
183183
const [oldEmail] = await dbManager.getEmailByKey(key);
184184
const newEmailId = await dbManager.deleteEmailLabelAndContactByEmailId(
@@ -195,7 +195,7 @@ const saveDraftToDatabase = async (composerId, dataDraft) => {
195195
return;
196196
}
197197
}
198-
sendEventToMailbox('update-drafts', badgeOperation);
198+
sendEventToMailbox('update-drafts', shouldUpdateBadge);
199199
};
200200

201201
module.exports = {

email_mailbox/src/actions/emails.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Email } from './types';
22
import {
33
getContactByIds,
44
getEmailsByThreadId,
5-
updateUnreadEmailByThreadIds,
65
setMuteEmailById,
76
setUnreadEmailById,
87
updateEmail,
@@ -12,7 +11,6 @@ import {
1211
} from '../utils/electronInterface';
1312
import { EmailUtils } from '../utils/electronUtilsInterface';
1413
import { loadContacts } from './contacts';
15-
import { updateLabelSuccess } from './labels';
1614
import { EmailStatus, SocketCommand } from '../utils/const';
1715
import { unsendEmailFiles } from './files';
1816
import {
@@ -139,19 +137,6 @@ export const removeEmailsOnSuccess = emailIds => ({
139137
emailIds
140138
});
141139

142-
export const updateUnreadEmails = (thread, label) => {
143-
return async dispatch => {
144-
try {
145-
await updateUnreadEmailByThreadIds([thread.id], thread.unread);
146-
if (label) {
147-
dispatch(updateLabelSuccess(label));
148-
}
149-
} catch (e) {
150-
// To do
151-
}
152-
};
153-
};
154-
155140
export const unsendEmail = params => {
156141
return async dispatch => {
157142
const { key, emailId, contactIds, unsendDate } = params;

email_mailbox/src/actions/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
muteNotifications,
3737
removeEmails,
3838
removeEmailsOnSuccess,
39-
updateUnreadEmails,
4039
unsendEmail,
4140
unsendEmailOnSuccess,
4241
updateEmailLabels
@@ -47,6 +46,7 @@ import {
4746
loadLabels,
4847
removeLabel,
4948
removeLabelOnSuccess,
49+
updateBadgeLabels,
5050
updateLabel,
5151
updateLabelSuccess
5252
} from './labels';
@@ -111,13 +111,13 @@ export {
111111
unsendEmailFiles,
112112
unsendEmailOnSuccess,
113113
updateAllFeedItemsAsOlder,
114+
updateBadgeLabels,
114115
updateFeedItemSuccess,
115116
updateEmailIdsThread,
116117
updateEmailLabels,
117118
updateLabel,
118119
updateLabelSuccess,
119120
updateStatusThread,
120-
updateUnreadEmails,
121121
updateUnreadThreads,
122122
updateUnreadThreadsSuccess
123123
};

email_mailbox/src/actions/labels.js

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,14 @@ import {
99
updateLabel as updateLabelDB,
1010
deleteLabelById
1111
} from '../utils/electronInterface';
12+
import { sendUpdateLabelsErrorMessage } from './../utils/electronEventInterface';
1213
import { SocketCommand } from '../utils/const';
1314

14-
export const addLabels = labels => {
15-
return {
16-
type: Label.ADD_BATCH,
17-
labels: labels
18-
};
19-
};
20-
21-
export const updateLabelSuccess = label => {
22-
return {
23-
type: Label.UPDATE_SUCCESS,
24-
label: label
25-
};
26-
};
27-
28-
export const addLabel = (label, isByEvent) => {
15+
export const addLabel = label => {
2916
return async dispatch => {
3017
try {
31-
const [response] = await createLabel(label);
32-
if (response) {
33-
const labelId = response;
18+
const [labelId] = await createLabel(label);
19+
if (labelId) {
3420
const { text, color, visible } = label;
3521
const labels = {
3622
[labelId]: {
@@ -41,23 +27,26 @@ export const addLabel = (label, isByEvent) => {
4127
visible
4228
}
4329
};
44-
if (isByEvent) {
45-
dispatch(addLabels(labels));
46-
} else {
47-
const eventParams = {
48-
cmd: SocketCommand.PEER_LABEL_CREATED,
49-
params: { color, text }
50-
};
51-
await postPeerEvent(eventParams);
52-
dispatch(addLabels(labels));
53-
}
30+
const eventParams = {
31+
cmd: SocketCommand.PEER_LABEL_CREATED,
32+
params: { color: color.replace('#', ''), text }
33+
};
34+
await postPeerEvent(eventParams);
35+
dispatch(addLabels(labels));
5436
}
5537
} catch (e) {
56-
//TO DO
38+
sendUpdateLabelsErrorMessage();
5739
}
5840
};
5941
};
6042

43+
export const addLabels = labels => {
44+
return {
45+
type: Label.ADD_BATCH,
46+
labels
47+
};
48+
};
49+
6150
export const loadLabels = () => {
6251
return async dispatch => {
6352
try {
@@ -85,11 +74,31 @@ export const loadLabels = () => {
8574
labels[LabelType.draft.id].badge = badgeDraft[0].count;
8675
dispatch(addLabels(labels));
8776
} catch (e) {
88-
// TO DO
77+
sendUpdateLabelsErrorMessage();
8978
}
9079
};
9180
};
9281

82+
export const removeLabel = id => {
83+
return async dispatch => {
84+
try {
85+
const response = await deleteLabelById(id);
86+
if (response) {
87+
dispatch(removeLabelOnSuccess(id));
88+
}
89+
} catch (e) {
90+
sendUpdateLabelsErrorMessage();
91+
}
92+
};
93+
};
94+
95+
export const removeLabelOnSuccess = labelId => {
96+
return {
97+
type: Label.REMOVE,
98+
labelId
99+
};
100+
};
101+
93102
export const updateLabel = ({ id, color, text, visible }) => {
94103
return async dispatch => {
95104
try {
@@ -98,27 +107,69 @@ export const updateLabel = ({ id, color, text, visible }) => {
98107
dispatch(updateLabelSuccess({ id, color, text, visible }));
99108
}
100109
} catch (e) {
101-
// TO DO
110+
sendUpdateLabelsErrorMessage();
102111
}
103112
};
104113
};
105114

106-
export const removeLabel = id => {
115+
export const updateBadgeLabels = labelIds => {
107116
return async dispatch => {
117+
if (!labelIds.length) return;
108118
try {
109-
const response = await deleteLabelById(id);
110-
if (response) {
111-
dispatch(removeLabelOnSuccess(id));
112-
}
119+
const labelsFiltered = labelIds.filter(labelId => {
120+
return (
121+
labelId === LabelType.inbox.id ||
122+
labelId === LabelType.spam.id ||
123+
labelId === LabelType.draft.id
124+
);
125+
});
126+
const labels = await Promise.all(
127+
labelsFiltered.map(async labelId => {
128+
if (labelId === LabelType.inbox.id) {
129+
const rejectedLabelIds = [LabelType.spam.id, LabelType.trash.id];
130+
const unreadInbox = await getEmailsUnredByLabelId({
131+
labelId,
132+
rejectedLabelIds
133+
});
134+
const badgeInbox = unreadInbox.length;
135+
return {
136+
id: String(labelId),
137+
badge: badgeInbox
138+
};
139+
} else if (labelId === LabelType.spam.id) {
140+
const unreadSpam = await getEmailsUnredByLabelId({
141+
labelId
142+
});
143+
const badgeSpam = unreadSpam.length;
144+
return {
145+
id: String(labelId),
146+
badge: badgeSpam
147+
};
148+
} else if (labelId === LabelType.draft.id) {
149+
const badgeDraft = await getEmailsCounterByLabelId(labelId);
150+
return {
151+
id: String(labelId),
152+
badge: badgeDraft[0].count
153+
};
154+
}
155+
})
156+
);
157+
dispatch(updateBadgeLabelsSuccess(labels));
113158
} catch (e) {
114-
// TO DO
159+
sendUpdateLabelsErrorMessage();
115160
}
116161
};
117162
};
163+
export const updateBadgeLabelsSuccess = labelIds => {
164+
return {
165+
type: Label.UPDATE_BADGE_LABELS,
166+
labelIds
167+
};
168+
};
118169

119-
export const removeLabelOnSuccess = labelId => {
170+
export const updateLabelSuccess = label => {
120171
return {
121-
type: Label.REMOVE_SUCCESS,
122-
labelId
172+
type: Label.UPDATE,
173+
label
123174
};
124175
};

0 commit comments

Comments
 (0)