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

113 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-07-27 18:53:07 +02:00
import React, {useEffect, useState} from "react";
2024-07-31 12:37:43 +02:00
import {Card, Divider, Flex, Form, message} from "antd";
import {EventAction, getWatchlists, postWatchlist, putWatchlist} from "../../utils/api";
2024-07-27 18:53:07 +02:00
import {AxiosError} from "axios";
2024-07-28 15:36:22 +02:00
import {t} from 'ttag'
2024-08-16 13:56:52 +02:00
import {WatchlistForm} from "../../components/tracking/watchlist/WatchlistForm";
import {WatchlistsList} from "../../components/tracking/watchlist/WatchlistsList";
2024-07-30 00:55:36 +02:00
import {Connector, getConnectors} from "../../utils/api/connectors";
2024-08-07 15:57:16 +02:00
import {showErrorAPI} from "../../utils";
2024-07-27 18:53:07 +02:00
2024-08-02 17:23:27 +02:00
export type Watchlist = {
2024-08-03 19:16:58 +02:00
name?: string
2024-08-02 17:23:27 +02:00
token: string,
domains: { ldhName: string }[],
triggers?: { event: EventAction, action: string }[],
2024-08-16 23:57:52 +02:00
dsn?: string[]
connector?: {
id: string
provider: string
createdAt: string
}
createdAt: string
2024-08-02 17:23:27 +02:00
}
2024-07-27 20:44:10 +02:00
2024-08-17 22:20:42 +02:00
type FormValuesType = {
name?: string
domains: string[],
triggers: string[]
connector?: string,
dsn?: string[]
}
const getRequestDataFromForm = (values: FormValuesType) => {
const domainsURI = values.domains.map(d => '/api/domains/' + d.toLowerCase())
let triggers = values.triggers.map(t => ({event: t, action: 'email'}))
if (values.dsn !== undefined) {
triggers = [...triggers, ...values.triggers.map(t => ({
event: t,
action: 'chat'
}))]
}
return {
name: values.name,
domains: domainsURI,
triggers,
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-29 00:04:45 +02:00
2024-07-27 18:53:07 +02:00
const [form] = Form.useForm()
const [messageApi, contextHolder] = message.useMessage()
2024-07-27 20:44:10 +02:00
const [watchlists, setWatchlists] = useState<Watchlist[] | null>()
2024-07-30 00:55:36 +02:00
const [connectors, setConnectors] = useState<(Connector & { id: string })[] | null>()
2024-07-27 18:53:07 +02:00
2024-08-17 22:20:42 +02:00
const onCreateWatchlist = (values: FormValuesType) => {
2024-08-16 23:57:52 +02:00
2024-08-17 22:20:42 +02:00
postWatchlist(getRequestDataFromForm(values)).then((w) => {
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-08-17 22:20:42 +02:00
const onUpdateWatchlist = async (values: FormValuesType & { token: string }) => putWatchlist({
2024-08-15 03:04:31 +02:00
token: values.token,
2024-08-17 22:20:42 +02:00
...getRequestDataFromForm(values)
}
).then((w) => {
refreshWatchlists()
messageApi.success(t`Watchlist updated !`)
}).catch((e: AxiosError) => {
throw showErrorAPI(e, messageApi)
})
2024-08-15 03:04:31 +02:00
2024-07-27 18:53:07 +02:00
const refreshWatchlists = () => getWatchlists().then(w => {
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
}, [])
return <Flex gap="middle" align="center" justify="center" vertical>
2024-08-15 03:04:31 +02:00
{contextHolder}
{
connectors &&
<Card title={t`Create a Watchlist`} style={{width: '100%'}}>
<WatchlistForm form={form} onFinish={onCreateWatchlist} connectors={connectors} isCreation={true}/>
</Card>
}
2024-07-31 12:37:43 +02:00
<Divider/>
2024-08-15 03:04:31 +02:00
{connectors && watchlists && watchlists.length > 0 &&
<WatchlistsList watchlists={watchlists} onDelete={refreshWatchlists}
connectors={connectors}
onUpdateWatchlist={onUpdateWatchlist}
/>}
2024-07-27 18:53:07 +02:00
</Flex>
2024-07-27 17:19:25 +02:00
}