import React, { FunctionComponent, useEffect, useState } from 'react' import { PlusCircleIcon, TrashIcon } from '@heroicons/react/outline' import Modal from './Modal' import Input from './Input' import { ElementTitle } from './styles' import useMangoStore from '../stores/useMangoStore' import Button, { LinkButton } from './Button' import { notify } from '../utils/notifications' import { useTranslation } from 'next-i18next' import ButtonGroup from './ButtonGroup' import InlineNotification from './InlineNotification' interface CreateAlertModalProps { onClose: () => void isOpen: boolean repayAmount?: string tokenSymbol?: string } const CreateAlertModal: FunctionComponent = ({ isOpen, onClose, }) => { const { t } = useTranslation('common') const actions = useMangoStore((s) => s.actions) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const activeAlerts = useMangoStore((s) => s.alerts.activeAlerts) const loading = useMangoStore((s) => s.alerts.loading) const submitting = useMangoStore((s) => s.alerts.submitting) const error = useMangoStore((s) => s.alerts.error) const [email, setEmail] = useState('') const [invalidAmountMessage, setInvalidAmountMessage] = useState('') const [health, setHealth] = useState('') const [showCustomHealthForm, setShowCustomHealthForm] = useState(false) const [showAlertForm, setShowAlertForm] = useState(false) const healthPresets = ['5', '10', '15', '25', '30'] const validateEmailInput = (amount) => { if (Number(amount) <= 0) { setInvalidAmountMessage(t('enter-amount')) } } const onChangeEmailInput = (amount) => { setEmail(amount) setInvalidAmountMessage('') } async function onCreateAlert() { if (!email) { notify({ title: 'An email address is required', type: 'error', }) return } else if (!health) { notify({ title: 'Alert health is required', type: 'error', }) return } const body = { mangoGroupPk: mangoGroup.publicKey.toString(), mangoAccountPk: mangoAccount.publicKey.toString(), health, alertProvider: 'mail', email, } const success: any = await actions.createAlert(body) if (success) { setShowAlertForm(false) } } const handleCancelCreateAlert = () => { if (activeAlerts.length > 0) { setShowAlertForm(false) } else { onClose() } } useEffect(() => { actions.loadAlerts(mangoAccount.publicKey) }, []) return ( {!loading && !submitting ? ( <> {activeAlerts.length > 0 && !showAlertForm ? ( <>
{t('active-alerts')}
{activeAlerts.map((alert, index) => (
{t('alert-info', { health: alert.health })}
actions.deleteAlert(alert._id)} />
))}
{activeAlerts.length >= 3 ? (
{t('alerts-max')}
) : null} ) : showAlertForm ? ( <>
{t('create-alert')}

{t('alerts-disclaimer')}

{error ? (
) : null}
{t('email-address')}
validateEmailInput(e.target.value)} value={email || ''} onChange={(e) => onChangeEmailInput(e.target.value)} />
{t('alert-health')}
setShowCustomHealthForm(!showCustomHealthForm) } > {showCustomHealthForm ? t('presets') : t('custom')}
{showCustomHealthForm ? ( setHealth(e.target.value)} suffix={
%
} value={health} /> ) : ( setHealth(p)} unit="%" values={healthPresets} /> )}
{t('cancel')}
) : error ? (
) : (
{t('no-alerts')}

{t('no-alerts-desc')}

)} ) : (
)} ) } export default React.memo(CreateAlertModal)