domain-watchdog/assets/pages/search/DomainSearchPage.tsx

60 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-12-30 23:50:15 +01:00
import React, {useEffect, useState} from 'react'
2024-12-31 13:55:42 +01:00
import type { FormProps} from 'antd'
import {Empty, Flex, message, Skeleton} from 'antd'
import type {Domain} from '../../utils/api'
import { getDomain} from '../../utils/api'
import type {AxiosError} from 'axios'
2024-07-28 15:36:22 +02:00
import {t} from 'ttag'
2024-12-31 13:55:42 +01:00
import type { FieldType} from '../../components/search/DomainSearchBar'
import {DomainSearchBar} from '../../components/search/DomainSearchBar'
2024-12-30 23:50:15 +01:00
import {DomainResult} from '../../components/search/DomainResult'
import {showErrorAPI} from '../../utils/functions/showErrorAPI'
import {useNavigate, useParams} from 'react-router-dom'
2024-07-27 14:40:08 +02:00
export default function DomainSearchPage() {
const {query} = useParams()
const [domain, setDomain] = useState<Domain | null>()
const [loading, setLoading] = useState<boolean>(false)
2024-07-27 01:35:00 +02:00
const [messageApi, contextHolder] = message.useMessage()
const navigate = useNavigate()
2024-07-27 01:35:00 +02:00
const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
navigate('/search/domain/' + values.ldhName)
if (loading) return
setLoading(true)
setDomain(null)
getDomain(values.ldhName).then(d => {
2024-07-27 16:45:20 +02:00
setDomain(d)
2024-07-28 15:36:22 +02:00
messageApi.success(t`Found !`)
2024-07-27 16:45:20 +02:00
}).catch((e: AxiosError) => {
setDomain(undefined)
2024-08-07 15:57:16 +02:00
showErrorAPI(e, messageApi)
}).finally(() => setLoading(false))
}
useEffect(() => {
if (query === undefined) return
onFinish({ldhName: query})
}, [])
2024-12-27 21:37:19 +01:00
2024-12-30 23:50:15 +01:00
return (
<Flex gap='middle' align='center' justify='center' vertical>
{contextHolder}
<DomainSearchBar initialValue={query} onFinish={onFinish}/>
2024-07-27 14:40:08 +02:00
2024-12-30 23:50:15 +01:00
<Skeleton loading={domain === null} active>
{
(domain != null) &&
(!domain.deleted
? <DomainResult domain={domain}/>
: <Empty
description={t`Although the domain exists in my database, it has been deleted from the WHOIS by its registrar.`}
/>)
}
</Skeleton>
</Flex>
)
}