2024-12-30 23:50:15 +01:00
|
|
|
import React, {useEffect, useState} from 'react'
|
2025-10-25 19:23:15 +02:00
|
|
|
import {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 {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'
|
2025-10-25 19:23:15 +02:00
|
|
|
import {CreateWatchlistButton} from "../../components/tracking/watchlist/CreateWatchlistButton"
|
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[]
|
2025-10-19 21:37:52 +02:00
|
|
|
trackedEppStatus: string[]
|
2024-12-30 23:50:15 +01:00
|
|
|
connector?: string
|
2024-08-17 22:20:42 +02:00
|
|
|
dsn?: string[]
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 19:23:15 +02:00
|
|
|
const getRequestDataFromFormCreation = (values: FormValuesType) =>
|
|
|
|
|
({ name: values.name,
|
|
|
|
|
domains: values.domains.map(d => '/api/domains/' + d.toLowerCase()),
|
2025-10-15 19:52:44 +02:00
|
|
|
trackedEvents: values.trackedEvents,
|
2025-10-19 21:37:52 +02:00
|
|
|
trackedEppStatus: values.trackedEppStatus,
|
2024-08-17 22:20:42 +02:00
|
|
|
connector: values.connector !== undefined ? ('/api/connectors/' + values.connector) : undefined,
|
2025-10-25 19:23:15 +02:00
|
|
|
dsn: values.dsn,
|
|
|
|
|
enabled: true
|
|
|
|
|
})
|
2024-08-17 22:20:42 +02:00
|
|
|
|
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
|
|
|
|
2025-10-25 19:23:15 +02:00
|
|
|
const onCreateWatchlist = async (values: FormValuesType) => await 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-10-25 19:23:15 +02:00
|
|
|
|
2024-12-30 23:50:15 +01:00
|
|
|
{(connectors != null) && (watchlists != null) && watchlists.length > 0 &&
|
2025-10-25 19:23:15 +02:00
|
|
|
<>
|
|
|
|
|
<CreateWatchlistButton onUpdateWatchlist={onCreateWatchlist} connectors={connectors} />
|
|
|
|
|
<Divider/>
|
|
|
|
|
<WatchlistsList
|
|
|
|
|
watchlists={watchlists}
|
|
|
|
|
onChange={refreshWatchlists}
|
|
|
|
|
connectors={connectors}
|
|
|
|
|
onUpdateWatchlist={onUpdateWatchlist}
|
|
|
|
|
/>
|
|
|
|
|
</>}
|
2024-12-30 23:50:15 +01:00
|
|
|
</Flex>
|
|
|
|
|
)
|
|
|
|
|
}
|