codecombat/app/views/modal/wizard_settings_modal.coffee

60 lines
1.8 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
View = require 'views/kinds/ModalView'
template = require 'templates/modal/wizard_settings'
WizardSprite = require 'lib/surface/WizardSprite'
ThangType = require 'models/ThangType'
{me} = require 'lib/auth'
2014-06-30 22:16:26 -04:00
forms = require 'lib/forms'
User = require 'models/User'
2014-01-03 13:32:13 -05:00
module.exports = class WizardSettingsModal extends View
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/wizard_settings_view'
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
forms.applyErrorsToForm(@$el, errors)
@disableModalInProgress(@$el)
res.success (model, response, options) =>
@hide()
@enableModalInProgress(@$el)