@@ -16,7 +16,7 @@ import "react-datepicker/dist/react-datepicker.css";
1616import "./styles.scss" ;
1717
1818const perspectiveStatisticsQuery = gql `
19- query statisticsPerspective($id: LingvodocID!, $start: Int! , $end: Int! ) {
19+ query statisticsPerspective($id: LingvodocID!, $start: Int, $end: Int) {
2020 perspective(id: $id) {
2121 id
2222 statistic(starting_time: $start, ending_time: $end)
@@ -51,8 +51,10 @@ class ApproveModal extends React.Component {
5151 super ( props ) ;
5252
5353 this . state = {
54- startDate : moment ( ) . subtract ( 5 , "years" ) ,
55- endDate : moment ( ) ,
54+ startDate : null ,
55+ endDate : null ,
56+ minDate : null ,
57+ maxDate : null ,
5658 user_id : null ,
5759 approveMap : [ ] ,
5860 showStatistics : null
@@ -63,6 +65,63 @@ class ApproveModal extends React.Component {
6365 this . onApprove = this . onApprove . bind ( this ) ;
6466 }
6567
68+ componentDidMount ( ) {
69+ // Force dates updating
70+ this . setState ( { endDate : moment ( ) } ) ;
71+ }
72+
73+ componentDidUpdate ( prevProps , prevState ) {
74+ const { loading, error, perspective } = this . props . data ;
75+
76+ if ( loading || error ) {
77+ return ;
78+ }
79+
80+ const { statistic : newStatistics } = perspective || { statistic : [ ] } ;
81+ const { statistic : oldStatistics } = prevProps . data . perspective || { statistic : [ ] } ;
82+
83+ let { minDate, maxDate } = this . state ;
84+
85+ if ( ! minDate || ! maxDate || newStatistics !== oldStatistics ) {
86+
87+ minDate = Math . min ( ...newStatistics . map ( stat => stat . first_created_at ) ) ;
88+ maxDate = Math . max ( ...newStatistics . map ( stat => stat . last_created_at ) ) ;
89+
90+ if ( minDate && maxDate ) {
91+
92+ this . setState ( { minDate, maxDate } ) ;
93+
94+ } else if ( newStatistics !== oldStatistics ) {
95+
96+ this . props . data . refetch ( { id : this . props . perspectiveId , start : null , end : null } ) . then (
97+ ( ) => this . setState ( { startDate : null , endDate : null , showStatistics : true } ) ) ;
98+ return ;
99+
100+ }
101+ }
102+
103+ const { user_id, startDate, endDate } = this . state ;
104+
105+ if ( ! startDate || ! endDate || user_id !== prevState . user_id ) {
106+
107+ newStatistics . forEach ( stat => {
108+ if ( user_id === stat . user_id ) {
109+ if ( stat . first_created_at && stat . last_created_at ) {
110+ this . setState ( { startDate : moment . unix ( stat . first_created_at ) , endDate : moment . unix ( stat . last_created_at ) } ) ;
111+
112+ } else {
113+
114+ this . setState ( {
115+ startDate : minDate ? moment . unix ( minDate ) : moment ( ) ,
116+ endDate : maxDate ? moment . unix ( maxDate ) : moment ( )
117+ } ) ;
118+ }
119+ }
120+ } ) ;
121+ }
122+
123+ }
124+
66125 getStatistics ( ) {
67126 const { perspectiveId } = this . props ;
68127 const { startDate, endDate } = this . state ;
@@ -181,22 +240,24 @@ class ApproveModal extends React.Component {
181240 < div className = "lingvo-statistics-block" >
182241 { this . context ( "From" ) }
183242 < DatePicker
184- selected = { startDate . toDate ( ) }
243+ placeholderText = "Minimal date"
244+ selected = { startDate ?. toDate ( ) }
185245 showTimeSelect
186246 timeFormat = "HH:mm"
187247 timeIntervals = { 15 }
188- onChange = { date => this . setState ( { startDate : moment ( date ) , showStatistics : false } ) }
248+ onChange = { date => this . setState ( { startDate : date ? moment ( date ) : null , showStatistics : false } ) }
189249 dateFormat = "dd.MM.yyyy HH:mm"
190250 />
191251 </ div >
192252 < div className = "lingvo-statistics-block" >
193253 { this . context ( "To" ) }
194254 < DatePicker
195- selected = { endDate . toDate ( ) }
255+ placeholderText = "Maximal date"
256+ selected = { endDate ?. toDate ( ) }
196257 showTimeSelect
197258 timeFormat = "HH:mm"
198259 timeIntervals = { 15 }
199- onChange = { date => this . setState ( { endDate : moment ( date ) , showStatistics : false } ) }
260+ onChange = { date => this . setState ( { endDate : date ? moment ( date ) : null , showStatistics : false } ) }
200261 dateFormat = "dd.MM.yyyy HH:mm"
201262 />
202263 </ div >
@@ -297,8 +358,6 @@ export default compose(
297358 options : props => ( {
298359 variables : {
299360 id : props . perspectiveId ,
300- start : moment ( ) . subtract ( 5 , "years" ) . unix ( ) ,
301- end : moment ( ) . unix ( )
302361 } ,
303362 notifyOnNetworkStatusChange : true
304363 } )
0 commit comments