diff --git a/assets/components/LoginForm.tsx b/assets/components/LoginForm.tsx index efef2ea..65d3a08 100644 --- a/assets/components/LoginForm.tsx +++ b/assets/components/LoginForm.tsx @@ -1,9 +1,10 @@ -import {Alert, Button, Form, Input, Space} from "antd"; +import {Button, Form, Input, message, Space} from "antd"; import {t} from "ttag"; -import React, {useContext, useEffect, useState} from "react"; +import React, {useContext, useEffect} from "react"; import {getUser, login} from "../utils/api"; import {AuthenticatedContext} from "../pages/LoginPage"; import {useNavigate} from "react-router-dom"; +import {showErrorAPI} from "../utils"; type FieldType = { @@ -13,8 +14,8 @@ type FieldType = { export function LoginForm({ssoLogin}: { ssoLogin?: boolean }) { - const [error, setError] = useState() const navigate = useNavigate() + const [messageApi, contextHolder] = message.useMessage() const {setIsAuthenticated} = useContext(AuthenticatedContext) @@ -33,23 +34,11 @@ export function LoginForm({ssoLogin}: { ssoLogin?: boolean }) { navigate('/home') }).catch((e) => { setIsAuthenticated(false) - if (e.response.data.message !== undefined) { - setError(e.response.data.message) - } else { - setError(e.response.data['hydra:description']) - } + showErrorAPI(e, messageApi) }) } return <> - {error && - } + {contextHolder}
() const navigate = useNavigate() + const [messageApi, contextHolder] = message.useMessage() const onFinish = (data: FieldType) => { register(data.username, data.password).then(() => { navigate('/home') }).catch((e) => { - - if (e.response?.status === 429) { - const duration = e.response.headers['retry-after'] - setError(t`Please retry after ${duration} seconds`) - return; - } - - if (e.response.data.message !== undefined) { - setError(e.response.data.message) - } else { - setError(e.response.data['hydra:description']) - } + showErrorAPI(e, messageApi) }) } return <> - {error && - } + {contextHolder} { setDomain(undefined) - - if (e.response?.status === 429) { - const duration = e.response.headers['retry-after'] - messageApi.error(t`Please retry after ${duration} seconds`) - return; - } - const data = e?.response?.data as { detail: string } - messageApi.error(data.detail !== '' ? data.detail : t`An error occurred`) + showErrorAPI(e, messageApi) }) } diff --git a/assets/pages/tracking/ConnectorsPage.tsx b/assets/pages/tracking/ConnectorsPage.tsx index 714e2a9..b4ebb13 100644 --- a/assets/pages/tracking/ConnectorsPage.tsx +++ b/assets/pages/tracking/ConnectorsPage.tsx @@ -5,6 +5,7 @@ import {Connector, getConnectors, postConnector} from "../../utils/api/connector import {ConnectorForm} from "../../components/tracking/ConnectorForm"; import {AxiosError} from "axios"; import {ConnectorElement, ConnectorsList} from "../../components/tracking/ConnectorsList"; +import {showErrorAPI} from "../../utils"; export default function ConnectorsPage() { const [form] = Form.useForm() @@ -17,17 +18,15 @@ export default function ConnectorsPage() { refreshConnectors() messageApi.success(t`Connector created !`) }).catch((e: AxiosError) => { - const data = e?.response?.data as { detail: string } - messageApi.error(data.detail ?? t`An error occurred`) + showErrorAPI(e, messageApi) }) } const refreshConnectors = () => getConnectors().then(c => { setConnectors(c['hydra:member']) }).catch((e: AxiosError) => { - const data = e?.response?.data as { detail: string } - messageApi.error(data.detail ?? t`An error occurred`) setConnectors(undefined) + showErrorAPI(e, messageApi) }) useEffect(() => { diff --git a/assets/pages/tracking/WatchlistPage.tsx b/assets/pages/tracking/WatchlistPage.tsx index fa7bd47..5fbd285 100644 --- a/assets/pages/tracking/WatchlistPage.tsx +++ b/assets/pages/tracking/WatchlistPage.tsx @@ -6,6 +6,7 @@ import {t} from 'ttag' import {WatchlistForm} from "../../components/tracking/WatchlistForm"; import {WatchlistsList} from "../../components/tracking/WatchlistsList"; import {Connector, getConnectors} from "../../utils/api/connectors"; +import {showErrorAPI} from "../../utils"; export type Watchlist = { @@ -45,17 +46,15 @@ export default function WatchlistPage() { 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`) + showErrorAPI(e, messageApi) }) } const refreshWatchlists = () => getWatchlists().then(w => { setWatchlists(w['hydra:member']) }).catch((e: AxiosError) => { - const data = e?.response?.data as { detail: string } - messageApi.error(data?.detail ?? t`An error occurred`) setWatchlists(undefined) + showErrorAPI(e, messageApi) }) useEffect(() => { @@ -63,8 +62,7 @@ export default function WatchlistPage() { getConnectors() .then(c => setConnectors(c['hydra:member'])) .catch((e: AxiosError) => { - const data = e?.response?.data as { detail: string } - messageApi.error(data?.detail ?? t`An error occurred`) + showErrorAPI(e, messageApi) }) }, []) diff --git a/assets/utils/index.ts b/assets/utils/index.ts new file mode 100644 index 0000000..f3e2911 --- /dev/null +++ b/assets/utils/index.ts @@ -0,0 +1,27 @@ +import {MessageInstance, MessageType} from "antd/lib/message/interface"; +import {AxiosError, AxiosResponse} from "axios"; +import {t} from "ttag"; + +export function showErrorAPI(e: AxiosError, messageApi: MessageInstance): MessageType | undefined { + + const response = e.response as AxiosResponse + const data = response.data + + if ('message' in data) { + return messageApi.error(data.message as string) + } + + if (!('detail' in data)) return + const detail = data.detail as string + + if (response.status === 429) { + const duration = response.headers['retry-after'] + return messageApi.error(t`Please retry after ${duration} seconds`) + } + + if (response.status.toString()[0] === '4') { + return messageApi.warning(detail !== '' ? detail : t`An error occurred`) + } + + return messageApi.error(detail !== '' ? detail : t`An error occurred`) +} \ No newline at end of file diff --git a/translations/translations.pot b/translations/translations.pot index c570b11..690eb22 100644 --- a/translations/translations.pot +++ b/translations/translations.pot @@ -3,20 +3,15 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Plural-Forms: nplurals=2; plural=(n!=1);\n" -#: assets/components/LoginForm.tsx:47 -#: assets/components/RegisterForm.tsx:40 -msgid "Error" -msgstr "" - -#: assets/components/LoginForm.tsx:62 -#: assets/components/RegisterForm.tsx:55 +#: assets/components/LoginForm.tsx:51 +#: assets/components/RegisterForm.tsx:38 msgid "Email address" msgstr "" -#: assets/components/LoginForm.tsx:64 -#: assets/components/LoginForm.tsx:72 -#: assets/components/RegisterForm.tsx:57 -#: assets/components/RegisterForm.tsx:65 +#: assets/components/LoginForm.tsx:53 +#: assets/components/LoginForm.tsx:61 +#: assets/components/RegisterForm.tsx:40 +#: assets/components/RegisterForm.tsx:48 #: assets/components/search/DomainSearchBar.tsx:23 #: assets/components/tracking/ConnectorForm.tsx:40 #: assets/components/tracking/ConnectorForm.tsx:66 @@ -31,16 +26,16 @@ msgstr "" msgid "Required" msgstr "" -#: assets/components/LoginForm.tsx:70 -#: assets/components/RegisterForm.tsx:63 +#: assets/components/LoginForm.tsx:59 +#: assets/components/RegisterForm.tsx:46 msgid "Password" msgstr "" -#: assets/components/LoginForm.tsx:80 +#: assets/components/LoginForm.tsx:69 msgid "Submit" msgstr "" -#: assets/components/LoginForm.tsx:85 +#: assets/components/LoginForm.tsx:74 msgid "Log in with SSO" msgstr "" @@ -374,47 +369,32 @@ msgstr "" msgid "Log in" msgstr "" -#: assets/components/RegisterForm.tsx:25 -#: assets/pages/search/DomainSearchPage.tsx:26 -#, javascript-format -msgid "Please retry after ${ duration } seconds" -msgstr "" - -#: assets/components/RegisterForm.tsx:72 +#: assets/components/RegisterForm.tsx:55 #: assets/pages/LoginPage.tsx:30 msgid "Register" msgstr "" -#: assets/pages/search/DomainSearchPage.tsx:20 +#: assets/pages/search/DomainSearchPage.tsx:21 msgid "Found !" msgstr "" -#: assets/pages/search/DomainSearchPage.tsx:30 -#: assets/pages/tracking/ConnectorsPage.tsx:21 -#: assets/pages/tracking/ConnectorsPage.tsx:29 -#: assets/pages/tracking/WatchlistPage.tsx:49 -#: assets/pages/tracking/WatchlistPage.tsx:57 -#: assets/pages/tracking/WatchlistPage.tsx:67 -msgid "An error occurred" -msgstr "" - -#: assets/pages/search/DomainSearchPage.tsx:35 +#: assets/pages/search/DomainSearchPage.tsx:29 msgid "Domain finder" msgstr "" -#: assets/pages/search/DomainSearchPage.tsx:56 +#: assets/pages/search/DomainSearchPage.tsx:50 msgid "EPP Status Codes" msgstr "" -#: assets/pages/search/DomainSearchPage.tsx:66 +#: assets/pages/search/DomainSearchPage.tsx:60 msgid "Timeline" msgstr "" -#: assets/pages/search/DomainSearchPage.tsx:71 +#: assets/pages/search/DomainSearchPage.tsx:65 msgid "Entities" msgstr "" -#: assets/pages/search/DomainSearchPage.tsx:79 +#: assets/pages/search/DomainSearchPage.tsx:73 msgid "" "Although the domain exists in my database, it has been deleted from the " "WHOIS by its registrar." @@ -497,19 +477,19 @@ msgstr "" msgid "Roles" msgstr "" -#: assets/pages/tracking/ConnectorsPage.tsx:18 +#: assets/pages/tracking/ConnectorsPage.tsx:19 msgid "Connector created !" msgstr "" -#: assets/pages/tracking/ConnectorsPage.tsx:39 +#: assets/pages/tracking/ConnectorsPage.tsx:38 msgid "Create a Connector" msgstr "" -#: assets/pages/tracking/WatchlistPage.tsx:46 +#: assets/pages/tracking/WatchlistPage.tsx:47 msgid "Watchlist created !" msgstr "" -#: assets/pages/tracking/WatchlistPage.tsx:72 +#: assets/pages/tracking/WatchlistPage.tsx:70 msgid "Create a Watchlist" msgstr "" @@ -563,6 +543,16 @@ msgid "" "another" msgstr "" +#: assets/utils/index.ts:19 +#, javascript-format +msgid "Please retry after ${ duration } seconds" +msgstr "" + +#: assets/utils/index.ts:23 +#: assets/utils/index.ts:26 +msgid "An error occurred" +msgstr "" + #: assets/App.tsx:101 msgid "TOS" msgstr ""