refactor: enforce frontend view model boundaries
This commit is contained in:
+39
-64
@@ -8,19 +8,11 @@ import {
|
||||
Stack,
|
||||
Text,
|
||||
} from "@mantine/core";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { ApiErrorCode, apiErrorCode, apiQueryClient, isApiErrorCode } from "@/shared/api";
|
||||
import { ApiErrorCode, isApiErrorCode } from "@/shared/api";
|
||||
|
||||
import {
|
||||
commitTbankConnectionBoundary,
|
||||
type TbankConnectionStatus,
|
||||
} from "../../model/connection-boundary";
|
||||
|
||||
enum ConnectionErrorCode { RequestFailed = "REQUEST_ERROR" }
|
||||
import { type TbankConnectionStatus } from "../../model/connection-boundary";
|
||||
import { useTbankConnectionSectionLoadedVm } from "./TbankConnectionSectionLoaded.vm";
|
||||
|
||||
type TbankConnectionSectionLoadedProps = {
|
||||
refreshError: boolean;
|
||||
@@ -32,48 +24,29 @@ type TbankConnectionSectionLoadedProps = {
|
||||
const TbankConnectionSectionLoaded: FC<TbankConnectionSectionLoadedProps> = (
|
||||
props,
|
||||
) => {
|
||||
const { status } = props;
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
const [isDeleteOpened, setIsDeleteOpened] = useState(false);
|
||||
const removalButtonRef = useRef<HTMLButtonElement>(null);
|
||||
const [requestError, setRequestError] = useState<null | string>(null);
|
||||
const {
|
||||
formState: { errors },
|
||||
handleSubmit,
|
||||
errors,
|
||||
isConnected,
|
||||
isDeleteOpened,
|
||||
isPending,
|
||||
onCloseDisconnect,
|
||||
onConfirmDisconnect,
|
||||
onOpenDisconnect,
|
||||
onSubmit,
|
||||
register,
|
||||
reset,
|
||||
} = useForm<{ token: string }>();
|
||||
const update = apiQueryClient.useMutation("put", "/api/v1/tbank-connection", {
|
||||
onError: error => setRequestError(apiErrorCode(error) ?? ConnectionErrorCode.RequestFailed),
|
||||
onSuccess: (data) => {
|
||||
commitTbankConnectionBoundary(queryClient, data);
|
||||
reset();
|
||||
},
|
||||
});
|
||||
const remove = apiQueryClient.useMutation(
|
||||
"delete",
|
||||
"/api/v1/tbank-connection",
|
||||
{
|
||||
onSuccess: () => {
|
||||
commitTbankConnectionBoundary(queryClient, {
|
||||
connected: false,
|
||||
connectedAt: /* eslint-disable-line unicorn/no-null -- The disconnected API snapshot requires a null connection timestamp. */ null,
|
||||
});
|
||||
setIsDeleteOpened(false);
|
||||
},
|
||||
},
|
||||
);
|
||||
const unavailableMessage = isApiErrorCode(requestError, ApiErrorCode.TbankUnavailable) ? "tbank.unavailable" : "tbank.requestError";
|
||||
const isPending = update.isPending || remove.isPending;
|
||||
const isConnected = status.connected;
|
||||
useEffect(() => {
|
||||
if (isDeleteOpened && remove.isError) removalButtonRef.current?.focus();
|
||||
}, [isDeleteOpened, remove.isError]);
|
||||
removalButtonRef,
|
||||
remove,
|
||||
requestError,
|
||||
status,
|
||||
t,
|
||||
unavailableMessage,
|
||||
update,
|
||||
} = useTbankConnectionSectionLoadedVm(props);
|
||||
|
||||
return (
|
||||
<Stack mt="xl">
|
||||
<Text fw={600}>{t("tbank.title")}</Text>
|
||||
|
||||
<Text size="sm">
|
||||
{isConnected
|
||||
? t("tbank.connected", {
|
||||
@@ -83,18 +56,22 @@ const TbankConnectionSectionLoaded: FC<TbankConnectionSectionLoadedProps> = (
|
||||
})
|
||||
: t("tbank.disconnected")}
|
||||
</Text>
|
||||
|
||||
<Text c="dimmed" size="sm">
|
||||
{t("tbank.hint")}
|
||||
</Text>
|
||||
|
||||
{props.refreshing && (
|
||||
<Text c="dimmed" role="status" size="sm">
|
||||
{t("tbank.refreshing")}
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{props.refreshError && (
|
||||
<Alert aria-live="polite" color="orange" role="status">
|
||||
<Stack gap="sm">
|
||||
<Text>{t("tbank.refreshError")}</Text>
|
||||
|
||||
<Button
|
||||
disabled={props.refreshing || isPending}
|
||||
onClick={props.retry}
|
||||
@@ -105,12 +82,8 @@ const TbankConnectionSectionLoaded: FC<TbankConnectionSectionLoadedProps> = (
|
||||
</Stack>
|
||||
</Alert>
|
||||
)}
|
||||
<form
|
||||
onSubmit={handleSubmit(({ token }) => {
|
||||
setRequestError(null);
|
||||
update.mutate({ body: { token } });
|
||||
})}
|
||||
>
|
||||
|
||||
<form onSubmit={onSubmit}>
|
||||
<fieldset disabled={isPending}>
|
||||
<Stack>
|
||||
<PasswordInput
|
||||
@@ -118,6 +91,7 @@ const TbankConnectionSectionLoaded: FC<TbankConnectionSectionLoadedProps> = (
|
||||
{...register("token", { required: t("tbank.tokenRequired") })}
|
||||
error={errors.token?.message}
|
||||
/>
|
||||
|
||||
{requestError && (
|
||||
<Alert color="red">
|
||||
{t(
|
||||
@@ -127,42 +101,43 @@ const TbankConnectionSectionLoaded: FC<TbankConnectionSectionLoadedProps> = (
|
||||
)}
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<Button loading={update.isPending} type="submit">
|
||||
{t(isConnected ? "tbank.replace" : "tbank.connect")}
|
||||
</Button>
|
||||
|
||||
{isConnected && (
|
||||
<Button
|
||||
color="red"
|
||||
onClick={() => {
|
||||
remove.reset();
|
||||
setIsDeleteOpened(true);
|
||||
}}
|
||||
variant="outline"
|
||||
>
|
||||
<Button color="red" onClick={onOpenDisconnect} variant="outline">
|
||||
{t("tbank.delete")}
|
||||
</Button>
|
||||
)}
|
||||
</Stack>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
<Modal
|
||||
closeButtonProps={{ "aria-label": t("tbank.close"), "disabled": isPending }}
|
||||
closeButtonProps={{
|
||||
"aria-label": t("tbank.close"),
|
||||
"disabled": isPending,
|
||||
}}
|
||||
closeOnClickOutside={!isPending}
|
||||
closeOnEscape={!isPending}
|
||||
onClose={() => !isPending && setIsDeleteOpened(false)}
|
||||
onClose={onCloseDisconnect}
|
||||
opened={isDeleteOpened}
|
||||
title={t("tbank.deleteTitle")}
|
||||
>
|
||||
<Stack>
|
||||
<Text>{t("tbank.deleteText")}</Text>
|
||||
|
||||
{remove.isError && (
|
||||
<Alert color="red">{t("tbank.requestError")}</Alert>
|
||||
)}
|
||||
|
||||
<Button
|
||||
color="red"
|
||||
disabled={isPending}
|
||||
loading={remove.isPending}
|
||||
onClick={() => remove.mutate({})}
|
||||
onClick={onConfirmDisconnect}
|
||||
ref={removalButtonRef}
|
||||
>
|
||||
{t("tbank.deleteConfirm")}
|
||||
|
||||
Reference in New Issue
Block a user