Mailspring/internal_packages/calendar-bar/lib/calendar-bar-event-store.coffee
Evan Morikawa 4619871e8d refactor(utils): switch to regular underscore
Summary:
Fixes: T1334

remove final InboxApp references

move out all underscore-plus methods

Mass find and replace of underscore-plus

sed -i '' -- 's/underscore-plus/underscore/g' **/*.coffee
sed -i '' -- 's/underscore-plus/underscore/g' **/*.cjsx

Test Plan: edgehill --test

Reviewers: bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D1534
2015-05-19 16:06:59 -07:00

72 lines
1.9 KiB
CoffeeScript

Reflux = require 'reflux'
_ = require 'underscore'
{DatabaseStore,
NamespaceStore,
Actions,
Event,
Calendar,
NylasAPI} = require 'nylas-exports'
moment = require 'moment'
CalendarBarEventStore = Reflux.createStore
init: ->
@_setStoreDefaults()
@_registerListeners()
@_populate()
@trigger(@)
########### PUBLIC #####################################################
events: ->
@_events
range: ->
@_range
########### PRIVATE ####################################################
_setStoreDefaults: ->
@_events = []
_registerListeners: ->
@listenTo DatabaseStore, @_onDataChanged
@listenTo NamespaceStore, @_onNamespaceChanged
_populate: ->
@_range =
start: moment({hour: 0, milliseconds: -1}).valueOf() / 1000.0
end: moment({hour: 24, milliseconds: 1}).valueOf() / 1000.0
namespace = NamespaceStore.current()
return unless namespace
DatabaseStore.findAll(Event, namespaceId: namespace.id).where([
Event.attributes.end.greaterThan(@_range.start),
Event.attributes.start.lessThan(@_range.end)
]).order(Event.attributes.start.ascending()).then (events) =>
@_events = events
@trigger(@)
_refetchFromAPI: ->
namespace = NamespaceStore.current()
return unless namespace
# Trigger a request to the API
oneDayAgo = Math.round(moment({hour: 0, milliseconds: -1}).valueOf() / 1000.0)
DatabaseStore.findAll(Calendar, namespaceId: namespace.id).then (calendars) ->
calendars.forEach (calendar) ->
NylasAPI.getCollection(namespace.id, 'events', {calendar_id: calendar.id, ends_after: oneDayAgo})
# Inbound Events
_onNamespaceChanged: ->
@_refetchFromAPI()
@_populate()
_onDataChanged: (change) ->
if change.objectClass == Calendar.name
@_refetchFromAPI()
if change.objectClass == Event.name
@_populate()
module.exports = CalendarBarEventStore