2016-02-19 02:00:11 +08:00
|
|
|
/** @babel */
|
2016-02-23 07:48:07 +08:00
|
|
|
import _ from 'underscore';
|
2016-02-19 02:00:11 +08:00
|
|
|
import {Actions, NylasAPI, AccountStore} from 'nylas-exports';
|
|
|
|
import {moveThreadsToSnooze} from './snooze-category-helpers';
|
|
|
|
import {PLUGIN_ID, PLUGIN_NAME} from './snooze-constants';
|
|
|
|
import SnoozeActions from './snooze-actions';
|
|
|
|
|
|
|
|
|
|
|
|
class SnoozeStore {
|
|
|
|
|
|
|
|
constructor(pluginId = PLUGIN_ID) {
|
|
|
|
this.pluginId = pluginId
|
|
|
|
|
|
|
|
this.unsubscribe = SnoozeActions.snoozeThreads.listen(this.onSnoozeThreads)
|
|
|
|
}
|
|
|
|
|
|
|
|
onSnoozeThreads = (threads, snoozeDate)=> {
|
|
|
|
const accounts = AccountStore.accountsForItems(threads)
|
|
|
|
const promises = accounts.map((acc)=> {
|
|
|
|
return NylasAPI.authPlugin(this.pluginId, PLUGIN_NAME, acc)
|
|
|
|
})
|
|
|
|
Promise.all(promises)
|
|
|
|
.then(()=> {
|
|
|
|
return moveThreadsToSnooze(threads)
|
|
|
|
})
|
2016-02-23 07:48:07 +08:00
|
|
|
.then((updatedThreadsByAccountId)=> {
|
|
|
|
_.each(updatedThreadsByAccountId, (update)=> {
|
|
|
|
const {updatedThreads, snoozeCategoryId, returnCategoryId} = update;
|
|
|
|
Actions.setMetadata(updatedThreads, this.pluginId, {snoozeDate, snoozeCategoryId, returnCategoryId})
|
|
|
|
})
|
2016-02-19 02:00:11 +08:00
|
|
|
})
|
|
|
|
.catch((error)=> {
|
2016-02-24 09:51:10 +08:00
|
|
|
NylasEnv.reportError(error);
|
|
|
|
NylasEnv.showErrorDialog(`Sorry, we were unable to save your snooze settings. ${error.message}`);
|
|
|
|
});
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
this.unsubscribe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SnoozeStore;
|