import React, {useEffect, useState} from "react"; import {Button, Card, Divider, Flex, Form, Input, message, Popconfirm, Select, Skeleton, Space, Typography} from "antd"; import {DeleteFilled, MinusCircleOutlined, PlusOutlined, ThunderboltFilled} from "@ant-design/icons"; import {deleteWatchlist, EventAction, getWatchlists, postWatchlist} from "../../utils/api"; import {AxiosError} from "axios"; import {t} from 'ttag' const formItemLayout = { labelCol: { xs: {span: 24}, sm: {span: 4}, }, wrapperCol: { xs: {span: 24}, sm: {span: 20}, }, }; const formItemLayoutWithOutLabel = { wrapperCol: { xs: {span: 24, offset: 0}, sm: {span: 20, offset: 4}, }, }; type Watchlist = { token: string, domains: { ldhName: string }[], triggers?: { event: EventAction, action: string }[] } export default function WatchlistPage() { const triggerEventItems: { label: string, value: EventAction }[] = [ { label: t`When a domain is expired`, value: 'expiration' }, { label: t`When a domain is deleted`, value: 'deletion' }, { label: t`When a domain is updated`, value: 'last changed' }, { label: t`When a domain is transferred`, value: 'transfer' }, { label: t`When a domain is locked`, value: 'locked' }, { label: t`When a domain is unlocked`, value: 'unlocked' }, { label: t`When a domain is reregistered`, value: 'reregistration' }, { label: t`When a domain is reinstantiated`, value: 'reinstantiation' }, { label: t`When a domain is registered`, value: 'registration' } ] const trigerActionItems = [ { label: t`Send me an email`, value: 'email' } ] const [form] = Form.useForm() const [messageApi, contextHolder] = message.useMessage() const [watchlists, setWatchlists] = useState() const onCreateWatchlist = (values: { domains: string[], triggers: { event: string, action: string }[] }) => { const domainsURI = values.domains.map(d => '/api/domains/' + d) postWatchlist(domainsURI, values.triggers).then((w) => { form.resetFields() refreshWatchlists() messageApi.success(t`Watchlist created !`) }).catch((e: AxiosError) => { const data = e?.response?.data as { detail: string } messageApi.error(data.detail ?? t`An error occurred`) }) } const refreshWatchlists = () => getWatchlists().then(w => { setWatchlists(w['hydra:member']) }).catch(() => setWatchlists(undefined)) useEffect(() => { refreshWatchlists() }, []) return {contextHolder}
{ if (!domains || domains.length < 1) { return Promise.reject(new Error(t`At least one domain name`)); } }, }, ]} > {(fields, {add, remove}, {errors}) => ( <> {fields.map((field, index) => ( {fields.length > 1 ? ( remove(field.name)} /> ) : null} ))} )} { if (!domains || domains.length < 1) { return Promise.reject(new Error(t`At least one domain trigger`)); } }, }, ]} > {(fields, {add, remove}, {errors}) => ( <> {fields.map((field, index) => ( {fields.length > 1 ? ( remove(field.name)} /> ) : null} ))} )}
{watchlists && watchlists.length > 0 && {watchlists.map(watchlist => <> deleteWatchlist(watchlist.token).then(refreshWatchlists)} okText={t`Yes`} cancelText={t`No`} > }> {t`Domain name`} : {watchlist?.domains.map(d => d.ldhName).join(',')} { watchlist.triggers && {t`Domain triggers`} : {watchlist.triggers.map(t => `${t.event} => ${t.action}`).join(',')} } )} }
}