You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OnboardingView.vue 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // import savesurveybyprofileid - call it on submit
  46. // paginate to save every steps answers
  47. export default {
  48. name: 'OnboardingView',
  49. components: {
  50. ...stepViews,
  51. SurveyCompleteView,
  52. },
  53. data: () => ({
  54. answered: {},
  55. aspectQuestions: [],
  56. responses: [],
  57. currentStep: 0,
  58. currentProfileId: null,
  59. survey: null,
  60. invalidResponse: false,
  61. userEmail: null,
  62. emailIsInCache: false,
  63. authenticator: {},
  64. }),
  65. async created() {
  66. this.survey = await surveyFactory.createSurvey()
  67. this.authenticator = new Authenticator()
  68. // TODO: Consider switch/case() depending on what tokens exist/are valid...
  69. sessionToken = this.grabCookie('siimee_session_onboarding')
  70. // if (!sessionToken) {
  71. // //
  72. // }
  73. accessToken = this.grabCookie('siimee_access_onboarding')
  74. // if (!accessToken) {
  75. // // blow up
  76. // }
  77. const sessionData = await this.authenticator.validateJwt(sessionToken)
  78. // NOTE: Left off here, INCOMPLETE, no ACCESS TOKEN yet, crazy amount of logic here...
  79. if (sessionData.isValid && !accessToken) {
  80. this.userEmail = sessionData.payload.email
  81. // this.emailIsRegistered
  82. this.emailIsInCache = await this.authenticator.checkEmailCache(
  83. this.userEmail,
  84. )
  85. }
  86. // TODO: EVERY ROUTE WE HIT AFTER THIS HAS TO BE AUTHENTICATED
  87. // ACCESS TOKEN WORKS
  88. if (this.emailIsInCache) {
  89. const user = await fetchUserByEmail(this.userEmail)
  90. const userId = user.user_id
  91. const profilesFromUserId = await fetchProfilesByUserId(userId)
  92. let profileId
  93. if (profilesFromUserId.length === 1) {
  94. profileId = profilesFromUserId[0].profile_id
  95. this.currentProfileId = profileId
  96. }
  97. const profile = await fetchProfileByProfileId(profileId)
  98. profile.responses.forEach(response => {
  99. this.responses.push({
  100. response_key_id: response.response_key_id,
  101. val: response.val,
  102. })
  103. })
  104. this.currentStep = this.responses.length + 3
  105. this.goToStep(this.currentStep)
  106. } else {
  107. // TODO: CHECK SESSION
  108. }
  109. },
  110. methods: {
  111. onSubmit() {
  112. console.log(JSON.stringify(this.answered))
  113. },
  114. async goToStep(num) {
  115. this.currentStep = num
  116. },
  117. // TODO: Rename this method, grab cookie from where?
  118. // grabStoredCookie(cookieKey)
  119. grabCookie(cookieKey) {
  120. const cookies = document.cookie
  121. .split('; ')
  122. .reduce((prev, current) => {
  123. const [name, ...value] = current.split('=')
  124. prev[name] = value.join('=')
  125. return prev
  126. }, {})
  127. const cookieVal =
  128. cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
  129. return cookieVal
  130. },
  131. async updateAnswers(payload) {
  132. if (payload) {
  133. // this.invalidResponse = false
  134. const k = payload.question.survey_stage
  135. this.answered[k] = payload.input
  136. if (!this.survey.validateAnswer(payload)) {
  137. this.invalidResponse = true
  138. return
  139. }
  140. // once validated, don't log password in answered object
  141. this.answered[k] = k === 'password' ? undefined : payload.input
  142. // formats initial responses for response table
  143. const response = {}
  144. response.response_key_id = payload.question.response_key_id
  145. response.val = payload.input
  146. this.responses.push(response)
  147. console.log('this.answered :=>', this.answered)
  148. console.log('this.responses :=>', this.responses)
  149. // sends latest survey response to db
  150. if (this.currentProfileId) {
  151. surveyFactory.addNewSurveyAnswer(
  152. this.responses[this.responses.length - 1],
  153. this.currentProfileId,
  154. )
  155. }
  156. if (k === 'aspects') return
  157. }
  158. if (this.currentStep > this.survey.steps.length) {
  159. this.onSubmit(this.answered)
  160. } else {
  161. this.goToStep(this.currentStep + 1)
  162. }
  163. },
  164. },
  165. }
  166. </script>
  167. <style lang="sass">
  168. .view--onboarding
  169. width: 100%
  170. max-width: 428px
  171. background-color: #fff
  172. color: #1F2024
  173. font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif
  174. margin: 0 auto
  175. article
  176. height: 100vh
  177. .answers
  178. text-align: center
  179. .w-button
  180. display: flex
  181. width: 315px
  182. height: 60px
  183. border-radius: 6
  184. background-color: #D5D5D5
  185. color: #1F2024
  186. margin: 11px auto
  187. font-weight: bold
  188. font-size: 16px
  189. text-transform: uppercase
  190. &.next-btn
  191. border-radius: 6px
  192. background-color: #5BA626
  193. color: #1F2024
  194. height: 50px
  195. width: 315px
  196. font-size: 18px
  197. padding: 7px
  198. .w-card
  199. background-color: #1F2024
  200. justify-content: center
  201. align-items: center
  202. width: 100%
  203. h3
  204. text-transform: uppercase
  205. text-align: center
  206. font-size: 28px
  207. font-weight: bold
  208. color: white
  209. margin-top: 88px
  210. p
  211. color: white
  212. font-size: 18px
  213. padding: 11px
  214. text-align: center
  215. margin: 22px auto
  216. font-weight: bold
  217. input
  218. display: flex
  219. width: 315px
  220. height: 60px
  221. padding: 11px
  222. border-radius: 6
  223. background-color: #D5D5D5
  224. color: #1F2024
  225. margin: 11px auto
  226. font-weight: bold
  227. font-size: 16px
  228. .w-select
  229. padding: 11px
  230. color: #1F2024
  231. .search-type
  232. color: #1F2024
  233. height: 50px
  234. &.question
  235. p
  236. font-size: 18px
  237. text-align: left
  238. margin: 7px auto
  239. font-weight: normal
  240. section
  241. p
  242. margin: 0
  243. font-weight: bold
  244. text-transform: capitalize
  245. .w-radio__input
  246. &.primary
  247. background-color: #FFFFFF
  248. border: #BCC5D3 1px solid
  249. .w-card__content
  250. .w-button
  251. height: 50px
  252. background-color: #5BA626
  253. </style>