feat: add operations page
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { MantineProvider } from "@mantine/core";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { renderHook, waitFor } from "@testing-library/react";
|
||||
import type { ReactNode } from "react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
brokerageOperationsQueryParams,
|
||||
useBrokerageOperations,
|
||||
} from "./useBrokerageOperations";
|
||||
|
||||
describe("useBrokerageOperations", () => {
|
||||
it("normalizes query parameters and loads a page without retries", async () => {
|
||||
expect(
|
||||
brokerageOperationsQueryParams({
|
||||
categories: ["tax", "income"],
|
||||
cursor: "next",
|
||||
}),
|
||||
).toEqual({ categories: ["income", "tax"], cursor: "next" });
|
||||
const client = new QueryClient();
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<MantineProvider>
|
||||
<QueryClientProvider client={client}>{children}</QueryClientProvider>
|
||||
</MantineProvider>
|
||||
);
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useBrokerageOperations("account-1", "token", { limit: 5 }),
|
||||
{ wrapper },
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
expect(result.current.data).toMatchObject({ items: [] });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { apiQueryClient } from "@/shared/api";
|
||||
import type { paths } from "@/shared/api/generated/schema";
|
||||
|
||||
type OperationsEndpoint =
|
||||
paths["/api/v1/brokerage-accounts/{accountId}/operations"]["get"];
|
||||
|
||||
export type BrokerageOperation =
|
||||
OperationsEndpoint["responses"][200]["content"]["application/json"]["items"][number];
|
||||
type BrokerageOperationsQuery = NonNullable<
|
||||
OperationsEndpoint["parameters"]["query"]
|
||||
>;
|
||||
export type OperationCategory = BrokerageOperation["category"];
|
||||
|
||||
export const operationCategories = [
|
||||
"trade",
|
||||
"income",
|
||||
"tax",
|
||||
"fee",
|
||||
"transfer",
|
||||
"other",
|
||||
] as const satisfies readonly OperationCategory[];
|
||||
|
||||
export function brokerageOperationsQueryParams(
|
||||
query: BrokerageOperationsQuery,
|
||||
) {
|
||||
return {
|
||||
...(query.from ? { from: query.from } : {}),
|
||||
...(query.to ? { to: query.to } : {}),
|
||||
...(query.categories?.length
|
||||
? { categories: [...query.categories].sort() }
|
||||
: {}),
|
||||
...(query.cursor ? { cursor: query.cursor } : {}),
|
||||
...(query.limit ? { limit: query.limit } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function useBrokerageOperations(
|
||||
accountId: string | undefined,
|
||||
accessToken: string | null,
|
||||
query: BrokerageOperationsQuery = {},
|
||||
) {
|
||||
const params = brokerageOperationsQueryParams(query);
|
||||
return useQuery({
|
||||
queryKey: ["brokerage-operations", accountId ?? "", params] as const,
|
||||
enabled: Boolean(accountId && accessToken),
|
||||
staleTime: 30_000,
|
||||
retry: false,
|
||||
...apiQueryClient.queryOptions(
|
||||
"get",
|
||||
"/api/v1/brokerage-accounts/{accountId}/operations",
|
||||
{ params: { path: { accountId: accountId ?? "" }, query: params } },
|
||||
),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export type {
|
||||
BrokerageOperation,
|
||||
OperationCategory,
|
||||
} from "./api/useBrokerageOperations";
|
||||
export {
|
||||
operationCategories,
|
||||
useBrokerageOperations,
|
||||
} from "./api/useBrokerageOperations";
|
||||
Reference in New Issue
Block a user