From 84cb9aea60768c68eeda7ed258a89a4e2663e381 Mon Sep 17 00:00:00 2001 From: yoosangwook Date: Fri, 27 Sep 2024 10:06:11 +0900 Subject: [PATCH] feat: Add Swal hook --- src/components/Playground.jsx | 27 +++++++++++++++++++++++++ src/hooks/useSwal.js | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/hooks/useSwal.js diff --git a/src/components/Playground.jsx b/src/components/Playground.jsx index f0a71e44..9614cea6 100644 --- a/src/components/Playground.jsx +++ b/src/components/Playground.jsx @@ -15,6 +15,7 @@ import QSelect from '@/components/ui/QSelect' import { Button } from '@nextui-org/react' import ColorPicker from './common/color-picker/ColorPicker' import { toastUp } from '@/hooks/useToast' +import { useSwal } from '@/hooks/useSwal' import styles from './playground.module.css' import Image from 'next/image' @@ -31,6 +32,7 @@ export default function Playground() { const testVar = process.env.NEXT_PUBLIC_TEST const converterUrl = process.env.NEXT_PUBLIC_CONVERTER_API_URL const { getMessage } = useMessage() + const { swalFire } = useSwal() const [color, setColor] = useState('#ff0000') @@ -103,6 +105,13 @@ export default function Playground() { date: '', }, ] + + const handleSwalAlert = () => { + swalFire({ + text: '알림 테스트입니다.', + }) + } + return ( <>
@@ -151,6 +160,24 @@ export default function Playground() { )}
+
+ +
+
+ +
) diff --git a/src/hooks/useSwal.js b/src/hooks/useSwal.js new file mode 100644 index 00000000..e39351d8 --- /dev/null +++ b/src/hooks/useSwal.js @@ -0,0 +1,38 @@ +import Swal from 'sweetalert2' +import withReactContent from 'sweetalert2-react-content' + +export const useSwal = () => { + const MySwal = withReactContent(Swal) + + const swalFire = ({ title = '', text = '', html = '', type = 'alert', confirmFn = () => {}, denyFn = () => {} }) => { + if (type === 'alert') { + MySwal.fire({ + title, + text, + icon: 'success', + confirmButtonText: '확인', + }) + } else if (type === 'confirm') { + MySwal.fire({ + title, + text, + html, + icon: 'question', + showCloseButton: true, + showCancelButton: true, + confirmButtonText: '확인', + cancelButtonText: '취소', + }).then((result) => { + if (result.isConfirmed) { + confirmFn() + } else if (result.isDenied) { + denyFn() + } + }) + } + } + + return { + swalFire, + } +}