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.

login.service.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { ref } from 'vue'
  2. import { fetchResponsesByProfileId, Chatter, StonkAlert } from '../services'
  3. import { surveyFactory } from '../utils'
  4. /**
  5. * Logged in profile state manager
  6. * Sort of a util and service hybrid
  7. */
  8. class Login {
  9. constructor() {
  10. this._loading = false
  11. // Make reactive with vue observer
  12. this.id = ref(null)
  13. this.responses = []
  14. this.tags = []
  15. this.toaster = null
  16. this.chatter = null
  17. }
  18. get isLoading() {
  19. return this._loading
  20. }
  21. /**
  22. * Track login separate from complete-ess
  23. * so a user can login but attached profile
  24. * can forward to survey
  25. * @returns {boolean}
  26. */
  27. get isLoggedIn() {
  28. return this.id.value != null
  29. }
  30. /**
  31. * Combine questions retrieved from the database and
  32. * questions defined in out lang file and
  33. * copare to responses stored
  34. * @returns {boolean}
  35. */
  36. get isComplete() {
  37. return (
  38. this.responses.length == surveyFactory.questionsFromDb.length &&
  39. surveyFactory.questionsFromDb.length > 0
  40. )
  41. }
  42. /**
  43. * Check that some responses are set
  44. * @returns {boolean}
  45. */
  46. get hasResponses() {
  47. return this.responses.length && this.responses.length > 0
  48. }
  49. /**
  50. * Login a profile by id number
  51. * @param {number} profileId
  52. * @returns {number} stored reactive id
  53. */
  54. async login(profileId, cb) {
  55. console.warn('logging in:', profileId)
  56. this.id.value = parseInt(profileId)
  57. this.setupChatter()
  58. this.setupToaster(cb)
  59. return this.id.value
  60. }
  61. logout() {
  62. console.warn('logging out:', this.id.value)
  63. this.id.value = null
  64. if (this.toaster) {
  65. this.toaster.stop()
  66. }
  67. if (this.chatter) {
  68. this.chatter.stop()
  69. }
  70. }
  71. async getTags() {
  72. try {
  73. const tags = []
  74. this.setTags(tags)
  75. } catch (err) {
  76. console.error(err)
  77. }
  78. }
  79. setTags(tags) {
  80. this.tags = tags
  81. }
  82. async getResponses() {
  83. try {
  84. const responseList = await fetchResponsesByProfileId(this.id)
  85. this.setResponses(responseList)
  86. } catch (err) {
  87. console.error(err)
  88. }
  89. }
  90. setResponses(responses) {
  91. this.responses = responses
  92. }
  93. /**
  94. * For push notifications and chat
  95. */
  96. setupToaster(waveCb) {
  97. this.toaster = new StonkAlert(this.id.value, waveCb)
  98. }
  99. setupChatter() {
  100. this.chatter = new Chatter()
  101. // Use the reactive id object
  102. const testAccountUUID = this.id.value
  103. this.chatter.setup(testAccountUUID)
  104. }
  105. }
  106. const currentProfile = new Login()
  107. export { currentProfile }