Skip to content

Instantly share code, notes, and snippets.

@excid3
Created March 8, 2015 22:32
Show Gist options
  • Save excid3/a09a1464ecf5407db124 to your computer and use it in GitHub Desktop.
Save excid3/a09a1464ecf5407db124 to your computer and use it in GitHub Desktop.
class SignUpLink
constructor: ->
@setEvents()
setEvents: ->
$("#sign-up-link").on "click", @handleClick
handleClick: ->
$(".sign-up-form").show()
$(".sign-in-form").hide()
class SignInLink
constructor: ->
@setEvents()
setEvents: ->
$("#sign-in-link").on "click", @handleClick
handleClick: ->
$(".sign-up-form").hide()
$(".sign-in-form").show()
class SignUpForm
constructor: ->
@form = $(".sign-up-form")
@setEvents()
setEvents: ->
@form.on "submit", @handleSubmit
handleSubmit: (e) =>
e.preventDefault()
$.ajax
url: @form.attr("action")
data: @form.serialize()
dataType: "json"
method: "post"
success: @handleSuccess
error: @handleError
handleSuccess: (data, status, xhr) ->
if data.success
$("#register-or-sign-in").hide()
else
errors = $.map(data.errors, (message, i) -> ("<p>#{message}</p>"))
$("#registration_errors").html errors
$("#registration_errors").show()
handleErrors: (xhr, status, error) ->
console.log(error)
class SignInForm
constructor: ->
@form = $(".sign-in-form")
@setEvents()
setEvents: ->
@form.on "submit", @handleSubmit
handleSubmit: (e) =>
e.preventDefault()
$.ajax
url: @form.attr("action")
data: @form.serialize()
dataType: "json"
method: "post"
success: @handleSuccess
error: @handleError
handleSuccess: (data, status, xhr) ->
$("meta[name=csrf-token]").attr("content", data.authenticity_token)
$("#register-or-sign-in").hide()
handleError: (xhr, status, errors) ->
$("#session_errors").append("<p>Invalid email or password</p>")
$("#session_errors").show()
class RegisterOrSignUp
constructor: ->
new SignUpLink
new SignInLink
new SignUpForm
new SignInForm
jQuery ->
new RegisterOrSignUp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment