feat: improve bank card expenses summary
This commit is contained in:
@@ -5,6 +5,8 @@ import {
|
||||
useAuthenticatedQuery,
|
||||
} from "@/shared/api";
|
||||
|
||||
import { moneyCents } from "../model/form-validation";
|
||||
|
||||
type BankCard =
|
||||
paths["/api/v1/bank-cards/{id}"]["get"]["responses"][200]["content"]["application/json"];
|
||||
type BankOperation =
|
||||
@@ -119,11 +121,25 @@ function useBankExpenses(
|
||||
});
|
||||
}
|
||||
|
||||
function sortBankSummary(data: BankSummary): BankSummary {
|
||||
return {
|
||||
...data,
|
||||
categories: data.categories.toSorted((a, b) => {
|
||||
const left = moneyCents(a.net.value);
|
||||
const right = moneyCents(b.net.value);
|
||||
if (left === null || right === null)
|
||||
throw new Error("Invalid bank summary amount");
|
||||
return left > right ? -1 : left < right ? 1 : 0;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function useBankSummary(month: string, cardId?: string) {
|
||||
return useAuthenticatedQuery({
|
||||
...apiQueryClient.queryOptions("get", "/api/v1/bank-cards/summary", {
|
||||
params: { query: { month, cardId } },
|
||||
}),
|
||||
select: sortBankSummary,
|
||||
enabled: Boolean(month),
|
||||
});
|
||||
}
|
||||
@@ -147,6 +163,7 @@ export {
|
||||
type CreateBankOperation,
|
||||
type CreateExpenseCategory,
|
||||
type ExpenseCategory,
|
||||
sortBankSummary,
|
||||
type UpdateBankCard,
|
||||
type UpdateBankOperation,
|
||||
type UpdateExpenseCategory,
|
||||
|
||||
Reference in New Issue
Block a user