123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { ref } from 'vue'
- import type { ButtonVariant } from '#shared/types/button.ts'
- import getUuid from '#shared/utils/getUuid.ts'
- import { useAppName } from './useAppName.ts'
- import type { Except } from 'type-fest'
- export type ConfirmationVariant = 'delete' | 'unsaved' | 'confirm'
- export interface ConfirmationOptions {
- headerTitle?: string
- headerTitlePlaceholder?: string[]
- headerIcon?: string
- text?: string
- textPlaceholder?: string[]
- buttonLabel?: string
- buttonVariant?: ButtonVariant
- cancelLabel?: string
- fullscreen?: boolean
- // TODO: should maybe also be implemented for mobile, so that we have a better alignment for the code
- confirmationVariant?: ConfirmationVariant
- confirmCallback: () => void
- cancelCallback: () => void
- closeCallback: () => void
- }
- const confirmationOptions = ref(new Map<string, ConfirmationOptions>())
- const lastConfirmationUuid = ref<string | undefined>()
- const triggerConfirmation = ref(0)
- export const useConfirmation = () => {
- const appName = useAppName()
- const waitForConfirmation = (
- text: string,
- options: Except<
- ConfirmationOptions,
- 'text' | 'confirmCallback' | 'cancelCallback' | 'closeCallback'
- > = {},
- name: string | undefined = undefined,
- ) => {
- const uniqueName =
- appName === 'desktop' ? name || getUuid() : 'confirmation'
- if (confirmationOptions.value.has(uniqueName)) {
- return new Promise<undefined>((resolve) => {
- resolve(undefined)
- })
- }
- return new Promise<boolean | undefined>((resolve) => {
- confirmationOptions.value.set(uniqueName, {
- ...options,
- text,
- confirmCallback() {
- resolve(true)
- },
- cancelCallback() {
- resolve(false)
- },
- closeCallback() {
- resolve(undefined)
- },
- })
- lastConfirmationUuid.value = uniqueName
- triggerConfirmation.value += 1
- })
- }
- const waitForVariantConfirmation = (
- variant: ConfirmationVariant = 'confirm',
- options: Except<
- ConfirmationOptions,
- 'text' | 'confirmCallback' | 'cancelCallback' | 'closeCallback'
- > = {},
- name: string | undefined = undefined,
- ) => {
- return waitForConfirmation(
- '',
- {
- ...options,
- confirmationVariant: variant,
- },
- name,
- )
- }
- return {
- lastConfirmationUuid,
- triggerConfirmation,
- confirmationOptions,
- waitForConfirmation,
- waitForVariantConfirmation,
- }
- }
|