feat(cards): refine controls and modal transitions
This commit is contained in:
@@ -10,53 +10,20 @@ import {
|
||||
Text,
|
||||
Title,
|
||||
} from "@mantine/core";
|
||||
import { DatePickerInput } from "@mantine/dates";
|
||||
import { type FC, useLayoutEffect, useRef, useState } from "react";
|
||||
import { type FC, useLayoutEffect, useRef } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Link } from "react-router";
|
||||
import {
|
||||
BankCardFace,
|
||||
BankFeedback,
|
||||
BankOperationIcon,
|
||||
BankPeriodPicker,
|
||||
BankSummaryView,
|
||||
} from "@/entities/bank-card";
|
||||
import { EntryCard, LoadingSkeleton, StateView } from "@/shared/ui";
|
||||
import classes from "./CardPage.module.css";
|
||||
import type { CardPageVm } from "./CardPage.vm";
|
||||
|
||||
type PeriodPickerProps = {
|
||||
period: CardPageVm["period"];
|
||||
onChange: CardPageVm["setPeriod"];
|
||||
};
|
||||
const PeriodPicker: FC<PeriodPickerProps> = (props) => {
|
||||
const { period, onChange } = props;
|
||||
const { t } = useTranslation();
|
||||
const scope = `${period.from}-${period.to}`;
|
||||
const [draft, setDraft] = useState<{
|
||||
scope: string;
|
||||
value: [string | null, string | null];
|
||||
}>();
|
||||
const value: [string | null, string | null] =
|
||||
draft?.scope === scope ? draft.value : [period.from, period.to];
|
||||
return (
|
||||
<DatePickerInput
|
||||
type="range"
|
||||
allowSingleDateInRange
|
||||
label={t("bank.period")}
|
||||
valueFormat="DD.MM.YYYY"
|
||||
value={value}
|
||||
onChange={(next) => {
|
||||
setDraft({ scope, value: next });
|
||||
onChange(next);
|
||||
}}
|
||||
popoverProps={{
|
||||
returnFocus: true,
|
||||
middlewares: { shift: { crossAxis: true } },
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
type CardPageViewProps = { vm: CardPageVm };
|
||||
const CardPageView: FC<CardPageViewProps> = (props) => {
|
||||
const { vm } = props;
|
||||
@@ -210,7 +177,7 @@ const CardPageView: FC<CardPageViewProps> = (props) => {
|
||||
retry={() => void vm.cards.refetch()}
|
||||
/>
|
||||
)}
|
||||
<PeriodPicker period={vm.period} onChange={vm.setPeriod} />
|
||||
<BankPeriodPicker period={vm.period} onChange={vm.setPeriod} />
|
||||
<BankSummaryView
|
||||
data={vm.summary.data}
|
||||
loading={vm.summary.isLoading}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import dayjs from "dayjs";
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useParams, useSearchParams } from "react-router";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
type BankOperation,
|
||||
bankMonth,
|
||||
bankPeriod,
|
||||
useBankCard,
|
||||
useBankCards,
|
||||
useBankMutations,
|
||||
@@ -15,29 +13,13 @@ import {
|
||||
import { apiErrorCode } from "@/shared/api";
|
||||
import { formatDate, formatMoney } from "@/shared/lib";
|
||||
|
||||
const calendarDate = z.string().date();
|
||||
|
||||
const PAGE_SIZES = ["5", "10", "20", "50", "100", "all"];
|
||||
|
||||
function useCardPageVm() {
|
||||
const { cardId } = useParams();
|
||||
const [params, setParams] = useSearchParams();
|
||||
const { t } = useTranslation();
|
||||
const month = bankMonth(params.get("month"));
|
||||
const from = params.get("from");
|
||||
const to = params.get("to");
|
||||
const period =
|
||||
from &&
|
||||
to &&
|
||||
calendarDate.safeParse(from).success &&
|
||||
calendarDate.safeParse(to).success &&
|
||||
from >= "0001-01-01" &&
|
||||
from <= to
|
||||
? { from, to }
|
||||
: {
|
||||
from: `${month}-01`,
|
||||
to: dayjs(`${month}-01`).endOf("month").format("YYYY-MM-DD"),
|
||||
};
|
||||
const period = bankPeriod(params);
|
||||
const requestedSize = params.get("limit") ?? "10";
|
||||
const pageSize = PAGE_SIZES.includes(requestedSize) ? requestedSize : "10";
|
||||
const rawPage = Number(params.get("page") ?? 1);
|
||||
|
||||
@@ -13,6 +13,7 @@ import { formatDate, formatMoney } from "@/shared/lib";
|
||||
type DeleteOperationProps = { operation: BankOperation; onClose: () => void };
|
||||
const DeleteOperation: FC<DeleteOperationProps> = (props) => {
|
||||
useBankFormFocus();
|
||||
const [opened, setOpened] = useState(true);
|
||||
const { t } = useTranslation();
|
||||
const [record, setRecord] = useState(props.operation);
|
||||
const [latest, setLatest] = useState<BankOperation>();
|
||||
@@ -20,11 +21,14 @@ const DeleteOperation: FC<DeleteOperationProps> = (props) => {
|
||||
const query = useBankOperation(record.id);
|
||||
const { deleteOperation } = useBankMutations();
|
||||
const pending =
|
||||
deleteOperation.isPending || (query.isFetching && Boolean(error));
|
||||
!opened ||
|
||||
deleteOperation.isPending ||
|
||||
(query.isFetching && Boolean(error));
|
||||
return (
|
||||
<Modal
|
||||
opened
|
||||
onClose={() => !pending && props.onClose()}
|
||||
opened={opened}
|
||||
onExitTransitionEnd={props.onClose}
|
||||
onClose={() => !pending && setOpened(false)}
|
||||
title={t("bank.deleteTitle")}
|
||||
closeOnEscape={!pending}
|
||||
closeOnClickOutside={!pending}
|
||||
@@ -91,7 +95,7 @@ const DeleteOperation: FC<DeleteOperationProps> = (props) => {
|
||||
id: record.id,
|
||||
version: record.version,
|
||||
});
|
||||
props.onClose();
|
||||
setOpened(false);
|
||||
} catch (failure) {
|
||||
setError(failure);
|
||||
}
|
||||
@@ -99,7 +103,11 @@ const DeleteOperation: FC<DeleteOperationProps> = (props) => {
|
||||
>
|
||||
{t("bank.delete")}
|
||||
</Button>
|
||||
<Button variant="default" disabled={pending} onClick={props.onClose}>
|
||||
<Button
|
||||
variant="default"
|
||||
disabled={pending}
|
||||
onClick={() => setOpened(false)}
|
||||
>
|
||||
{t("bank.cancel")}
|
||||
</Button>
|
||||
</Group>
|
||||
|
||||
@@ -47,6 +47,7 @@ type OperationFormProps = {
|
||||
};
|
||||
const OperationForm: FC<OperationFormProps> = (props) => {
|
||||
useBankFormFocus();
|
||||
const [opened, setOpened] = useState(true);
|
||||
const { t } = useTranslation();
|
||||
const [categoriesOpen, setCategoriesOpen] = useState(false);
|
||||
const [version, setVersion] = useState(props.operation?.version ?? 1);
|
||||
@@ -164,15 +165,16 @@ const OperationForm: FC<OperationFormProps> = (props) => {
|
||||
{ page: sourcePage },
|
||||
);
|
||||
const source = methods.watch("cardId");
|
||||
const pending = methods.formState.isSubmitting;
|
||||
const pending = !opened || methods.formState.isSubmitting;
|
||||
const options = props.cards
|
||||
.filter((c) => !c.archivedAt)
|
||||
.map((c) => ({ value: c.id, label: c.name }));
|
||||
return (
|
||||
<>
|
||||
<Modal
|
||||
opened
|
||||
onClose={() => !pending && !categoriesOpen && props.onClose()}
|
||||
opened={opened}
|
||||
onExitTransitionEnd={props.onClose}
|
||||
onClose={() => !pending && !categoriesOpen && setOpened(false)}
|
||||
title={t(props.operation ? "bank.editOperation" : "bank.addOperation")}
|
||||
closeOnEscape={!pending && !categoriesOpen}
|
||||
closeOnClickOutside={!pending && !categoriesOpen}
|
||||
@@ -217,7 +219,7 @@ const OperationForm: FC<OperationFormProps> = (props) => {
|
||||
id: intent,
|
||||
...body,
|
||||
});
|
||||
props.onClose();
|
||||
setOpened(false);
|
||||
} catch (failure) {
|
||||
setError(failure);
|
||||
}
|
||||
@@ -392,7 +394,7 @@ const OperationForm: FC<OperationFormProps> = (props) => {
|
||||
>
|
||||
{t("bank.save")}
|
||||
</Button>
|
||||
<Button variant="default" onClick={props.onClose}>
|
||||
<Button variant="default" onClick={() => setOpened(false)}>
|
||||
{t("bank.cancel")}
|
||||
</Button>
|
||||
</Group>
|
||||
|
||||
Reference in New Issue
Block a user