refactor: clarify frontend model ownership
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { IncomeAnalytics, useIncomeAnalytics } from "@/entities/income";
|
||||
import type { IncomeAnalytics, IncomeAnalyticsMeta } from "@/entities/income";
|
||||
|
||||
import { formatDate } from "@/shared/lib";
|
||||
|
||||
@@ -29,8 +29,6 @@ type IncomeDisplayModel = {
|
||||
yearNotice: ComparisonNotice;
|
||||
};
|
||||
|
||||
type IncomeMeta = ReturnType<typeof useIncomeAnalytics>["meta"];
|
||||
|
||||
function comparisonDisplay(
|
||||
period: IncomePeriod,
|
||||
comparison: IncomeAnalytics["yearComparison"],
|
||||
@@ -63,7 +61,7 @@ function comparisonDisplay(
|
||||
|
||||
function comparisonNotice(
|
||||
dependency:
|
||||
NonNullable<NonNullable<IncomeMeta>["dependencies"]>[number] | undefined,
|
||||
IncomeAnalyticsMeta["dependencies"][number] | undefined,
|
||||
unavailable: "previousUnavailable" | "yearUnavailable",
|
||||
): ComparisonNotice {
|
||||
if (!dependency || dependency.unavailableReason) return unavailable;
|
||||
@@ -98,7 +96,7 @@ function formatIncomeChange(
|
||||
|
||||
function toIncomeDisplayModel(
|
||||
analytics: IncomeAnalytics,
|
||||
meta?: IncomeMeta,
|
||||
meta?: IncomeAnalyticsMeta,
|
||||
): IncomeDisplayModel | null {
|
||||
const instruments = new Map<
|
||||
string,
|
||||
|
||||
@@ -5,10 +5,8 @@ import { AccountIncomePageError } from "./components/AccountIncomePageError.view
|
||||
import { AccountIncomePageLoaded } from "./components/AccountIncomePageLoaded.view";
|
||||
import { AccountIncomePageLoading } from "./components/AccountIncomePageLoading.view";
|
||||
|
||||
type AccountIncomePageProps = { accountId?: string };
|
||||
|
||||
const AccountIncomePage: FC<AccountIncomePageProps> = (props) => {
|
||||
const viewModel = useAccountIncomePageViewModel(props.accountId);
|
||||
const AccountIncomePage: FC = () => {
|
||||
const viewModel = useAccountIncomePageViewModel();
|
||||
if (viewModel.state === "loading") return <AccountIncomePageLoading />;
|
||||
|
||||
return viewModel.state === "error"
|
||||
|
||||
@@ -44,11 +44,8 @@ type AccountIncomePageViewModel
|
||||
}
|
||||
| { state: "loading" };
|
||||
|
||||
const useAccountIncomePageViewModel = (
|
||||
scopeId?: string,
|
||||
): AccountIncomePageViewModel => {
|
||||
const { accountId: routeAccountId } = useParams();
|
||||
const accountId = scopeId ?? routeAccountId ?? "";
|
||||
const useAccountIncomePageViewModel = (): AccountIncomePageViewModel => {
|
||||
const { accountId = "" } = useParams();
|
||||
const [searchParameters, setSearchParameters] = useSearchParams();
|
||||
|
||||
const requested = useMemo(
|
||||
@@ -83,7 +80,7 @@ const useAccountIncomePageViewModel = (
|
||||
}, [period, requested, setSearchParameters]);
|
||||
useEffect(() => {
|
||||
form.reset(draftPeriod(period));
|
||||
}, [form, period]);
|
||||
}, [accountId, form, period]);
|
||||
|
||||
if (analytics.isLoading || !analytics.data) {
|
||||
return analytics.isError || analytics.meta?.sync.state === "failed"
|
||||
@@ -117,7 +114,7 @@ const useAccountIncomePageViewModel = (
|
||||
analytics: analytics.data,
|
||||
display,
|
||||
form,
|
||||
formRevision: `${period.from}:${period.to}:${resetRevision}`,
|
||||
formRevision: `${accountId}:${period.from}:${period.to}:${resetRevision}`,
|
||||
lastSuccessfulAt: analytics.meta?.lastSuccessfulAt,
|
||||
onReset,
|
||||
onSubmit,
|
||||
|
||||
Reference in New Issue
Block a user