Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OnboardingView.vue 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template lang="pug">
  2. main.view--onboarding
  3. article(
  4. style='display: flex; flex-direction: column; align-items: center'
  5. v-if='currentStep !== survey.steps.length'
  6. )
  7. .answers(v-for='(value, key) in answered')
  8. span(v-if='key == "name" && value && currentStep == 2') Hi {{ value }}!
  9. br
  10. .step(v-for='(step, i) in survey.steps')
  11. component(
  12. :is='step.component'
  13. :question='step'
  14. :answered='answered'
  15. :responses='responses'
  16. :survey='survey'
  17. :currentStep='currentStep'
  18. :surveyStepsCount='survey.steps.length'
  19. @handle-submit='onSubmit'
  20. @update-answers='updateAnswers'
  21. v-if='step && currentStep == i'
  22. )
  23. .invalidResponseMessage(
  24. style='text-align: center'
  25. v-if='invalidResponse'
  26. )
  27. p {{ survey.steps[currentStep].invalidInputPrompt }}
  28. footer
  29. p(v-if='currentStep != 0') You have completed: {{ currentStep }} / {{ survey.steps.length }} survey steps
  30. article(v-else)
  31. SurveyCompleteView(:answers='answered' :surveySteps='survey.steps')
  32. </template>
  33. <script>
  34. import { Authenticator } from '../services/auth.service.js'
  35. import { fetchUserByEmail } from '../services/user.service.js'
  36. import {
  37. fetchProfilesByUserId,
  38. fetchProfileByProfileId,
  39. } from '../services/profile.service.js'
  40. import { surveyFactory } from '@/utils'
  41. import stepViews from '@/components/onboarding'
  42. import SurveyCompleteView from './SurveyCompleteView.vue'
  43. let sessionToken = null
  44. let accessToken = null
  45. let currentProfileId = null
  46. export default {
  47. name: 'OnboardingView',
  48. components: {
  49. ...stepViews,
  50. SurveyCompleteView,
  51. },
  52. data: () => ({
  53. answered: {},
  54. aspectQuestions: [],
  55. responses: [],
  56. currentStep: 0,
  57. survey: null,
  58. invalidResponse: false,
  59. authenticator: {},
  60. }),
  61. async created() {
  62. this.survey = await surveyFactory.createSurvey()
  63. this.authenticator = new Authenticator()
  64. // TODO: Note that all this try/catch can be in a function instead,
  65. // since it has to be done on created() and every step after 6...
  66. sessionToken = this.grabStoredCookie('siimee_session')
  67. accessToken = this.grabStoredCookie('siimee_access')
  68. try {
  69. await this.verifySessionToken(sessionToken)
  70. const sessionData = await this.verifyAccessToken(accessToken)
  71. await this.isEmailInRegistry(sessionData.payload.email)
  72. const userId = await this.grabUserIdByEmail(
  73. sessionData.payload.email,
  74. )
  75. const profileId = await this.grabProfileIdByUserId(userId)
  76. // TODO: consider different implementation once
  77. // updateAnswers() no longer relies on this
  78. currentProfileId = profileId
  79. this.responses = await this.grabResponsesByProfileId(profileId)
  80. this.currentStep = this.responses.length + 3
  81. this.goToStep(this.currentStep)
  82. } catch (err) {
  83. console.error('ERROR :=>', err)
  84. this.goToStep(0)
  85. }
  86. },
  87. methods: {
  88. onSubmit() {
  89. console.log(JSON.stringify(this.answered))
  90. },
  91. async goToStep(num) {
  92. this.currentStep = num
  93. },
  94. grabStoredCookie(cookieKey) {
  95. const cookies = document.cookie
  96. .split('; ')
  97. .reduce((prev, current) => {
  98. const [name, ...value] = current.split('=')
  99. prev[name] = value.join('=')
  100. return prev
  101. }, {})
  102. const cookieVal =
  103. cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
  104. return cookieVal
  105. },
  106. async verifySessionToken(sessionToken) {
  107. if (!sessionToken) {
  108. console.warn('WARNING :=> sessionToken is not defined')
  109. } else return await this.validateToken(sessionToken)
  110. },
  111. async verifyAccessToken(accessToken) {
  112. if (!accessToken) {
  113. console.warn('WARNING :=> accessToken is not defined')
  114. } else return await this.validateToken(accessToken)
  115. },
  116. async validateToken(token) {
  117. const validatedToken = await this.authenticator.validateSession(
  118. token,
  119. )
  120. if (validatedToken.error) {
  121. throw new Error(validatedToken.error)
  122. } else {
  123. return validatedToken
  124. }
  125. },
  126. async isEmailInRegistry(email) {
  127. const emailIsInRegistry =
  128. await this.authenticator.checkIfEmailIsRegistered(email)
  129. if (!emailIsInRegistry) {
  130. throw new Error('Email Is NOT in Registry!')
  131. } else return emailIsInRegistry // true
  132. },
  133. async grabUserIdByEmail(email) {
  134. const user = await fetchUserByEmail(email)
  135. if (!user) {
  136. throw new Error('User NOT found by email')
  137. } else return user.user_id
  138. },
  139. async grabProfileIdByUserId(userId) {
  140. const profilesFromUserId = await fetchProfilesByUserId(userId)
  141. if (profilesFromUserId.length === 1) {
  142. return profilesFromUserId[0].profile_id
  143. } else {
  144. // TODO: Refactor once more is known on users with multiple profiles
  145. console.error(
  146. 'ERROR :=> Multiple Profiles for this User ID',
  147. profilesFromUserId,
  148. )
  149. throw new Error('Multiple Profiles for this User ID')
  150. }
  151. },
  152. async grabProfileByProfileId(profileId) {
  153. const profile = await fetchProfileByProfileId(profileId)
  154. if (!profile) {
  155. throw new Error(`No Profile Found for profileId ${profileId}`)
  156. } else {
  157. return profile
  158. }
  159. },
  160. async grabResponsesByProfileId(profileId) {
  161. const responses = []
  162. const profile = await this.grabProfileByProfileId(profileId)
  163. if (!profile.responses.length) {
  164. throw new Error(`No Responses Found for profileId ${profileId}`)
  165. } else {
  166. profile.responses.forEach(response => {
  167. responses.push({
  168. response_key_id: response.response_key_id,
  169. val: response.val,
  170. })
  171. })
  172. return responses
  173. }
  174. },
  175. async updateAnswers(payload) {
  176. if (payload) {
  177. // this.invalidResponse = false
  178. const k = payload.question.survey_stage
  179. this.answered[k] = payload.input
  180. if (!this.survey.validateAnswer(payload)) {
  181. this.invalidResponse = true
  182. return
  183. }
  184. // once validated, don't log password in answered object
  185. this.answered[k] = k === 'password' ? undefined : payload.input
  186. // formats initial responses for response table
  187. const response = {}
  188. response.response_key_id = payload.question.response_key_id
  189. response.val = payload.input
  190. this.responses.push(response)
  191. console.log('this.answered :=>', this.answered)
  192. console.log('this.responses :=>', this.responses)
  193. // sends latest survey response to db
  194. if (currentProfileId) {
  195. surveyFactory.addNewSurveyAnswer(
  196. this.responses[this.responses.length - 1],
  197. this.currentProfileId,
  198. )
  199. }
  200. if (k === 'aspects') return
  201. }
  202. if (this.currentStep > this.survey.steps.length) {
  203. this.onSubmit(this.answered)
  204. } else {
  205. this.goToStep(this.currentStep + 1)
  206. }
  207. },
  208. },
  209. }
  210. </script>
  211. <style lang="sass">
  212. .view--onboarding
  213. width: 100%
  214. max-width: 428px
  215. background-color: #fff
  216. color: #1F2024
  217. font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif
  218. margin: 0 auto
  219. article
  220. height: 100vh
  221. .answers
  222. text-align: center
  223. .w-button
  224. display: flex
  225. width: 315px
  226. height: 60px
  227. border-radius: 6
  228. background-color: #D5D5D5
  229. color: #1F2024
  230. margin: 11px auto
  231. font-weight: bold
  232. font-size: 16px
  233. text-transform: uppercase
  234. &.next-btn
  235. border-radius: 6px
  236. background-color: #5BA626
  237. color: #1F2024
  238. height: 50px
  239. width: 315px
  240. font-size: 18px
  241. padding: 7px
  242. .w-card
  243. background-color: #1F2024
  244. justify-content: center
  245. align-items: center
  246. width: 100%
  247. h3
  248. text-transform: uppercase
  249. text-align: center
  250. font-size: 28px
  251. font-weight: bold
  252. color: white
  253. margin-top: 88px
  254. p
  255. color: white
  256. font-size: 18px
  257. padding: 11px
  258. text-align: center
  259. margin: 22px auto
  260. font-weight: bold
  261. input
  262. display: flex
  263. width: 315px
  264. height: 60px
  265. padding: 11px
  266. border-radius: 6
  267. background-color: #D5D5D5
  268. color: #1F2024
  269. margin: 11px auto
  270. font-weight: bold
  271. font-size: 16px
  272. .w-select
  273. padding: 11px
  274. color: #1F2024
  275. .search-type
  276. color: #1F2024
  277. height: 50px
  278. &.question
  279. p
  280. font-size: 18px
  281. text-align: left
  282. margin: 7px auto
  283. font-weight: normal
  284. section
  285. p
  286. margin: 0
  287. font-weight: bold
  288. text-transform: capitalize
  289. .w-radio__input
  290. &.primary
  291. background-color: #FFFFFF
  292. border: #BCC5D3 1px solid
  293. .w-card__content
  294. .w-button
  295. height: 50px
  296. background-color: #5BA626
  297. </style>