mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-07 21:44:51 +00:00
* refactor: add HTTP client wrapper and upgrade axios to v1 Introduce a thin HTTP wrapper (resources/scripts/http) that centralizes axios configuration, interceptors, and auth header injection. All 43 files now import from the wrapper instead of axios directly, making future library swaps a single-file change. Upgrade axios from 0.30.0 to 1.14.0. * fix: restore window.Ls assignment removed during axios refactor company.js uses window.Ls.set() to persist selected company, which broke after the axios plugin (that set window.Ls) was deleted.
156 lines
4.3 KiB
JavaScript
Vendored
156 lines
4.3 KiB
JavaScript
Vendored
import http from '@/scripts/http'
|
|
import { defineStore } from 'pinia'
|
|
import { useNotificationStore } from '@/scripts/stores/notification'
|
|
import { handleError } from '@/scripts/helpers/error-handling'
|
|
|
|
export const useUserStore = (useWindow = false) => {
|
|
const defineStoreFunc = useWindow ? window.pinia.defineStore : defineStore
|
|
const { global } = window.i18n
|
|
|
|
return defineStoreFunc({
|
|
id: 'user',
|
|
|
|
state: () => ({
|
|
currentUser: null,
|
|
currentAbilities: [],
|
|
currentUserSettings: {},
|
|
|
|
userForm: {
|
|
name: '',
|
|
email: '',
|
|
password: '',
|
|
confirm_password: '',
|
|
language: '',
|
|
},
|
|
}),
|
|
|
|
getters: {
|
|
currentAbilitiesCount: (state) => state.currentAbilities.length,
|
|
},
|
|
|
|
actions: {
|
|
updateCurrentUser(data) {
|
|
return new Promise((resolve, reject) => {
|
|
http
|
|
.put('/api/v1/me', data)
|
|
.then((response) => {
|
|
this.currentUser = response.data.data
|
|
Object.assign(this.userForm, response.data.data)
|
|
const notificationStore = useNotificationStore()
|
|
notificationStore.showNotification({
|
|
type: 'success',
|
|
message: global.t('settings.account_settings.updated_message'),
|
|
})
|
|
resolve(response)
|
|
})
|
|
.catch((err) => {
|
|
handleError(err)
|
|
reject(err)
|
|
})
|
|
})
|
|
},
|
|
|
|
fetchCurrentUser(params) {
|
|
return new Promise((resolve, reject) => {
|
|
http
|
|
.get(`/api/v1/me`, params)
|
|
.then((response) => {
|
|
this.currentUser = response.data.data
|
|
this.userForm = response.data.data
|
|
resolve(response)
|
|
})
|
|
.catch((err) => {
|
|
handleError(err)
|
|
reject(err)
|
|
})
|
|
})
|
|
},
|
|
|
|
uploadAvatar(data) {
|
|
return new Promise((resolve, reject) => {
|
|
http
|
|
.post('/api/v1/me/upload-avatar', data)
|
|
.then((response) => {
|
|
this.currentUser.avatar = response.data.data.avatar
|
|
resolve(response)
|
|
})
|
|
.catch((err) => {
|
|
handleError(err)
|
|
reject(err)
|
|
})
|
|
})
|
|
},
|
|
|
|
fetchUserSettings(settings) {
|
|
return new Promise((resolve, reject) => {
|
|
http
|
|
.get('/api/v1/me/settings', {
|
|
params: {
|
|
settings,
|
|
},
|
|
})
|
|
.then((response) => {
|
|
resolve(response)
|
|
})
|
|
.catch((err) => {
|
|
handleError(err)
|
|
reject(err)
|
|
})
|
|
})
|
|
},
|
|
|
|
updateUserSettings(data) {
|
|
return new Promise((resolve, reject) => {
|
|
http
|
|
.put('/api/v1/me/settings', data)
|
|
.then((response) => {
|
|
if (data.settings.language) {
|
|
this.currentUserSettings.language = data.settings.language
|
|
global.locale.value = data.settings.language
|
|
}
|
|
if (data.settings.default_estimate_template) {
|
|
this.currentUserSettings.default_estimate_template =
|
|
data.settings.default_estimate_template
|
|
}
|
|
if (data.settings.default_invoice_template) {
|
|
this.currentUserSettings.default_invoice_template =
|
|
data.settings.default_invoice_template
|
|
}
|
|
resolve(response)
|
|
})
|
|
.catch((err) => {
|
|
handleError(err)
|
|
reject(err)
|
|
})
|
|
})
|
|
},
|
|
|
|
hasAbilities(abilities) {
|
|
return !!this.currentAbilities.find((ab) => {
|
|
if (ab.name === '*') return true
|
|
if (typeof abilities === 'string') {
|
|
return ab.name === abilities
|
|
}
|
|
return !!abilities.find((p) => {
|
|
return ab.name === p
|
|
})
|
|
})
|
|
},
|
|
|
|
hasAllAbilities(abilities) {
|
|
let isAvailable = true
|
|
this.currentAbilities.filter((ab) => {
|
|
let hasContain = !!abilities.find((p) => {
|
|
return ab.name === p
|
|
})
|
|
if (!hasContain) {
|
|
isAvailable = false
|
|
}
|
|
})
|
|
|
|
return isAvailable
|
|
},
|
|
},
|
|
})()
|
|
}
|