@@ -9,28 +9,14 @@ import {
99 updateLabel as updateLabelDB ,
1010 deleteLabelById
1111} from '../utils/electronInterface' ;
12+ import { sendUpdateLabelsErrorMessage } from './../utils/electronEventInterface' ;
1213import { 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+
6150export 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+
93102export 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