diff --git a/components/RecentTransactions.tsx b/components/RecentTransactions.tsx index 245c0bd..02a32d7 100644 --- a/components/RecentTransactions.tsx +++ b/components/RecentTransactions.tsx @@ -23,10 +23,21 @@ import { GET_TRANSACTIONS } from "constants/queries"; import Transaction from "components/Transaction"; import Loading from "components/Loading"; -const RecentTransactions: React.FC = () => { +export interface RecentTransactionsProps { + dateStart?: Date; + dateEnd?: Date; +} + +const RecentTransactions: React.FC = (props) => { const intl = useIntl(); const { data, loading, error } = useQuery(GET_TRANSACTIONS, { - variables: { order: { desc: "date" } }, + variables: { + order: { desc: "date" }, + filter: + props.dateStart && props.dateEnd + ? { date: { between: { min: props.dateStart, max: props.dateEnd } } } + : {}, + }, }); if (error) { console.error(error); diff --git a/pages/index.tsx b/pages/index.tsx index e601354..141483c 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -15,8 +15,10 @@ */ import type { NextPage } from "next"; -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import { useIntl } from "react-intl"; +import { DateTime } from "luxon"; +import DatePicker from "react-datepicker"; import { useQuery } from "@apollo/client"; import toast from "react-hot-toast"; import { TRANSACTION_AMOUNT_AGGREGATE } from "constants/queries"; @@ -27,10 +29,20 @@ import RecentTransactions from "components/RecentTransactions"; const Home: NextPage = () => { const intl = useIntl(); + const [dateStart, setDateStart] = useState(DateTime.now().startOf("month")); + const [dateEnd, setDateEnd] = useState(DateTime.now()); + const { data: income, error: incomeError } = useQuery( TRANSACTION_AMOUNT_AGGREGATE, { - variables: { filter: { and: [{ amount: { gt: 0 } }] } }, + variables: { + filter: { + and: [ + { amount: { gt: 0 } }, + { date: { between: { min: dateStart, max: dateEnd } } }, + ], + }, + }, } ); if (incomeError) { @@ -40,7 +52,14 @@ const Home: NextPage = () => { const { data: expense, error: expenseError } = useQuery( TRANSACTION_AMOUNT_AGGREGATE, { - variables: { filter: { and: [{ amount: { lt: 0 } }] } }, + variables: { + filter: { + and: [ + { amount: { lt: 0 } }, + { date: { between: { min: dateStart, max: dateEnd } } }, + ], + }, + }, } ); if (expenseError) { @@ -58,6 +77,25 @@ const Home: NextPage = () => { return (
+
+
+ {/* @ts-ignore */} + setDateStart(DateTime.fromJSDate(date))} + className="rounded-md w-36" + wrapperClassName="mr-7" + /> + + {/* @ts-ignore */} + setDateEnd(DateTime.fromJSDate(date))} + className="rounded-md w-36" + wrapperClassName="" + /> +
+
{ type="expense" />
- +
);