Skip to content

Commit

Permalink
Improve how column meta is handled by the email stores.
Browse files Browse the repository at this point in the history
This fixes meta counts not switching back after searching and adds loading icons
when searching.
  • Loading branch information
Fizzadar committed May 1, 2019
1 parent 25cfc9c commit 50efe92
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
28 changes: 24 additions & 4 deletions kanmail/client/emails/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { messageThreader } from 'threading.js';

import requestStore from 'stores/request.js';

import { getColumnStore, getColumnStoreKeys } from 'stores/columns.js';
import {
getColumnStore,
getColumnStoreKeys,
getColumnMetaStore,
} from 'stores/columns.js';


export default class BaseEmails {
Expand All @@ -17,11 +21,10 @@ export default class BaseEmails {

constructor() {
this.active = false;

this.folderLocks = {};

// Map of account/messageId -> email message object
this.emails = {};
// Initialise emails/meta objects
this.reset()

// Map of acocunt/folder/uid -> email message object
this.accountFolderUidToEmail = {};
Expand All @@ -44,7 +47,18 @@ export default class BaseEmails {
*/

reset() {
// Map of account/messageId -> email message object
this.emails = {};
// Map of folder -> account -> meta
this.meta = {};
}

setMetaForAccountFolder(accountKey, folderName, meta) {
if (!this.meta[folderName]) {
this.meta[folderName] = {};
}

this.meta[folderName][accountKey] = meta;
}

addEmailsToAccountFolder(accountKey, folderName, emails) {
Expand Down Expand Up @@ -305,6 +319,7 @@ export default class BaseEmails {

_.each(getColumnStoreKeys(), columnName => {
const store = getColumnStore(columnName);
const metaStore = getColumnMetaStore(columnName);

let threads = folderEmails[columnName] || [];
threads = _.orderBy(threads, emails => {
Expand All @@ -319,7 +334,12 @@ export default class BaseEmails {
|| _.includes(ALIAS_FOLDERS, columnName)
);

// Push to the column and meta stores
store.setThreads(threads, forceUpdate);
_.each(
this.meta[columnName],
(meta, accountKey) => metaStore.setAccountMeta(accountKey, meta),
);
});

const renderTaken = (performance.now() - renderStart).toFixed(2);
Expand Down
8 changes: 4 additions & 4 deletions kanmail/client/emails/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class MainEmails extends BaseEmails {
`Sync emails in ${accountKey}/${folderName}`,
url, query,
).then(data => {
this.setMetaForAccountFolder(accountKey, folderName, data.meta);

let changed = false;

if (data.deleted_uids.length > 0) {
Expand All @@ -66,8 +68,6 @@ class MainEmails extends BaseEmails {
}

if (changed || options.forceProcess) {
const columnMetaStore = getColumnMetaStore(folderName);
columnMetaStore.setMeta(accountKey, data.meta);
this.processEmailChanges();
}
});
Expand Down Expand Up @@ -101,6 +101,8 @@ class MainEmails extends BaseEmails {
`/api/emails/${accountKey}/${folderName}`,
query,
).then(data => {
this.setMetaForAccountFolder(accountKey, folderName, data.meta);

let changed = false;

if (data.emails.length >= 0) {
Expand All @@ -114,8 +116,6 @@ class MainEmails extends BaseEmails {
}

if (changed || options.forceProcess) {
const columnMetaStore = getColumnMetaStore(folderName);
columnMetaStore.setMeta(accountKey, data.meta);
this.processEmailChanges();
}
});
Expand Down
10 changes: 7 additions & 3 deletions kanmail/client/emails/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ class SearchEmails extends BaseEmails {
}

getFolderEmails = (folderName, options={}) => {
const columnMetaStore = getColumnMetaStore(folderName);
columnMetaStore.setLoading(true);

const requests = [];

// For each account, search for matching emails
_.each(_.keys(settingsStore.props.accounts), accountKey => {
requests.push(this.searchEmails(accountKey, folderName, options))
});

return Promise.all(requests);
return Promise.all(requests)
.then(() => columnMetaStore.setLoading(false))
.catch(() => columnMetaStore.setLoading(false));
}

searchEmails(accountKey, folderName, options={}) {
Expand All @@ -49,8 +54,7 @@ class SearchEmails extends BaseEmails {
`Fetch emails from ${accountKey}/${folderName}`,
url, query,
).then(data => {
const columnMetaStore = getColumnMetaStore(folderName);
columnMetaStore.setMeta(accountKey, data.meta);
this.setMetaForAccountFolder(accountKey, folderName, data.meta);

let changed = false;

Expand Down
2 changes: 1 addition & 1 deletion kanmail/client/stores/columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ColumnMetaStore extends BaseStore {
};
}

setMeta(accountKey, meta) {
setAccountMeta(accountKey, meta) {
this.props.counts[accountKey] = meta.count;
// const counts = _.reduce(meta, (memo, data, accountKey) => {
// memo[accountKey] = data.count;
Expand Down

0 comments on commit 50efe92

Please sign in to comment.