import React, { Fragment, useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { Button, Form, FormGroup, Label, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap'; import Select from 'react-select'; import { Input, Checkbox, RadioButton } from '../../../../common/FormControls'; import useForm from '../../../../common/hooks/useForm.js'; import { EXTRAS } from '../../../../../redux/modules/appState/share/forms/alertForm'; import { createAlertAPI } from '../../../../../api/analytics/createAnalytics'; import { getCurrentTimezone, timezones } from '../../../../../common/Timezones'; import { compose } from 'redux'; import reduxConnect from '../../../../../redux/utils/connect'; import translate from 'react-i18next/dist/commonjs/translate'; import { THEME_TYPES } from '../../../../../redux/modules/appState/share/forms/notificationForm'; const initialForm = { name: '', recipients: [], subject: '', automatedSubject: false, unsubscribeNotification: false, published: false, allowUnsubscribe: false, articleExtracts: EXTRAS.CONTEXTUAL, highlight: false, showSourceCountry: false, showUserComments: false, themeType: THEME_TYPES.PLAIN, sendWhenEmpty: false, timezone: getCurrentTimezone(), notificationType: 'alert', // automatic: [], // auto schedule // sentUntil: '', errors: { name: null } }; function AlertDialog(props) { const { toggle, isOpen, alertCharts, actions, resetAlertChart, user } = props; const [loading, setLoading] = useState(false); const { form, handleChange, handleValidation, errors, validateSubmit, resetForm } = useForm(initialForm); function handleSubmit() { const obj = validateSubmit(); if (!obj) { return actions.addAlert({ type: 'error', transKey: 'requiredInfo' }); } setLoading(true); if (obj.automatedSubject) { delete obj.subject; } obj.sources = alertCharts.map((chart) => ({ id: chart.id, type: 'chart' })); createAlertAPI(obj).then((res) => { if (res.error) { res.data ? actions.addAlert(res.data) : actions.addAlert({ type: 'error', transKey: 'somethingWrong' }); setLoading(false); return; } actions.addAlert({ type: 'notice', transKey: 'alertSaved' }); setLoading(false); toggle(); resetForm(); resetAlertChart(); }); } useEffect(() => { if (form.recipients && user.recipient && user.recipient.id) { handleChange('recipients', [user.recipient.id]); } return () => resetForm(); }, []); return ( Create Alert
{alertCharts.map((chart, i, arr) => ( {chart.name} {arr.length - 1 !== i ? ', ' : ''} ))}
{!form.automatedSubject && ( )}