2023-07-31 21:24:41 +00:00
|
|
|
import type { Accessor, JSX } from 'solid-js'
|
|
|
|
|
2023-11-14 15:10:00 +00:00
|
|
|
import { createContext, createSignal, useContext } from 'solid-js'
|
|
|
|
|
2023-10-16 15:57:29 +00:00
|
|
|
import { ButtonVariant } from '../components/_shared/Button/Button'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { hideModal, showModal } from '../stores/ui'
|
2023-07-31 21:24:41 +00:00
|
|
|
|
|
|
|
type ConfirmMessage = {
|
|
|
|
confirmBody?: string | JSX.Element
|
|
|
|
confirmButtonLabel?: string
|
2023-10-16 15:57:29 +00:00
|
|
|
confirmButtonVariant?: ButtonVariant
|
2023-07-31 21:24:41 +00:00
|
|
|
declineButtonLabel?: string
|
2023-10-16 15:57:29 +00:00
|
|
|
declineButtonVariant?: ButtonVariant
|
2023-07-31 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConfirmContextType = {
|
|
|
|
confirmMessage: Accessor<ConfirmMessage>
|
2024-02-04 17:40:15 +00:00
|
|
|
showConfirm: (message?: {
|
|
|
|
confirmBody?: ConfirmMessage['confirmBody']
|
|
|
|
confirmButtonLabel?: ConfirmMessage['confirmButtonLabel']
|
|
|
|
confirmButtonVariant?: ConfirmMessage['confirmButtonVariant']
|
|
|
|
declineButtonLabel?: ConfirmMessage['declineButtonLabel']
|
|
|
|
declineButtonVariant?: ConfirmMessage['declineButtonVariant']
|
|
|
|
}) => Promise<boolean>
|
|
|
|
resolveConfirm: (value: boolean) => void
|
2023-07-31 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ConfirmContext = createContext<ConfirmContextType>()
|
|
|
|
|
|
|
|
export function useConfirm() {
|
|
|
|
return useContext(ConfirmContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ConfirmProvider = (props: { children: JSX.Element }) => {
|
|
|
|
const [confirmMessage, setConfirmMessage] = createSignal<ConfirmMessage>(null)
|
|
|
|
|
|
|
|
let resolveFn: (value: boolean) => void
|
|
|
|
|
|
|
|
const showConfirm = (
|
|
|
|
message: {
|
|
|
|
confirmBody?: ConfirmMessage['confirmBody']
|
|
|
|
confirmButtonLabel?: ConfirmMessage['confirmButtonLabel']
|
2023-10-16 15:57:29 +00:00
|
|
|
confirmButtonVariant?: ConfirmMessage['confirmButtonVariant']
|
2023-07-31 21:24:41 +00:00
|
|
|
declineButtonLabel?: ConfirmMessage['declineButtonLabel']
|
2023-10-16 15:57:29 +00:00
|
|
|
declineButtonVariant?: ConfirmMessage['declineButtonVariant']
|
2023-11-14 15:10:00 +00:00
|
|
|
} = {},
|
2023-07-31 21:24:41 +00:00
|
|
|
): Promise<boolean> => {
|
|
|
|
const messageToShow = {
|
|
|
|
confirmBody: message.confirmBody,
|
|
|
|
confirmButtonLabel: message.confirmButtonLabel,
|
2023-10-16 15:57:29 +00:00
|
|
|
confirmButtonVariant: message.confirmButtonVariant,
|
|
|
|
declineButtonLabel: message.declineButtonLabel,
|
2023-11-14 15:10:00 +00:00
|
|
|
declineButtonVariant: message.declineButtonVariant,
|
2023-07-31 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setConfirmMessage(messageToShow)
|
|
|
|
showModal('confirm')
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
resolveFn = resolve
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const resolveConfirm = (value: boolean) => {
|
|
|
|
resolveFn(value)
|
|
|
|
hideModal()
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
showConfirm,
|
2023-11-14 15:10:00 +00:00
|
|
|
resolveConfirm,
|
2023-07-31 21:24:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 17:40:15 +00:00
|
|
|
const value: ConfirmContextType = { confirmMessage, ...actions }
|
2023-07-31 21:24:41 +00:00
|
|
|
|
|
|
|
return <ConfirmContext.Provider value={value}>{props.children}</ConfirmContext.Provider>
|
|
|
|
}
|