qcast-front/src/hooks/useSwal.js

63 lines
1.5 KiB
JavaScript

import Swal from 'sweetalert2'
import withReactContent from 'sweetalert2-react-content'
import { useMessage } from '@/hooks/useMessage'
/**
* title: 제목
* text: 내용
* html: html
* type: alert, confirm
* icon: icontype (success, error, warning, info, question)
* confirmFn: 확인 버튼 클릭 시 실행할 함수
* denyFn: 취소 버튼 클릭 시 실행할 함수
* @returns
*/
export const useSwal = () => {
const MySwal = withReactContent(Swal)
const { getMessage } = useMessage()
const swalFire = ({
title = '',
text = '',
html = '',
type = 'alert',
icon = '',
confirmButtonText = '',
cancelButtonText = '',
confirmFn = () => {},
denyFn = () => {},
}) => {
if (type === 'alert') {
MySwal.fire({
title,
text,
icon: icon === '' ? 'success' : icon,
confirmButtonText: getMessage('common.ok'),
}).then(() => {
confirmFn()
})
} else if (type === 'confirm') {
MySwal.fire({
title,
text,
html,
icon: icon === '' ? 'question' : icon,
showCloseButton: true,
showCancelButton: true,
confirmButtonText: confirmButtonText === '' ? getMessage('common.ok') : confirmButtonText,
cancelButtonText: cancelButtonText === '' ? getMessage('common.cancel') : cancelButtonText,
}).then((result) => {
if (result.isConfirmed) {
confirmFn()
} else if (result.isDismissed) {
denyFn()
}
})
}
}
return {
swalFire,
}
}