Files
InvoiceShelf/resources/scripts/stores/user.store.ts
Darko Gjorgjijoski 71388ec6a5 Rename resources/scripts-v2 to resources/scripts and drop @v2 alias
Now that the legacy v1 frontend (commit 064bdf53) is gone, the v2 directory is the only frontend and the v2 suffix is just noise. Renames resources/scripts-v2 to resources/scripts via git mv (so git records the move as renames, preserving blame and log --follow), then bulk-rewrites the 152 files that imported via @v2/... to use @/scripts/... instead. The existing @ alias (resources/) covers the new path with no extra config needed.

Drops the now-unused @v2 alias from vite.config.js and points the laravel-vite-plugin entry at resources/scripts/main.ts. Updates the only blade reference (resources/views/app.blade.php) to match. The package.json test script (eslint ./resources/scripts) automatically targets the right place after the rename without any edit.

Verified: npm run build exits clean and the Vite warning lines now reference resources/scripts/plugins/i18n.ts, confirming every import resolved through the new path. git log --follow on any moved file walks back through its scripts-v2 history.
2026-04-07 12:50:16 +02:00

165 lines
4.7 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { userService } from '@/scripts/api/services/user.service'
import type { UpdateProfilePayload, UserSettingsPayload } from '@/scripts/api/services/user.service'
import { useNotificationStore } from './notification.store'
import { handleApiError } from '../utils/error-handling'
import type { User } from '@/scripts/types/domain/user'
import type { Ability } from '@/scripts/types/domain/role'
import type { ApiResponse } from '@/scripts/types/api'
export interface UserForm {
name: string
email: string
password: string
confirm_password: string
language: string
}
export const useUserStore = defineStore('user', () => {
// State
const currentUser = ref<User | null>(null)
const currentAbilities = ref<Ability[]>([])
const currentUserSettings = ref<Record<string, string>>({})
const userForm = ref<UserForm>({
name: '',
email: '',
password: '',
confirm_password: '',
language: '',
})
// Getters
const currentAbilitiesCount = computed<number>(() => currentAbilities.value.length)
const isOwner = computed<boolean>(() => currentUser.value?.is_owner ?? false)
// Actions
async function fetchCurrentUser(): Promise<ApiResponse<User>> {
try {
const response = await userService.getProfile()
currentUser.value = response.data
userForm.value = {
name: response.data.name,
email: response.data.email,
password: '',
confirm_password: '',
language: currentUserSettings.value.language || '',
}
return response
} catch (err: unknown) {
handleApiError(err)
throw err
}
}
async function updateCurrentUser(data: UpdateProfilePayload): Promise<ApiResponse<User>> {
try {
const response = await userService.updateProfile(data)
currentUser.value = response.data
userForm.value = {
name: response.data.name,
email: response.data.email,
password: '',
confirm_password: '',
language: currentUserSettings.value.language || '',
}
const notificationStore = useNotificationStore()
notificationStore.showNotification({
type: 'success',
message: 'settings.account_settings.updated_message',
})
return response
} catch (err: unknown) {
handleApiError(err)
throw err
}
}
async function uploadAvatar(data: FormData): Promise<ApiResponse<User>> {
try {
const response = await userService.uploadAvatar(data)
if (currentUser.value) {
currentUser.value.avatar = response.data.avatar
}
return response
} catch (err: unknown) {
handleApiError(err)
throw err
}
}
async function fetchUserSettings(settings?: string[]): Promise<Record<string, string | null>> {
try {
const response = await userService.getSettings(settings)
return response
} catch (err: unknown) {
handleApiError(err)
throw err
}
}
async function updateUserSettings(data: UserSettingsPayload): Promise<void> {
try {
await userService.updateSettings(data)
const settings = data.settings as Record<string, string | number | boolean | null>
if (settings.language && typeof settings.language === 'string') {
currentUserSettings.value.language = settings.language
}
if (settings.default_estimate_template && typeof settings.default_estimate_template === 'string') {
currentUserSettings.value.default_estimate_template = settings.default_estimate_template
}
if (settings.default_invoice_template && typeof settings.default_invoice_template === 'string') {
currentUserSettings.value.default_invoice_template = settings.default_invoice_template
}
} catch (err: unknown) {
handleApiError(err)
throw err
}
}
function hasAbilities(abilities: string | string[]): boolean {
return !!currentAbilities.value.find((ab) => {
if (ab.name === '*') return true
if (typeof abilities === 'string') {
return ab.name === abilities
}
return !!abilities.find((p) => ab.name === p)
})
}
function hasAllAbilities(abilities: string[]): boolean {
let isAvailable = true
currentAbilities.value.filter((ab) => {
const hasContain = !!abilities.find((p) => ab.name === p)
if (!hasContain) {
isAvailable = false
}
})
return isAvailable
}
return {
currentUser,
currentAbilities,
currentUserSettings,
userForm,
currentAbilitiesCount,
isOwner,
fetchCurrentUser,
updateCurrentUser,
uploadAvatar,
fetchUserSettings,
updateUserSettings,
hasAbilities,
hasAllAbilities,
}
})