<commit_before>Template.username.onCreated ->
	self = this
	self.username = new ReactiveVar

	Meteor.call 'getUsernameSuggestion', (error, username) ->
		self.username.set
			ready: true
			username: username
		Meteor.defer ->
			self.find('input').focus()

Template.username.helpers
	username: ->
		return Template.instance().username.get()

Template.username.events
	'submit #login-card': (event, instance) ->
		event.preventDefault()

		username = instance.username.get()
		username.empty = false
		username.error = false
		username.invalid = false
		instance.username.set(username)

		button = $(event.target).find('button.login')
		RocketChat.Button.loading(button)

		value = $("input").val().trim()
		if value is ''
			username.empty = true
			instance.username.set(username)
			RocketChat.Button.reset(button)
			return

		Meteor.call 'setUsername', value, (err, result) ->
			if err?
				if err.error is 'username-invalid'
					username.invalid = true
				else
					username.error = true
				username.username = value

			RocketChat.Button.reset(button)
			instance.username.set(username)

			if not err?
				Meteor.call 'joinDefaultChannels'<commit_msg>Send user to /index after username setup<commit_after>Template.username.onCreated ->
	self = this
	self.username = new ReactiveVar

	Meteor.call 'getUsernameSuggestion', (error, username) ->
		self.username.set
			ready: true
			username: username
		Meteor.defer ->
			self.find('input').focus()

Template.username.helpers
	username: ->
		return Template.instance().username.get()

Template.username.events
	'submit #login-card': (event, instance) ->
		event.preventDefault()

		username = instance.username.get()
		username.empty = false
		username.error = false
		username.invalid = false
		instance.username.set(username)

		button = $(event.target).find('button.login')
		RocketChat.Button.loading(button)

		value = $("input").val().trim()
		if value is ''
			username.empty = true
			instance.username.set(username)
			RocketChat.Button.reset(button)
			return

		Meteor.call 'setUsername', value, (err, result) ->
			if err?
				if err.error is 'username-invalid'
					username.invalid = true
				else
					username.error = true
				username.username = value

			RocketChat.Button.reset(button)
			instance.username.set(username)

			if not err?
				Meteor.call 'joinDefaultChannels'
				FlowRouter.go 'index'
{'fraction_non_alphanumeric': 0.12610340479192939, 'fraction_numerical': 0.0, 'mean_word_length': 3.675834970530452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4316275', 'n_tokens_mistral': 856, 'n_tokens_neox': 722, 'n_words': 155}