codecombat/app/views/modal/WizardSettingsModal.coffee

61 lines
1.9 KiB
CoffeeScript
Raw Normal View History

ModalView = require 'views/core/ModalView'
template = require 'templates/modal/wizard-settings-modal'
WizardLank = require 'lib/surface/WizardLank'
2014-01-03 13:32:13 -05:00
ThangType = require 'models/ThangType'
{me} = require 'core/auth'
forms = require 'core/forms'
User = require 'models/User'
2014-01-03 13:32:13 -05:00
2014-07-17 20:22:26 -04:00
module.exports = class WizardSettingsModal extends ModalView
2014-06-30 22:16:26 -04:00
id: 'wizard-settings-modal'
2014-01-03 13:32:13 -05:00
template: template
closesOnClickOutside: false
events:
'keyup #wizard-settings-name': -> @trigger 'nameChanged'
'click #wizard-settings-done': 'onWizardSettingsDone'
constructor: (options) ->
2014-03-23 21:55:44 -04:00
@onNameChange = _.debounce(@checkNameExists, 500)
@on 'nameChanged', @onNameChange
super options
2014-01-03 13:32:13 -05:00
afterRender: ->
WizardSettingsView = require 'views/account/WizardSettingsView'
view = new WizardSettingsView()
@insertSubView view
super()
2014-01-03 13:32:13 -05:00
checkNameExists: =>
forms.clearFormAlerts(@$el)
name = $('#wizard-settings-name').val()
User.getUnconflictedName name, (newName) =>
forms.clearFormAlerts(@$el)
if name isnt newName
forms.setErrorToProperty @$el, 'name', 'This name is already taken so you won\'t be able to keep it.', true
onWizardSettingsDone: ->
2014-03-23 13:41:15 -04:00
me.set('name', $('#wizard-settings-name').val())
forms.clearFormAlerts(@$el)
res = me.validate()
if res?
forms.applyErrorsToForm(@$el, res)
return
2014-06-11 16:16:17 -04:00
res = me.patch()
return unless res
save = $('#save-button', @$el).text($.i18n.t('common.saving', defaultValue: 'Saving...'))
.addClass('btn-info').show().removeClass('btn-danger')
res.error =>
errors = JSON.parse(res.responseText)
2014-06-30 22:16:26 -04:00
console.warn 'Got errors saving user:', errors
return if @destroyed
forms.applyErrorsToForm(@$el, errors)
@disableModalInProgress(@$el)
res.success (model, response, options) =>
@hide() unless @destroyed
@enableModalInProgress(@$el)