mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-02-17 08:50:58 -05:00
Add PurchaseCoursesView
This commit is contained in:
parent
66b7bba09b
commit
b36bf7b171
3 changed files with 127 additions and 0 deletions
|
@ -68,6 +68,7 @@ module.exports = class CocoRouter extends Backbone.Router
|
|||
'courses': go('courses/CoursesView')
|
||||
'courses/students': go('courses/StudentCoursesView')
|
||||
'courses/teachers': go('courses/TeacherCoursesView')
|
||||
'courses/purchase': go('courses/PurchaseCoursesView')
|
||||
'courses/enroll(/:courseID)': go('courses/CourseEnrollView')
|
||||
'courses/:courseID(/:courseInstanceID)': go('courses/CourseDetailsView')
|
||||
|
||||
|
|
41
app/templates/courses/purchase-courses-view.jade
Normal file
41
app/templates/courses/purchase-courses-view.jade
Normal file
|
@ -0,0 +1,41 @@
|
|||
extends /templates/base
|
||||
|
||||
block content
|
||||
|
||||
if view.state === 'purchasing'
|
||||
p.text-center Purchasing...
|
||||
.progress.progress-striped.active
|
||||
.progress-bar(style="width: 100%")
|
||||
|
||||
else if view.state === 'purchased'
|
||||
p Thank you for your purchase! You can now assign (more) students to paid courses.
|
||||
|
||||
p.text-center
|
||||
a(href="/courses/teachers") Return to course management.
|
||||
|
||||
else
|
||||
h3.text-center Purchase Courses for Students
|
||||
|
||||
if view.state === 'error'
|
||||
.alert.alert-danger= view.stateMessage
|
||||
|
||||
.form-horizontal
|
||||
.form-group
|
||||
label.col-sm-3.control-label Students
|
||||
.col-sm-6
|
||||
input#students-input.form-control(
|
||||
placeholder='<number of seats>'
|
||||
value=view.numberOfStudents
|
||||
type='number'
|
||||
)
|
||||
.help-block Each student will have access to all courses.
|
||||
|
||||
#price-form-group.form-group
|
||||
label.col-sm-3.control-label Price
|
||||
.col-sm-6
|
||||
.form-control-static
|
||||
| #{view.getPriceString()} ($#{view.pricePerStudent.toFixed(2)} per student)
|
||||
|
||||
.form-group
|
||||
.col-sm-offset-3.col-sm-10
|
||||
button#purchase-btn.btn.btn-primary Purchase
|
85
app/views/courses/PurchaseCoursesView.coffee
Normal file
85
app/views/courses/PurchaseCoursesView.coffee
Normal file
|
@ -0,0 +1,85 @@
|
|||
app = require 'core/application'
|
||||
AuthModal = require 'views/core/AuthModal'
|
||||
CocoCollection = require 'collections/CocoCollection'
|
||||
Course = require 'models/Course'
|
||||
RootView = require 'views/core/RootView'
|
||||
stripeHandler = require 'core/services/stripe'
|
||||
template = require 'templates/courses/purchase-courses-view'
|
||||
utils = require 'core/utils'
|
||||
|
||||
module.exports = class PurchaseCoursesView extends RootView
|
||||
id: 'purchase-courses-view'
|
||||
template: template
|
||||
numberOfStudents: 30
|
||||
pricePerStudent: 4
|
||||
|
||||
initialize: (options) ->
|
||||
@listenTo stripeHandler, 'received-token', @onStripeReceivedToken
|
||||
super(options)
|
||||
|
||||
events:
|
||||
'input #students-input': 'onInputStudentsInput'
|
||||
'click #purchase-btn': 'onClickPurchaseButton'
|
||||
|
||||
getPriceString: -> '$' + (@getPrice()).toFixed(2)
|
||||
getPrice: -> @pricePerStudent * @numberOfStudents
|
||||
|
||||
onInputStudentsInput: ->
|
||||
@numberOfStudents = parseInt(@$('#students-input').val()) or 0
|
||||
@updatePrice()
|
||||
|
||||
updatePrice: ->
|
||||
@renderSelectors '#price-form-group'
|
||||
|
||||
onClickPurchaseButton: ->
|
||||
return @openModalView new AuthModal() if me.isAnonymous()
|
||||
if @numberOfStudents < 1 or not _.isFinite(@numberOfStudents)
|
||||
alert("Please enter the maximum number of students needed for your class.")
|
||||
return
|
||||
|
||||
@state = undefined
|
||||
@stateMessage = undefined
|
||||
@render()
|
||||
|
||||
# Show Stripe handler
|
||||
application.tracker?.trackEvent 'Started course prepaid purchase', {
|
||||
price: @pricePerStudent, students: @pricePerStudent}
|
||||
stripeHandler.open
|
||||
amount: @price
|
||||
description: "Full course access for #{@numberOfStudents} students"
|
||||
bitcoin: true
|
||||
alipay: if me.get('country') is 'china' or (me.get('preferredLanguage') or 'en-US')[...2] is 'zh' then true else 'auto'
|
||||
|
||||
onStripeReceivedToken: (e) ->
|
||||
@state = 'purchasing'
|
||||
@render?()
|
||||
console.log 'e', e
|
||||
|
||||
data =
|
||||
maxRedeemers: @numberOfStudents
|
||||
type: 'course'
|
||||
stripe:
|
||||
token: e.token.id
|
||||
timestamp: new Date().getTime()
|
||||
|
||||
$.ajax({
|
||||
url: '/db/prepaid/-/purchase',
|
||||
data: data,
|
||||
method: 'POST',
|
||||
context: @
|
||||
success: ->
|
||||
application.tracker?.trackEvent 'Finished course prepaid purchase', {price: @pricePerStudent, seats: @numberOfStudents}
|
||||
@state = 'purchased'
|
||||
@render?()
|
||||
|
||||
error: (jqxhr, textStatus, errorThrown) ->
|
||||
application.tracker?.trackEvent 'Failed course prepaid purchase', status: textStatus
|
||||
if jqxhr.status is 402
|
||||
@state = 'error'
|
||||
@stateMessage = arguments[2]
|
||||
else
|
||||
@state = 'error'
|
||||
@stateMessage = "#{jqxhr.status}: #{jqxhr.responseText}"
|
||||
@render?()
|
||||
})
|
||||
|
Loading…
Reference in a new issue