import { ref } from 'vue' import { fetchResponsesByProfileId, Chatter, StonkAlert } from '../services' import { surveyFactory } from '../utils' /** * Logged in profile state manager * Sort of a util and service hybrid */ class Login { constructor() { this._loading = false // Make reactive with vue observer this.id = ref(null) this.responses = [] this.tags = [] this.toaster = null this.chatter = null } get isLoading() { return this._loading } /** * Track login separate from complete-ess * so a user can login but attached profile * can forward to survey * @returns {boolean} */ get isLoggedIn() { return this.id.value != null } /** * Combine questions retrieved from the database and * questions defined in out lang file and * copare to responses stored * @returns {boolean} */ get isComplete() { return ( this.responses.length == surveyFactory.questionsFromDb.length && surveyFactory.questionsFromDb.length > 0 ) } /** * Check that some responses are set * @returns {boolean} */ get hasResponses() { return this.responses.length && this.responses.length > 0 } /** * Login a profile by id number * @param {number} profileId * @returns {number} stored reactive id */ async login(profileId) { console.warn('logging in:', profileId) this.id.value = parseInt(profileId) return this.id.value } logout() { this.id.value = null if (this.toaster) { this.toaster.stop() } if (this.chatter) { this.toaster.stop() } } async getTags() { try { const tags = [] this.setTags(tags) } catch (err) { console.error(err) } } setTags(tags) { this.tags = tags } async getResponses() { try { const responseList = await fetchResponsesByProfileId(this.id) this.setResponses(responseList) } catch (err) { console.error(err) } } setResponses(responses) { this.responses = responses } /** * For push notifications and chat */ setupToaster(waveCb) { this.toaster = new StonkAlert(this.id.value, waveCb) } setupChatter() { this.chatter = new Chatter() const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID this.chatter.setup(testAccountUUID) } } const currentProfile = new Login() export { currentProfile }