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

129 lines
3.5 KiB
CoffeeScript

$ = jQuery
context = window
logger = context.JK.logger
rest = context.JK.Rest()
AttachmentActions = @AttachmentActions
@AttachmentStore = Reflux.createStore(
{
listenables: AttachmentActions
lessonId: null
uploading: false
init: ->
# Register with the app store to get @app
this.listenTo(context.AppStore, this.onAppInit)
onAppInit: (@app) ->
@ui = new context.JK.UIHelper(@app);
recordingsSelected: (recordings) ->
logger.debug("recording selected", recordings)
onStartAttachRecording: (lessonId) ->
if @lessonId?
logger.warn("rejecting startAttachRecording attempt as currently busy")
return
@lessonId = lessonId
@ui.launchRecordingSelectorDialog([], (recordings) =>
@recordingsSelected(recordings)
)
@change()
onStartAttachNotation: (lessonId) ->
if @lessonId?
logger.warn("rejecting onStartAttachNotation attempt as currently busy")
return
@lessonId = lessonId
logger.debug("notation upload started")
@triggerNotation()
@change()
onStartAttachAudio: (lessonId) ->
if @lessonId?
logger.warn("rejecting onStartAttachAudio attempt as currently busy")
return
@lessonId = lessonId
logger.debug("audio upload started")
@triggerAudio()
@changed()
triggerNotation: () ->
if !@attachNotationBtn?
@attachNotationBtn = $('input.attachment-notation').eq(0)
@attachNotationBtn.trigger('click')
triggerAudio: () ->
if !@attachAudioBtn?
@attachAudioBtn = $('input.attachment-audio').eq(0)
@attachAudioBtn.trigger('click')
onUploadNotations: (notations, doneCallback, failCallback) ->
logger.debug("beginning upload of notations")
@uploading = true
@changed()
formData = new FormData()
maxExceeded = false;
$.each(notations, (i, file) => (
max = 10 * 1024 * 1024;
if file.size > max
maxExceeded = true
return false
formData.append('files[]', file)
))
if maxExceeded
@app.notify({
title: "Maximum Music Notation Size Exceeded",
text: "You can only upload files up to 10 megabytes in size."
})
failCallback()
@uploading = false
@changed()
return
formData.append('client_id', app.clientId)
formData.append('lesson_session_id', @lessonid);
rest.uploadMusicNotations(formData)
.done((response) => @doneUploadingNotatations(notations, response))
.fail((jqXHR) => @failUploadingNotations(jqXHR))
doneUploadingNotatations: (notations, response) ->
error_files = [];
$.each(response, (i, music_notation) => (
if music_notation.errors
error_files.push(notations[i].name)
)
)
if error_files.length > 0
failCallback()
@app.notifyAlert("Failed to upload notations.", error_files.join(', '));
else
doneCallback()
failUploadingNotations: (jqXHR) ->
if jqXHR.status == 413
# the file is too big. Let the user know.
# This should happen when they select the file, but a misconfiguration on the server could cause this.
@app.notify({
title: "Maximum Music Notation Size Exceeded",
text: "You can only upload files up to 10 megabytes in size."
})
else
@app.notifyServerError(jqXHR, "Unable to upload music notations");
changed: () ->
this.trigger({lessonId: @lessonId, uploading: @uploading})
}
)