//@ts-ignore
import StartToastifyInstance from 'toastify-js/src/toastify-es'
//@ts-ignore
import dom from '@left4code/tw-starter/dist/js/dom'
const createToastifyConfig = (
element: Node
): StartToastifyInstance.Options => ({
node: element,
duration: 5000,
newWindow: true,
close: true,
gravity: 'top',
position: 'right',
stopOnFocus: true,
})
const createToastifyElement = (innerElement: string, dataTest = '') =>
dom(`
${innerElement}
`)[0]
const getSuccessMessage = (methodName: string) => {
return (
{
post: 'Ieraksts izveidots!',
put: 'Izmaiņas saglabātas!',
patch: 'Izmaiņas saglabātas!',
delete: 'Ieraksts dzēsts!',
}[methodName] ?? ''
)
}
export const showGlobalError = (description: string) => {
const element = createToastifyElement(
`
`,
'error-popup'
)
StartToastifyInstance(createToastifyConfig(element)).showToast()
}
type ToastifyInstance = {
showToast(): void
hideToast(): void
}
let activeSuccessToast: ToastifyInstance | null = null
export const showSuccessMessage = (successMessage: string) => {
const successElement = createToastifyElement(
`
`,
'success-popup'
)
if (activeSuccessToast) activeSuccessToast.hideToast()
activeSuccessToast = StartToastifyInstance(
createToastifyConfig(successElement)
) as ToastifyInstance
activeSuccessToast.showToast()
}
export const showWriteRequestSuccess = (methodName: string) => {
const successMessage = getSuccessMessage(methodName)
showSuccessMessage(successMessage)
}