import { ref } from 'vue' import { fetchResponsesByProfileId } 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 = [] } 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 } 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 } } const currentProfile = new Login() export { currentProfile }