Skip to content

Commit

Permalink
card updates
Browse files Browse the repository at this point in the history
  • Loading branch information
carkom committed Apr 5, 2024
1 parent ea5b6c1 commit 7cb1255
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { createRef, useMemo, useState } from 'react';
import React, { createRef, useEffect, useMemo, useState } from 'react';

import { send, sendCatch } from 'loot-core/platform/client/fetch/index';
import * as monthUtils from 'loot-core/src/shared/months';
import { type CustomReportEntity } from 'loot-core/types/models/reports';

import { useAccounts } from '../../../hooks/useAccounts';
Expand Down Expand Up @@ -72,6 +73,7 @@ export function CustomReportListCards({
const [err, setErr] = useState('');
const [name, setName] = useState('');
const inputRef = createRef<HTMLInputElement>();
const [earliestTransaction, setEarliestTransaction] = useState('');

const payees = usePayees();
const accounts = useAccounts();
Expand All @@ -85,6 +87,14 @@ export function CustomReportListCards({
onDeleteMenuOpen(reportData === undefined ? '' : reportData, false);
};

useEffect(() => {
async function run() {
const trans = await send('get-earliest-transaction');
setEarliestTransaction(trans ? trans.date : monthUtils.currentDay());
}
run();
}, []);

const onAddUpdate = async ({
reportData,
}: {
Expand Down Expand Up @@ -216,6 +226,7 @@ export function CustomReportListCards({
payees={payees}
accounts={accounts}
categories={categories}
earliestTransaction={earliestTransaction}
/>
</View>
</ReportCard>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { styles } from '../../../style/styles';
import { theme } from '../../../style/theme';
import { Text } from '../../common/Text';
import { ChooseGraph } from '../ChooseGraph';
import { getLiveRange } from '../getLiveRange';
import { LoadingIndicator } from '../LoadingIndicator';
import { ReportOptions } from '../ReportOptions';
import { createCustomSpreadsheet } from '../spreadsheets/custom-spreadsheet';
Expand All @@ -35,16 +36,30 @@ export function GetCardData({
payees,
accounts,
categories,
earliestTransaction,
}: {
report: CustomReportEntity;
payees: PayeeEntity[];
accounts: AccountEntity[];
categories: { list: CategoryEntity[]; grouped: CategoryGroupEntity[] };
earliestTransaction: string;
}) {
let startDate = report.startDate;
let endDate = report.endDate;

if (!report.isDateStatic) {
const [dateStart, dateEnd] = getLiveRange(
report.dateRange,
earliestTransaction,
);
startDate = dateStart || report.startDate;
endDate = dateEnd || report.startDate;
}

const getGroupData = useMemo(() => {
return createGroupedSpreadsheet({
startDate: report.startDate,
endDate: report.endDate,
startDate,
endDate,
interval: report.interval,
categories,
selectedCategories: report.selectedCategories ?? categories.list,
Expand All @@ -56,11 +71,11 @@ export function GetCardData({
showUncategorized: report.showUncategorized,
balanceTypeOp: ReportOptions.balanceTypeMap.get(report.balanceType),
});
}, [report, categories]);
}, [report, categories, startDate, endDate]);
const getGraphData = useMemo(() => {
return createCustomSpreadsheet({
startDate: report.startDate,
endDate: report.endDate,
startDate,
endDate,
interval: report.interval,
categories,
selectedCategories: report.selectedCategories ?? categories.list,
Expand All @@ -76,7 +91,7 @@ export function GetCardData({
accounts,
graphType: report.graphType,
});
}, [report, categories, payees, accounts]);
}, [report, categories, payees, accounts, startDate, endDate]);
const graphData = useReport('default' + report.name, getGraphData);
const groupedData = useReport('grouped' + report.name, getGroupData);

Expand All @@ -86,8 +101,8 @@ export function GetCardData({
return data?.data ? (
<ErrorBoundary FallbackComponent={ErrorFallback}>
<ChooseGraph
startDate={report.startDate}
endDate={report.endDate}
startDate={startDate}
endDate={endDate}
data={data}
mode={report.mode}
graphType={report.graphType}
Expand Down

0 comments on commit 7cb1255

Please sign in to comment.