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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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) {
  55. console.warn('logging in:', profileId)
  56. this.id.value = parseInt(profileId)
  57. return this.id.value
  58. }
  59. logout() {
  60. this.id.value = null
  61. if (this.toaster) {
  62. this.toaster.stop()
  63. }
  64. if (this.chatter) {
  65. this.toaster.stop()
  66. }
  67. }
  68. async getTags() {
  69. try {
  70. const tags = []
  71. this.setTags(tags)
  72. } catch (err) {
  73. console.error(err)
  74. }
  75. }
  76. setTags(tags) {
  77. this.tags = tags
  78. }
  79. async getResponses() {
  80. try {
  81. const responseList = await fetchResponsesByProfileId(this.id)
  82. this.setResponses(responseList)
  83. } catch (err) {
  84. console.error(err)
  85. }
  86. }
  87. setResponses(responses) {
  88. this.responses = responses
  89. }
  90. /**
  91. * For push notifications and chat
  92. */
  93. setupToaster(waveCb) {
  94. this.toaster = new StonkAlert(this.id.value, waveCb)
  95. }
  96. setupChatter() {
  97. this.chatter = new Chatter()
  98. const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
  99. this.chatter.setup(testAccountUUID)
  100. }
  101. }
  102. const currentProfile = new Login()
  103. export { currentProfile }