mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-04-02 16:21:01 -04:00
anonymous users are now warned if their new name is already chosen
This commit is contained in:
parent
1a587b607f
commit
a9074ce603
4 changed files with 67 additions and 19 deletions
app
test/demo/views/modals
|
@ -18,7 +18,7 @@ module.exports = class CocoRouter extends Backbone.Router
|
|||
|
||||
# Direct links
|
||||
'test/*subpath': go('TestView')
|
||||
'demo/*subpath': go('DemoView')
|
||||
'demo(/*subpath)': go('DemoView')
|
||||
'play/ladder/:levelID': go('play/ladder/ladder_view')
|
||||
'play/ladder': go('play/ladder_home')
|
||||
|
||||
|
|
|
@ -9,12 +9,12 @@ module.exports.formToObject = (el) ->
|
|||
|
||||
obj
|
||||
|
||||
module.exports.applyErrorsToForm = (el, errors) ->
|
||||
module.exports.applyErrorsToForm = (el, errors, warning=false) ->
|
||||
errors = [errors] if not $.isArray(errors)
|
||||
missingErrors = []
|
||||
for error in errors
|
||||
if error.dataPath
|
||||
prop = error.dataPath[1..]
|
||||
console.log prop
|
||||
message = error.message
|
||||
|
||||
else
|
||||
|
@ -23,16 +23,28 @@ module.exports.applyErrorsToForm = (el, errors) ->
|
|||
message = error.message if error.formatted
|
||||
prop = error.property
|
||||
|
||||
input = $("[name='#{prop}']", el)
|
||||
if not input.length
|
||||
missingErrors.push(error)
|
||||
continue
|
||||
formGroup = input.closest('.form-group')
|
||||
formGroup.addClass 'has-error'
|
||||
formGroup.append($("<span class='help-block error-help-block'>#{message}</span>"))
|
||||
return missingErrors
|
||||
setErrorToProperty el, prop, message, warning
|
||||
|
||||
module.exports.setErrorToField = setErrorToField = (el, message, warning=false) ->
|
||||
formGroup = el.closest('.form-group')
|
||||
unless formGroup.length
|
||||
return console.error "#{el} did not contain a form group"
|
||||
|
||||
kind = if warning then 'warning' else 'error'
|
||||
formGroup.addClass "has-#{kind}"
|
||||
formGroup.append $("<span class='help-block #{kind}-help-block'>#{message}</span>")
|
||||
|
||||
module.exports.setErrorToProperty = setErrorToProperty = (el, property, message, warning=false) ->
|
||||
input = $("[name='#{property}']", el)
|
||||
unless input.length
|
||||
return console.error "#{property} not found in #{el}"
|
||||
|
||||
setErrorToField input, message, warning
|
||||
|
||||
module.exports.clearFormAlerts = (el) ->
|
||||
$('.has-error', el).removeClass('has-error')
|
||||
$('.has-warning', el).removeClass('has-warning')
|
||||
$('.alert.alert-danger', el).remove()
|
||||
$('.alert.alert-warning', el).remove()
|
||||
el.find('.help-block.error-help-block').remove()
|
||||
el.find('.help-block.warning-help-block').remove()
|
||||
|
|
|
@ -10,14 +10,15 @@ module.exports = class WizardSettingsModal extends View
|
|||
template: template
|
||||
closesOnClickOutside: false
|
||||
|
||||
events:
|
||||
'keyup #wizard-settings-name': -> @trigger 'nameChanged'
|
||||
'click #wizard-settings-done': 'onWizardSettingsDone'
|
||||
|
||||
constructor: (options) ->
|
||||
@onNameChange = _.debounce(@checkNameExists, 500)
|
||||
@on 'nameChanged', @onNameChange
|
||||
super options
|
||||
|
||||
events:
|
||||
'keyup #wizard-settings-name': 'onNameChange'
|
||||
'click #wizard-settings-done': 'onWizardSettingsDone'
|
||||
|
||||
afterRender: ->
|
||||
WizardSettingsView = require 'views/account/wizard_settings_view'
|
||||
view = new WizardSettingsView()
|
||||
|
@ -27,10 +28,13 @@ module.exports = class WizardSettingsModal extends View
|
|||
checkNameExists: =>
|
||||
forms.clearFormAlerts(@$el)
|
||||
name = $('#wizard-settings-name').val()
|
||||
success = (id) =>
|
||||
forms.clearFormAlerts(@$el)
|
||||
forms.applyErrorsToForm(@$el, {property: 'name', message: 'is already taken'}) if id and id isnt me.id
|
||||
$.ajax("/db/user/#{name}/nameToID", {success: success})
|
||||
$.ajax "/auth/name/#{name}",
|
||||
success: (data) =>
|
||||
forms.clearFormAlerts(@$el)
|
||||
statusCode: 409: (data) =>
|
||||
response = JSON.stringify data.responseText
|
||||
#forms.applyErrorsToForm(@$el, {dataPath: ['/', 'name'], message: 'This name is already taken so you won\'t be able to keep it.'}, true)
|
||||
forms.setErrorToProperty @$el, 'name', 'This name is already taken so you won\'t be able to keep it.', true
|
||||
|
||||
onWizardSettingsDone: ->
|
||||
me.set('name', $('#wizard-settings-name').val())
|
||||
|
|
32
test/demo/views/modals/WizardSettingsModal.demo.coffee
Normal file
32
test/demo/views/modals/WizardSettingsModal.demo.coffee
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue