summaryrefslogtreecommitdiff
path: root/src/lib/elements/hooks/useConfirmAlert.js
blob: 4975c57dc1ffe628ba2231bf3586400ebcca210c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { useState } from "react";
import dynamic from "next/dynamic";

const DynamicConfirmAlert = dynamic(() => import('@/components/elements/ConfirmAlert'));

const useConfirmAlert = ({
  title,
  caption,
  closeText,
  submitText,
  onSubmit,
}) => {
  const [ isOpen, setIsOpen ] = useState(false);
  const [ data, setData ] = useState(null);

  const closeConfirmAlert = () => {
    setIsOpen(false);
    setData(null);
  };
  const openConfirmAlert = ( data = null ) => {
    setIsOpen(true);
    setData(data);
  };
  const handleSubmit = async () => {
    await onSubmit(data);
    closeConfirmAlert();
  };

  const ConfirmAlert = (
    <DynamicConfirmAlert
      title={title}
      caption={caption}
      closeText={closeText}
      submitText={submitText}
      onClose={closeConfirmAlert}
      onSubmit={handleSubmit}
      show={isOpen}
    />
  );

  return {
    isOpen,
    closeConfirmAlert,
    openConfirmAlert,
    ConfirmAlert
  };
}

export default useConfirmAlert;