1
0

feat: convert model methods to named functions

This commit is contained in:
Dominik Pschenitschni
2022-09-23 16:51:35 +02:00
parent 176ad565cc
commit 8e3f54ae42
16 changed files with 75 additions and 51 deletions

View File

@ -3,7 +3,7 @@ import {defineStore, acceptHMRUpdate} from 'pinia'
import {HTTPFactory, AuthenticatedHTTPFactory} from '@/http-common'
import {i18n, getCurrentLanguage, saveLanguage} from '@/i18n'
import {objectToSnakeCase} from '@/helpers/case'
import UserModel from '@/models/user'
import UserModel, { getAvatarUrl } from '@/models/user'
import UserSettingsService from '@/services/userSettings'
import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
import {setLoadingPinia} from '@/store/helper'
@ -15,6 +15,7 @@ import type {IUserSettings} from '@/modelTypes/IUserSettings'
import router from '@/router'
import {useConfigStore} from '@/stores/config'
import UserSettingsModel from '@/models/userSettings'
import {store} from '@/store'
export const useAuthStore = defineStore('auth', {
state: () : AuthState => ({
@ -28,6 +29,7 @@ export const useAuthStore = defineStore('auth', {
lastUserInfoRefresh: null,
isLoading: false,
isLoadingGeneralSettings: false,
}),
getters: {
authUser(state) {
@ -48,6 +50,10 @@ export const useAuthStore = defineStore('auth', {
this.isLoading = isLoading
},
setIsLoadingGeneralSettings(isLoading: boolean) {
this.isLoadingGeneralSettings = isLoading
},
setUser(info: IUser | null) {
this.info = info
if (info !== null) {
@ -78,7 +84,7 @@ export const useAuthStore = defineStore('auth', {
},
reloadAvatar() {
if (!this.info) return
this.avatarUrl = `${this.info.getAvatarUrl()}&=${+new Date()}`
this.avatarUrl = `${getAvatarUrl(this.info)}&=${+new Date()}`
},
updateLastUserRefresh() {
this.lastUserInfoRefresh = new Date()
@ -87,6 +93,7 @@ export const useAuthStore = defineStore('auth', {
// Logs a user in with a set of credentials.
async login(credentials) {
const HTTP = HTTPFactory()
store.commit('loading', true)
this.setIsLoading(true)
// Delete an eventually preexisting old token
@ -110,6 +117,7 @@ export const useAuthStore = defineStore('auth', {
throw e
} finally {
store.commit('loading', false)
this.setIsLoading(false)
}
},
@ -118,6 +126,7 @@ export const useAuthStore = defineStore('auth', {
// Not sure if this is the right place to put the logic in, maybe a seperate js component would be better suited.
async register(credentials) {
const HTTP = HTTPFactory()
store.commit('loading', true)
this.setIsLoading(true)
try {
await HTTP.post('register', credentials)
@ -129,12 +138,14 @@ export const useAuthStore = defineStore('auth', {
throw e
} finally {
store.commit('loading', false)
this.setIsLoading(false)
}
},
async openIdAuth({provider, code}) {
const HTTP = HTTPFactory()
store.commit('loading', true)
this.setIsLoading(true)
const data = {
@ -151,6 +162,7 @@ export const useAuthStore = defineStore('auth', {
// Tell others the user is autheticated
this.checkAuth()
} finally {
store.commit('loading', false)
this.setIsLoading(false)
}
},
@ -266,11 +278,10 @@ export const useAuthStore = defineStore('auth', {
settings: IUserSettings
showMessage : boolean
}) {
// const showMessage = payload.showMessage ?? true
const userSettingsService = new UserSettingsService()
// FIXME
const cancel = setLoadingPinia(useAuthStore, 'general-settings')
const cancel = setLoadingPinia(this, this.setIsLoadingGeneralSettings)
try {
saveLanguage(settings.language)
await userSettingsService.update(settings)

View File

@ -80,7 +80,7 @@ export const useLabelStore = defineStore('label', {
return
}
const cancel = setLoadingPinia(useLabelStore, this.setIsLoading)
const cancel = setLoadingPinia(this)
try {
const labels = await getAllLabels()
@ -92,7 +92,7 @@ export const useLabelStore = defineStore('label', {
},
async deleteLabel(label: ILabel) {
const cancel = setLoadingPinia(useLabelStore)
const cancel = setLoadingPinia(this)
const labelService = new LabelService()
try {
@ -106,7 +106,7 @@ export const useLabelStore = defineStore('label', {
},
async updateLabel(label: ILabel) {
const cancel = setLoadingPinia(useLabelStore)
const cancel = setLoadingPinia(this)
const labelService = new LabelService()
try {
@ -120,7 +120,7 @@ export const useLabelStore = defineStore('label', {
},
async createLabel(label: ILabel) {
const cancel = setLoadingPinia(useLabelStore)
const cancel = setLoadingPinia(this)
const labelService = new LabelService()
try {

View File

@ -87,7 +87,7 @@ export const useListStore = defineStore('list', {
},
async createList(list: IList) {
const cancel = setLoadingPinia(useListStore)
const cancel = setLoadingPinia(this)
const listService = new ListService()
try {
@ -103,7 +103,7 @@ export const useListStore = defineStore('list', {
},
async updateList(list: IList) {
const cancel = setLoadingPinia(useListStore)
const cancel = setLoadingPinia(this)
const listService = new ListService()
try {
@ -139,7 +139,7 @@ export const useListStore = defineStore('list', {
},
async deleteList(list: IList) {
const cancel = setLoadingPinia(useListStore)
const cancel = setLoadingPinia(this)
const listService = new ListService()
try {