Mailspring/internal_packages/thread-snooze/lib/snooze-category-helpers.js
Juan Tejada e82278ab25 feat(snooze): Adds initial design pass and update snooze popover
Summary:
- Add FixedPopover, an absolutely positioned popover component to use for swipe snoozing:
  - This component needs to be finished, as its current behavior is primarily for the snooze plugin
- Updates popover.cjsx to properly render pointer for thesnooze popover for the `down` direction
- Adds new design assets
- Adds date input to snooze popover

Test Plan: - TODO

Reviewers: evan, bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2624
2016-02-23 20:03:42 -08:00

129 lines
3.6 KiB
JavaScript

/** @babel */
import _ from 'underscore';
import {
Actions,
Thread,
Category,
CategoryStore,
DatabaseStore,
AccountStore,
SyncbackCategoryTask,
TaskQueueStatusStore,
TaskFactory,
} from 'nylas-exports';
import {SNOOZE_CATEGORY_NAME} from './snooze-constants'
export function createSnoozeCategory(accountId, name = SNOOZE_CATEGORY_NAME) {
const category = new Category({
displayName: name,
accountId: accountId,
})
const task = new SyncbackCategoryTask({category})
Actions.queueTask(task)
return TaskQueueStatusStore.waitForPerformRemote(task).then(()=>{
return DatabaseStore.findBy(Category, {clientId: category.clientId})
.then((updatedCat)=> {
if (updatedCat && updatedCat.isSavedRemotely()) {
return Promise.resolve(updatedCat)
}
return Promise.reject(new Error('Could not create Snooze category'))
})
})
}
export function whenCategoriesReady() {
const categoriesReady = ()=> CategoryStore.categories().length > 0
if (!categoriesReady()) {
return new Promise((resolve)=> {
const unsubscribe = CategoryStore.listen(()=> {
if (categoriesReady()) {
unsubscribe()
resolve()
}
})
})
}
return Promise.resolve()
}
export function getSnoozeCategory(accountId, categoryName = SNOOZE_CATEGORY_NAME) {
return whenCategoriesReady()
.then(()=> {
const userCategories = CategoryStore.userCategories(accountId)
const category = _.findWhere(userCategories, {displayName: categoryName})
if (category) {
return Promise.resolve(category);
}
return createSnoozeCategory(accountId, categoryName)
})
}
export function getSnoozeCategoriesByAccount(accounts = AccountStore.accounts()) {
const categoriesByAccountId = {}
accounts.forEach(({id})=> {
if (categoriesByAccountId[id] != null) return;
categoriesByAccountId[id] = getSnoozeCategory(id)
})
return Promise.props(categoriesByAccountId)
}
export function groupProcessedThreadsByAccountId(categoriesByAccountId, threads) {
return DatabaseStore.modelify(Thread, _.pluck(threads, 'id')).then((updatedThreads)=> {
const threadsByAccountId = {}
updatedThreads.forEach((thread)=> {
const accId = thread.accountId
if (!threadsByAccountId[accId]) {
threadsByAccountId[accId] = {
updatedThreads: [thread],
snoozeCategoryId: categoriesByAccountId[accId].serverId,
returnCategoryId: CategoryStore.getInboxCategory(accId).serverId,
}
} else {
threadsByAccountId[accId].updatedThreads.push(thread);
}
});
return Promise.resolve(threadsByAccountId);
})
}
export function moveThreads(threads, categoriesByAccountId, {snooze} = {}) {
const inbox = CategoryStore.getInboxCategory
const snoozeCat = (accId)=> categoriesByAccountId[accId]
const tasks = TaskFactory.tasksForApplyingCategories({
threads,
categoriesToRemove: snooze ? inbox : snoozeCat,
categoryToAdd: snooze ? snoozeCat : inbox,
})
Actions.queueTasks(tasks)
const promises = tasks.map(task => TaskQueueStatusStore.waitForPerformRemote(task))
// Resolve with the updated threads
return (
Promise.all(promises).then(()=> {
return groupProcessedThreadsByAccountId(categoriesByAccountId, threads)
})
)
}
export function moveThreadsToSnooze(threads) {
return getSnoozeCategoriesByAccount()
.then((categoriesByAccountId)=> {
return moveThreads(threads, categoriesByAccountId, {snooze: true})
})
}
export function moveThreadsFromSnooze(threads) {
return getSnoozeCategoriesByAccount()
.then((categoriesByAccountId)=> {
return moveThreads(threads, categoriesByAccountId, {snooze: false})
})
}