jam-cloud/web/app/assets/javascripts/react-components/stores/BroadcastStore.js.coffee

159 lines
5.1 KiB
CoffeeScript

$ = jQuery
context = window
logger = context.JK.logger
broadcastActions = @BroadcastActions
rest = context.JK.Rest()
broadcastActions.load.listenAndPromise(rest.getBroadcastNotification);
BroadcastStore = Reflux.createStore(
{
listenables: broadcastActions
currentSession: null
currentLesson: null
broadcast: null
currentLessonTimer: null
teacherFault: false
isJamClass: false
init: ->
this.listenTo(context.AppStore, this.onAppInit)
this.listenTo(context.SessionStore, this.onSessionChange)
this.listenTo(context.NavStore, this.onNavChange)
onAppInit: (@app) ->
@lessonUtils = context.JK.LessonUtils
lessonTick: () ->
@timeManagement()
@changed()
timeManagement: () ->
lastCheck = $.extend({}, @currentLesson)
lessonSession = @currentLesson
lessonSession.until = @lessonUtils.getTimeRemaining(lessonSession.scheduled_start)
if lessonSession.until.total < 0
# we are past the start time
if lessonSession.until.total > -(10 * 60 * 1000) # 10 minutes
lessonSession.initialWindow = true
else
lessonSession.initialWindow = false
lessonSession.beforeSession = false
else
# we are before the due time
lessonSession.initialWindow = false
lessonSession.beforeSession = true
# if we've transitioned to a new window
if !lessonSession.beforeSession && ((lastCheck.initialWindow || !lastCheck.initialWindow?) && !lessonSession.initialWindow)
logger.debug("BroadcastStore: lesson session 'initial window' transition")
rest.getLessonAnalysis({id: lessonSession.id}).done((response) => @lessonAnalysisDone(response)).fail(@app.ajaxError)
lessonAnalysisDone: (@analysis) ->
if !@currentLesson?
logger.debug("BroadcastStore: ignoring lessonAnalysisDone")
return
if @analysis.status == 'completed'
logger.debug("BroadcastStore: lesson is over")
@currentLesson.completed = true
@currentLesson.success = @analysis.success
@clearTimer()
@changed()
else if @analysis.analysis.reason != 'teacher_fault'
logger.debug("BroadcastStore: not teacher fault; clearing lesson info")
@clearLesson()
else
logger.debug("BroadcastStore: teacher is at fault")
@teacherFault = true
@clearTimer()
@changed()
clearLesson: () ->
if @currentLesson?
@currentLesson = null
@clearTimer()
@teacherFault = false
@changed()
clearTimer: () ->
if @currentLessonTimer?
clearInterval(@currentLessonTimer)
@currentLessonTimer = null
onNavChange: (nav) ->
path = nav.screenPath.toLowerCase()
@isJamClass = path.indexOf('jamclass') > -1 || path.indexOf('teacher') > -1
@changed()
onSessionChange: (session) ->
@session = session
currentSession = session.session
if currentSession? && currentSession.lesson_session? && session.inSession()
@currentSession = currentSession
lessonSession = currentSession.lesson_session
# so that receivers can check type of info coming at them via one-way events
lessonSession.isLesson = true
if lessonSession.status == 'completed'
lessonSession.completed = true
lessonSession.success = lessonSession.success
#else
# rest.getLessonAnalysis({id: lessonSession.id}).done((response) => @lessonAnalysisDone(response)).fail(@app.ajaxError)
@currentLesson = lessonSession
@timeManagement()
logger.debug("BroadcastStore: currentLesson until: ", @currentLesson.until, lessonSession.scheduled_start)
if !@currentLessonTimer?
@currentLessonTimer = setInterval((() => @lessonTick()), 1000)
@changed()
else
@clearLesson()
onLoad: () ->
logger.debug("loading broadcast notification...")
onLoadCompleted: (response) ->
if response.id?
logger.debug("broadcast notification sync completed")
@broadcast = response
@changed()
onLoadFailed: (jqXHR) ->
if jqXHR.status != 404
logger.error("broadcast notification sync failed")
onHide: () ->
@broadcast = null
@changed()
changed: () ->
if @currentLesson?
@currentLesson.isStudent == @currentLesson.student_id == context.JK.currentUserId
@currentLesson.isTeacher = !@currentLesson.isStudent
@currentLesson.teacherFault = @teacherFault
@currentLesson.teacherPresent = @session.findParticipantByUserId(@currentLesson.teacher_id)
@currentLesson.studentPresent = @session.findParticipantByUserId(@currentLesson.student_id)
if (@currentLesson.teacherPresent? && @currentLesson.isStudent) || (@currentLesson.studentPresent? && @currentLesson.isTeacher)
# don't show anything if the other person is there
this.trigger(null)
else
this.trigger(@currentLesson)
else if @isJamClass
this.trigger({isJamClass: true})
else
this.trigger(@broadcast)
}
)
context.JK.Stores.Broadcast = BroadcastStore