Skip to content

Commit

Permalink
fix missing the latest number in montly net worth (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
puncsky authored Mar 19, 2021
1 parent c2ca202 commit a60047c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/screens/home-screen/hooks/use-home-charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useQuery } from "react-apollo";
import { homeCharts } from "@/screens/home-screen/data/home-charts";
import { HomeCharts } from "@/screens/home-screen/data/__generated__/HomeCharts";
import { i18n } from "@/translations";
import { selectNetWorthArray } from "@/screens/home-screen/selectors/select-net-worth-array";

function getNetWorth(currency: string, data?: HomeCharts) {
if (!currency) {
Expand Down Expand Up @@ -33,30 +34,13 @@ function getLastSixMonth(currency: string, data?: HomeCharts) {
return { labels, numbers };
}

function getNetWorthDataArray(currency: string, data?: HomeCharts) {
const netWorth = data?.homeCharts?.data.find((n) => n.label === "Net Worth");
const last = netWorth?.data.slice(
netWorth?.data.length - 7,
netWorth?.data.length - 1
);
let labels = last?.map((l) => l.date.slice(5, 7)) || [];
let numbers = last?.map((l) => Number(l.balance[currency] / 1000 || 0)) || [];
if (labels.length === 0) {
labels = [i18n.t("noDataCharts")];
}
if (numbers.length === 0) {
numbers = [0];
}
return { labels, numbers };
}

export const useHomeCharts = (userId: string, currency: string) => {
const { loading, data, error, refetch } = useQuery<HomeCharts>(homeCharts, {
variables: { userId },
});
const netWorth = getNetWorth(currency, data);
const lastSixMonthData = getLastSixMonth(currency, data);
const lastSixWorthData = getNetWorthDataArray(currency, data);
const lastSixWorthData = selectNetWorthArray(currency, data);
return {
loading,
data,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { selectNetWorthArray } from "@/screens/home-screen/selectors/select-net-worth-array";

describe("selectNetWorthArray", () => {
it("", async () => {
const data = {
homeCharts: {
data: [
{
type: "balances",
label: "Net Worth",
data: [
{
date: "2021-02-01",
balance: {
USD: 100.0,
},
budgets: null,
__typename: "ChartItemV2",
},
{
date: "2021-03-01",
balance: {
USD: 200.0,
},
budgets: null,
__typename: "ChartItemV2",
},
{
date: "2021-03-16",
balance: {
USD: 300.0,
},
budgets: null,
__typename: "ChartItemV2",
},
],
__typename: "LabeledChartItem",
},
],
success: true,
__typename: "HomeChartsResponse",
},
};

const resp = selectNetWorthArray("USD", data);
expect(resp).toEqual({ labels: ["02", "03"], numbers: [0.1, 0.3] });
});
});
31 changes: 31 additions & 0 deletions src/screens/home-screen/selectors/select-net-worth-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { HomeCharts } from "@/screens/home-screen/data/__generated__/HomeCharts";
import { i18n } from "@/translations";

function isSameMonth(date1?: string, date2?: string): boolean {
return date1?.slice(5, 7) === date2?.slice(5, 7);
}

export function selectNetWorthArray(currency: string, data?: HomeCharts) {
const netWorth = data?.homeCharts?.data.find((n) => n.label === "Net Worth");
const last = netWorth?.data.slice(
netWorth?.data.length - 7,
netWorth?.data.length
);
if (
last &&
last.length >= 2 &&
isSameMonth(last[last.length - 1].date, last[last.length - 2].date)
) {
last.splice(last.length - 2, 1);
}

let labels = last?.map((l) => l.date.slice(5, 7)) || [];
let numbers = last?.map((l) => Number(l.balance[currency] / 1000 || 0)) || [];
if (labels.length === 0) {
labels = [i18n.t("noDataCharts")];
}
if (numbers.length === 0) {
numbers = [0];
}
return { labels, numbers };
}

0 comments on commit a60047c

Please sign in to comment.