Files
InvoiceShelf/resources/scripts-v2/stores/dialog.store.ts
Darko Gjorgjijoski 2b996d30bf Phase 2: Typed global Pinia stores in scripts-v2/
Rewrite all 7 global stores from JS options API to TypeScript
composition API. 8 files, 1005 lines, zero any types.

- auth.store.ts: login/logout with authService
- global.store.ts: bootstrap, menus, sidebar, config fetching
- company.store.ts: company selection, admin mode, settings
- user.store.ts: current user, abilities, settings
- notification.store.ts: typed toast notifications
- dialog.store.ts: confirm dialog returning Promise<boolean>
- modal.store.ts: modal state with isEdit getter

All use async/await, typed API services, localStore utility,
and explicit ref<T> generics.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 05:15:00 +02:00

91 lines
1.9 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref } from 'vue'
export type DialogVariant = 'primary' | 'danger'
export type DialogSize = 'sm' | 'md' | 'lg'
export interface OpenDialogPayload {
title: string
message: string
size?: DialogSize
data?: unknown
variant?: DialogVariant
yesLabel?: string
noLabel?: string
hideNoButton?: boolean
}
export const useDialogStore = defineStore('dialog', () => {
// State
const active = ref<boolean>(false)
const title = ref<string>('')
const message = ref<string>('')
const size = ref<DialogSize>('md')
const data = ref<unknown>(null)
const variant = ref<DialogVariant>('danger')
const yesLabel = ref<string>('Yes')
const noLabel = ref<string>('No')
const hideNoButton = ref<boolean>(false)
const resolve = ref<((value: boolean) => void) | null>(null)
// Actions
function openDialog(payload: OpenDialogPayload): Promise<boolean> {
active.value = true
title.value = payload.title
message.value = payload.message
size.value = payload.size ?? 'md'
data.value = payload.data ?? null
variant.value = payload.variant ?? 'danger'
yesLabel.value = payload.yesLabel ?? 'Yes'
noLabel.value = payload.noLabel ?? 'No'
hideNoButton.value = payload.hideNoButton ?? false
return new Promise<boolean>((res) => {
resolve.value = res
})
}
function confirm(): void {
if (resolve.value) {
resolve.value(true)
}
closeDialog()
}
function cancel(): void {
if (resolve.value) {
resolve.value(false)
}
closeDialog()
}
function closeDialog(): void {
active.value = false
setTimeout(() => {
title.value = ''
message.value = ''
data.value = null
resolve.value = null
}, 300)
}
return {
active,
title,
message,
size,
data,
variant,
yesLabel,
noLabel,
hideNoButton,
resolve,
openDialog,
confirm,
cancel,
closeDialog,
}
})