codecombat/app/views/admin/TrialRequestsView.coffee
Matt Lott d7cddcb136 Teacher trial subscription form
Add a teacher survey form for applying for a free trial subscription
for evaluation purposes.
Add an admin trial requests review page, where admins can approve/deny
requests.
2015-06-05 06:48:19 -07:00

66 lines
2.3 KiB
CoffeeScript

RootView = require 'views/core/RootView'
template = require 'templates/admin/trial-requests'
CocoCollection = require 'collections/CocoCollection'
TrialRequest = require 'models/TrialRequest'
module.exports = class TrialRequestsView extends RootView
id: 'admin-trial-requests-view'
template: template
events:
'click .btn-approve': 'onClickApprove'
'click .btn-deny': 'onClickDeny'
constructor: (options) ->
super options
if me.isAdmin()
sortRequests = (a, b) ->
statusA = a.get('status')
statusB = b.get('status')
if statusA is 'submitted' and statusB is 'submitted'
if a.get('_id') < b.get('_id')
-1
else
1
else if statusA is 'submitted'
-1
else if statusB is 'submitted'
1
else if not b.get('reviewDate') or a.get('reviewDate') > b.get('reviewDate')
-1
else
1
@trialRequests = new CocoCollection([], { url: '/db/trial.request', model: TrialRequest, comparator: sortRequests })
@supermodel.loadCollection(@trialRequests, 'trial-requests', {cache: false})
getRenderData: ->
context = super()
context.trialRequests = @trialRequests?.models ? []
context
onClickApprove: (e) ->
trialRequestData = $(e.target).data('trial-request')
trialRequest = _.find @trialRequests.models, (a) -> a.get('_id') is trialRequestData._id
unless trialRequest
console.error 'Could not find trial request model for', trialRequestData
return
trialRequest.set('status', 'approved')
trialRequest.patch
error: (model, response, options) =>
console.error 'Error patching trial request', response
success: (model, response, options) =>
@render?()
onClickDeny: (e) ->
trialRequestData = $(e.target).data('trial-request')
trialRequest = _.find @trialRequests.models, (a) -> a.get('_id') is trialRequestData._id
unless trialRequest
console.error 'Could not find trial request model for', trialRequestData
return
return unless window.confirm("Deny #{trialRequest.get('properties').email}?")
trialRequest.set('status', 'denied')
trialRequest.patch
error: (model, response, options) =>
console.error 'Error patching trial request', response
success: (model, response, options) =>
@render?()