domain-watchdog/assets/pages/tracking/WatchlistPage.tsx

96 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-12-30 23:50:15 +01:00
import React, {useEffect, useState} from 'react'
import {Card, Divider, Flex, Form, message} from 'antd'
2025-10-15 19:52:44 +02:00
import type {Watchlist} from '../../utils/api'
2024-12-31 13:55:42 +01:00
import {getWatchlists, postWatchlist, putWatchlist} from '../../utils/api'
import type {AxiosError} from 'axios'
2024-07-28 15:36:22 +02:00
import {t} from 'ttag'
2024-12-30 23:50:15 +01:00
import {WatchlistForm} from '../../components/tracking/watchlist/WatchlistForm'
import {WatchlistsList} from '../../components/tracking/watchlist/WatchlistsList'
2024-12-31 13:55:42 +01:00
import type {Connector} from '../../utils/api/connectors'
import { getConnectors} from '../../utils/api/connectors'
2024-08-22 01:44:50 +02:00
2024-12-30 23:50:15 +01:00
import {showErrorAPI} from '../../utils/functions/showErrorAPI'
2024-07-27 18:53:07 +02:00
2024-12-30 23:50:15 +01:00
interface FormValuesType {
2024-08-17 22:20:42 +02:00
name?: string
2024-12-30 23:50:15 +01:00
domains: string[]
2025-10-15 19:52:44 +02:00
trackedEvents: string[]
trackedEppStatus: string[]
2024-12-30 23:50:15 +01:00
connector?: string
2024-08-17 22:20:42 +02:00
dsn?: string[]
}
const getRequestDataFromFormCreation = (values: FormValuesType) => {
const domainsURI = values.domains.map(d => '/api/domains/' + d.toLowerCase())
2024-08-17 22:20:42 +02:00
return {
name: values.name,
domains: domainsURI,
2025-10-15 19:52:44 +02:00
trackedEvents: values.trackedEvents,
trackedEppStatus: values.trackedEppStatus,
2024-08-17 22:20:42 +02:00
connector: values.connector !== undefined ? ('/api/connectors/' + values.connector) : undefined,
dsn: values.dsn
}
}
2024-07-27 17:19:25 +02:00
export default function WatchlistPage() {
2024-07-27 18:53:07 +02:00
const [form] = Form.useForm()
const [messageApi, contextHolder] = message.useMessage()
2024-08-21 02:01:20 +02:00
const [watchlists, setWatchlists] = useState<Watchlist[]>()
2024-12-30 23:50:15 +01:00
const [connectors, setConnectors] = useState<Array<Connector & { id: string }>>()
2024-07-27 18:53:07 +02:00
2024-08-17 22:20:42 +02:00
const onCreateWatchlist = (values: FormValuesType) => {
postWatchlist(getRequestDataFromFormCreation(values)).then(() => {
2024-07-27 18:53:07 +02:00
form.resetFields()
refreshWatchlists()
2024-07-28 15:36:22 +02:00
messageApi.success(t`Watchlist created !`)
2024-07-27 18:53:07 +02:00
}).catch((e: AxiosError) => {
2024-08-07 15:57:16 +02:00
showErrorAPI(e, messageApi)
2024-07-27 18:53:07 +02:00
})
}
2024-12-30 23:50:15 +01:00
const onUpdateWatchlist = async (values: FormValuesType & { token: string }) => await putWatchlist({
2024-08-15 03:04:31 +02:00
token: values.token,
2025-10-15 19:52:44 +02:00
...getRequestDataFromFormCreation(values)
2024-08-17 22:20:42 +02:00
}
2024-12-30 23:50:15 +01:00
).then(() => {
2024-08-17 22:20:42 +02:00
refreshWatchlists()
messageApi.success(t`Watchlist updated !`)
}).catch((e: AxiosError) => {
throw showErrorAPI(e, messageApi)
})
2024-08-15 03:04:31 +02:00
2024-12-30 23:50:15 +01:00
const refreshWatchlists = async () => await getWatchlists().then(w => {
2024-07-27 18:53:07 +02:00
setWatchlists(w['hydra:member'])
2024-07-29 19:37:17 +02:00
}).catch((e: AxiosError) => {
setWatchlists(undefined)
2024-08-07 15:57:16 +02:00
showErrorAPI(e, messageApi)
2024-07-29 19:37:17 +02:00
})
2024-07-27 18:53:07 +02:00
useEffect(() => {
refreshWatchlists()
2024-07-31 02:55:20 +02:00
getConnectors()
.then(c => setConnectors(c['hydra:member']))
.catch((e: AxiosError) => {
2024-08-07 15:57:16 +02:00
showErrorAPI(e, messageApi)
2024-07-31 02:55:20 +02:00
})
2024-07-27 18:53:07 +02:00
}, [])
2024-12-30 23:50:15 +01:00
return (
<Flex gap='middle' align='center' justify='center' vertical>
{contextHolder}
2025-02-20 16:32:25 +01:00
<Card size='small' loading={connectors === undefined} title={t`Create a Watchlist`} style={{width: '100%'}}>
2024-12-30 23:50:15 +01:00
{(connectors != null) &&
<WatchlistForm form={form} onFinish={onCreateWatchlist} connectors={connectors} isCreation/>}
</Card>
<Divider/>
{(connectors != null) && (watchlists != null) && watchlists.length > 0 &&
<WatchlistsList
2025-05-22 14:00:17 +02:00
watchlists={watchlists}
onDelete={refreshWatchlists}
2024-12-30 23:50:15 +01:00
connectors={connectors}
onUpdateWatchlist={onUpdateWatchlist}
/>}
</Flex>
)
}