1- import { useState , useEffect } from 'react' ;
1+ import { useState , useEffect , useRef } from 'react' ;
22import DatePicker from 'react-datepicker' ;
33import 'react-datepicker/dist/react-datepicker.css' ;
44import styles from './ApplicationChart.module.css' ;
@@ -8,21 +8,31 @@ function TimeFilter({ onFilterChange, darkMode }) {
88 const [ startDate , setStartDate ] = useState ( null ) ;
99 const [ endDate , setEndDate ] = useState ( null ) ;
1010 const [ error , setError ] = useState ( '' ) ;
11+ const prevOptionRef = useRef ( selectedOption ) ; // To track previous selected filter option
1112
1213 useEffect ( ( ) => {
14+ if ( selectedOption === 'custom' && prevOptionRef . current !== 'custom' ) {
15+ // Reset custom dates when switching to custom
16+ setStartDate ( null ) ;
17+ setEndDate ( null ) ;
18+ }
19+
1320 if ( selectedOption === 'custom' && startDate && endDate ) {
1421 if ( startDate > endDate ) {
1522 setError ( 'Start date cannot be after end date.' ) ;
16- return ;
1723 } else {
1824 setError ( '' ) ;
1925 }
2026 } else {
2127 setError ( '' ) ;
2228 }
2329
30+ // Updated parent with current filter state
2431 onFilterChange ( { selectedOption, startDate, endDate, error : '' } ) ;
25- } , [ selectedOption , startDate , endDate ] ) ; // Removed onFilterChange from dependencies
32+
33+ prevOptionRef . current = selectedOption ; // Updatse previous option
34+ } , [ selectedOption , startDate , endDate ] ) ;
35+ // Removed onFilterChange from dependencies
2636
2737 return (
2838 < div className = { `${ styles . TimeFilter } ` } >
0 commit comments