11import React , { useEffect , useState } from 'react' ;
22import ReactLoading from 'react-loading' ;
3+ import { FormLabel , FormGroup , FormControl } from 'react-bootstrap' ;
4+
35import {
46 BarChart ,
57 Bar ,
68 XAxis ,
79 YAxis ,
8- LineChart ,
9- Line ,
1010 CartesianGrid ,
1111 Tooltip ,
1212 Legend ,
1313} from 'recharts' ;
14+ import { isDate } from 'moment' ;
1415import API from '../../apis/API' ; // library for AJAX functions
1516import Header , { HeaderPadding } from '../Navigation/Header' ;
1617import TransactionListItem from './TransactionListItem' ;
@@ -19,6 +20,16 @@ import './Transaction.css';
1920function TransactionStats ( ) {
2021 const [ transactions , setTransactions ] = useState ( [ ] ) ;
2122 const [ loading , setLoading ] = useState ( true ) ;
23+ const [ startDate , setStartDate ] = useState ( new Date ( 2020 , 1 ) ) ;
24+ const [ endDate , setEndDate ] = useState ( new Date ( ) ) ;
25+
26+ const handleStartChange = ( event ) => {
27+ setStartDate ( event . target . value ) ;
28+ } ;
29+
30+ const handleEndChange = ( event ) => {
31+ setEndDate ( event . target . value ) ;
32+ } ;
2233
2334 useEffect ( ( ) => {
2435 API . instance . get ( '/transaction' ) . then ( ( res ) => {
@@ -46,12 +57,19 @@ function TransactionStats() {
4657 const renderPlot = ( ) => {
4758 if ( loading ) return < ReactLoading color = "#e26d5c" /> ;
4859 const sales = transactions . filter ( ( t ) => t . endLoc === 'sold' ) ;
49- console . log ( sales ) ;
60+ const salesInRange = sales . filter ( ( s ) => {
61+ return (
62+ new Date ( s . date ) > new Date ( startDate ) &&
63+ new Date ( s . date ) <= new Date ( endDate )
64+ ) ;
65+ } ) ;
66+ console . log ( salesInRange . length ) ;
67+
5068 return (
5169 < BarChart
5270 width = { 600 }
5371 height = { 300 }
54- data = { sales }
72+ data = { salesInRange }
5573 margin = { { top : 5 , right : 30 , left : 20 , bottom : 5 } }
5674 >
5775 < CartesianGrid strokeDasharray = "3 3" />
@@ -64,11 +82,39 @@ function TransactionStats() {
6482 ) ;
6583 } ;
6684
85+ const dateInputs = ( ) => {
86+ if ( loading ) return < ReactLoading color = "#e26d5c" /> ;
87+ return (
88+ < div className = "container" >
89+ < FormGroup bssize = "large" >
90+ < FormLabel > Start Date</ FormLabel >
91+ < FormControl
92+ name = "startDate"
93+ type = "date"
94+ value = { startDate }
95+ onChange = { handleStartChange }
96+ />
97+ </ FormGroup >
98+
99+ < FormGroup bssize = "large" >
100+ < FormLabel > End Date</ FormLabel >
101+ < FormControl
102+ name = "startDate"
103+ type = "date"
104+ value = { endDate }
105+ onChange = { handleEndChange }
106+ />
107+ </ FormGroup >
108+ </ div >
109+ ) ;
110+ } ;
111+
67112 return (
68113 < div className = "container" >
69114 < Header />
70115 < HeaderPadding />
71116 < div className = "SalesPlot" > { renderPlot ( ) } </ div >
117+ < div className = "DateInputs" > { dateInputs ( ) } </ div >
72118 </ div >
73119 ) ;
74120}
0 commit comments