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

login.service.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { ref } from 'vue'
  2. import { fetchResponsesByProfileId } 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. }
  16. get isLoading() {
  17. return this._loading
  18. }
  19. /**
  20. * Track login separate from complete-ess
  21. * so a user can login but attached profile
  22. * can forward to survey
  23. * @returns {boolean}
  24. */
  25. get isLoggedIn() {
  26. return this.id.value != null
  27. }
  28. /**
  29. * Combine questions retrieved from the database and
  30. * questions defined in out lang file and
  31. * copare to responses stored
  32. * @returns {boolean}
  33. */
  34. get isComplete() {
  35. return this.responses.length == surveyFactory.questionsFromDb.length && surveyFactory.questionsFromDb.length > 0
  36. }
  37. /**
  38. * Check that some responses are set
  39. * @returns {boolean}
  40. */
  41. get hasResponses() {
  42. return this.responses.length && this.responses.length > 0
  43. }
  44. /**
  45. * Login a profile by id number
  46. * @param {number} profileId
  47. * @returns {number} stored reactive id
  48. */
  49. async login(profileId) {
  50. console.warn('logging in:', profileId)
  51. this.id.value = parseInt(profileId)
  52. return this.id.value
  53. }
  54. logout() { this.id.value = null }
  55. async getTags() {
  56. try {
  57. const tags = []
  58. this.setTags(tags)
  59. } catch (err) {
  60. console.error(err)
  61. }
  62. }
  63. setTags(tags) { this.tags = tags }
  64. async getResponses() {
  65. try {
  66. const responseList = await fetchResponsesByProfileId(this.id)
  67. this.setResponses(responseList)
  68. } catch (err) {
  69. console.error(err)
  70. }
  71. }
  72. setResponses(responses) { this.responses = responses }
  73. }
  74. const currentProfile = new Login()
  75. export { currentProfile }