scala-news-reader
rss/atom news reader in scala
git clone https://9o.is/git/scala-news-reader.git
SignUp.scala
(4895B)
1 package com.joereader.snippet
2
3 import com.joereader._
4 import model._
5 import lib.Helper._
6 import config._
7 import SnipHelpers._
8
9 import net.liftweb._
10 import util.Helpers._
11 import http._
12 import SHtml._
13 import js._
14 import JsCmds._
15 import scala.xml.NodeSeq
16
17 // Requests variables sent from page to page during sign up
18 object EmailVar extends RequestVar("")
19
20 object BlogIdVar extends RequestVar("")
21
22 object VerifiedVar extends RequestVar(false)
23
24 /**
25 * Sign Up Wizard!
26 */
27 class SignUp {
28
29 var email = EmailVar.is
30 val blogId = BlogIdVar.is
31
32 /**
33 * First Step: Collect email from user to start wizard.
34 */
35 def setupEmail = {
36
37 def invitedMsg: JsCmd = S.notice(
38 "email-join-err",
39 "You've been invited already silly!!")
40
41 def continue(): JsCmd = {
42 var js: JsCmd = Noop
43 if (email.isEmail) {
44 if (User.findByEmail(email).isDefined)
45 js = invitedMsg & SetValById("email-join", "")
46 else {
47 BetaUser.createRecord.id(email).save
48 js = S.redirectTo(Site.signUp1.url, () => EmailVar(email))
49 }
50 }
51 else js = S.error("email-join-err", "Please enter a valid email.")
52 js
53 }
54
55 if (SignUp.hideSignup)
56 "*" #> NodeSeq.Empty
57 else
58 "#signup-style *" #>
59 // @navbarHeight + @emailSignupHeight + fudge in less
60 "@media (min-width: 980px) {body{padding-top: 150px}}" &
61 "#fb-btn [href]" #> Site.facebookSignInSignIn.url &
62 "#email-signup-btn [onclick]" #> {
63 Show("email-join-container") & Hide("fb-btn") &
64 Show("join-btn") & Hide("email-signup-btn")
65 } &
66 "#email-join" #> text(email, email = _) &
67 "#join-btn" #> ajaxSubmit("Join Now for Free", continue)
68 }
69
70 /**
71 * Second Step: Check if he's a blog writer. If so,
72 * continue to next steps and allow him to setup and customize his blog.
73 */
74 def setupWriter = {
75 assert(email.isEmpty)
76
77 "#yes-button" #> ajaxSubmit("Yes", () =>
78 S.redirectTo(Site.signUp2.url, () => EmailVar(email))) &
79 "#no-button" #> ajaxSubmit("No ", () => S.redirectTo(Site.signUp7.url))
80 }
81
82 // Third step is blog verification which is handled by
83 // Verification.render.
84 def setupVerification = {
85 assert(email.isEmpty)
86 "*" #> NodeSeq.Empty // do nothing only check if theres an email
87 }
88
89 /**
90 * Fourth Step: Set the categories that the user writes for his blog.
91 */
92 def setupCategories = {
93 val user = User.currentUser
94 val blog = Blog.findByStringId(blogId)
95 val verified = VerifiedVar.is
96
97 assert(!verified || user.isEmpty || blog.isEmpty)
98
99 def continue(): JsCmd = S.redirectTo(Site.signUp4.url, () => {
100 BlogIdVar(blogId)
101 VerifiedVar(verified)
102 })
103
104 "#blogname" #> blog.map(_.name.is) &
105 "#categories-area" #>
106 Templates("templates-hidden" :: "parts" :: "categories" :: Nil).map {
107 ns =>
108 Site.categoriesLoc.requestValue(blog)
109 ns
110 } &
111 "#categories-continue" #> ajaxSubmit("Continue", continue)
112 }
113
114 /**
115 * Fifth Step: if the user is the owner of the blog, allow him to customize
116 * the blog page, else continue to step six.
117 */
118 def setupBlog = {
119 val user = User.currentUser
120 val blog = Blog.findByStringId(blogId)
121 val verified = VerifiedVar.is
122
123 assert(!verified || user.isEmpty || blog.isEmpty)
124
125 // skip to next page if this user is not owner
126 if (blog.exists {blog => User.currentUser.exists(blog.owner.isNot)})
127 S.redirectTo(Site.signUp5.url, () => VerifiedVar(verified))
128
129 def continue(): JsCmd =
130 S.redirectTo(Site.signUp5.url, () => VerifiedVar(verified))
131
132 "#blog-area" #>
133 Templates("templates-hidden" :: "parts" :: "blog-profile" :: Nil).map {
134 ns =>
135 Site.blogProfileLoc.requestValue(blog)
136 ns
137 } &
138 "#blog-continue" #> ajaxSubmit("Continue", continue)
139 }
140
141 /**
142 * Sixth Step: Customize the user's page.
143 */
144 def setupUser = {
145 val user = User.currentUser
146 val verified = VerifiedVar.is
147
148 assert(!verified || user.isEmpty)
149
150 def continue(): JsCmd =
151 S.redirectTo(Site.signUp6.url, () => VerifiedVar(verified))
152
153 "#user-area" #>
154 Templates("templates-hidden" :: "parts" :: "user-profile" :: Nil).map {
155 ns =>
156 Site.userProfileLoc.requestValue(user)
157 ns
158 } &
159 "#user-continue" #> ajaxSubmit("Continue", continue)
160 }
161
162 /**
163 * Seventh Step: Set the user's password.
164 */
165 def setupPassword = {
166 val user = User.currentUser
167 val verified = VerifiedVar.is
168
169 assert(!verified || user.isEmpty)
170 def continue(): JsCmd = S.redirectTo(Site.reader.url)
171
172 "#user-continue" #> ajaxSubmit("Continue", continue)
173 }
174
175 def assert(bool: Boolean) {
176 if (bool) S.redirectTo(Site.home.url)
177 }
178
179 }
180
181 object SignUp {
182 def hideSignup: Boolean = !Site.isMarketingPage(S.uri) || User.isLoggedIn
183 }