codecombat/app/views/admin/MainAdminView.coffee

62 lines
2.1 KiB
CoffeeScript
Raw Normal View History

{backboneFailure, genericFailure} = require 'core/errors'
RootView = require 'views/core/RootView'
2014-01-03 13:32:13 -05:00
template = require 'templates/admin'
module.exports = class MainAdminView extends RootView
2014-06-30 22:16:26 -04:00
id: 'admin-view'
2014-01-03 13:32:13 -05:00
template: template
lastUserSearchValue: ''
2014-02-26 17:14:43 -05:00
events:
'keyup': 'checkForFormSubmissionEnterPress'
2014-02-26 17:14:43 -05:00
'click #enter-espionage-mode': 'enterEspionageMode'
'click #user-search-button': 'searchForUser'
'click #increment-button': 'incrementUserAttribute'
checkForFormSubmissionEnterPress: (e) ->
if e.which is 13 and @$el.find('#espionage-name-or-email').val() isnt ''
@enterEspionageMode()
return
if @$el.find('#user-search').val() isnt @lastUserSearchValue
@searchForUser()
enterEspionageMode: ->
userNameOrEmail = @$el.find('#espionage-name-or-email').val().toLowerCase()
2014-02-26 17:14:43 -05:00
$.ajax
2014-06-30 22:16:26 -04:00
type: 'POST',
url: '/auth/spy'
data: {nameOrEmailLower: userNameOrEmail}
2014-02-26 17:14:43 -05:00
success: @espionageSuccess
error: @espionageFailure
2014-02-26 17:14:43 -05:00
espionageSuccess: (model) ->
window.location.reload()
espionageFailure: (jqxhr, status, error)->
2014-02-26 17:14:43 -05:00
console.log "There was an error entering espionage mode: #{error}"
searchForUser: ->
return @onSearchRequestSuccess [] unless @lastUserSearchValue = @$el.find('#user-search').val().toLowerCase()
$.ajax
type: 'POST',
url: '/db/user/-/admin_search'
data: {search: @lastUserSearchValue}
success: @onSearchRequestSuccess
error: @onSearchRequestFailure
onSearchRequestSuccess: (users) =>
result = ''
if users.length
result = ("<tr><td><code>#{user._id}</code></td><td>#{_.escape(user.name or 'Anoner')}</td><td>#{_.escape(user.email)}</td></tr>" for user in users)
result = "<table class=\"table\">#{result.join('\n')}</table>"
@$el.find('#user-search-result').html(result)
onSearchRequestFailure: (jqxhr, status, error) =>
return if @destroyed
console.warn "There was an error looking up #{@lastUserSearchValue}:", error
incrementUserAttribute: (e) ->
val = $('#increment-field').val()
me.set(val, me.get(val) + 1)
me.save()