aboutsummaryrefslogtreecommitdiffstats
path: root/components/Popup.js
blob: 911d2fa36341c5b89a7400c14fc32fb1112b8f8e (plain) (blame)
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
import React, {useState, useEffect} from 'react'

const Popup = ({popup}) => {
  const [visible, setVisible] = useState(false)
  const {
    content = null,
    time = null,
    error = null,
    yes = null,
    no = null,
  } = popup

  useEffect(() => {
    setVisible(true)
    time && setTimeout(() => setVisible(false), time)
  }, [popup])

  if (!content) return null

  return visible ? (
    <div className={`window window--popup${error ? ' window--error' : ''}`}>
      <div className="window__content--popup">{ content }</div>
      {
        (yes || no) && (<div className="window__buttons--popup">
          {[yes, no].map(a => a && <input key={a.label} className='window__button' type="button" onClick={() => { setVisible(false); a.action() }} value={a.label} />)}
        </div>)
      }
    </div>
  ) : null
}

export default Popup