From 6f54d7a0dc04dc663c3f93e9c6c2f0779ccce554 Mon Sep 17 00:00:00 2001
From: Ramesh Kumar <ramesh.p@matriotsolutions.com>
Date: Tue, 23 Mar 2021 11:55:12 +0530
Subject: [PATCH] TMSS-641: Normal Suntime function made as async to avoid
 repeated calls on state changes.

---
 .../components/Timeline/CalendarTimeline.js   | 25 +++++++++++--------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/SAS/TMSS/frontend/tmss_webapp/src/components/Timeline/CalendarTimeline.js b/SAS/TMSS/frontend/tmss_webapp/src/components/Timeline/CalendarTimeline.js
index e5b63045d1c..2a46eb36408 100644
--- a/SAS/TMSS/frontend/tmss_webapp/src/components/Timeline/CalendarTimeline.js
+++ b/SAS/TMSS/frontend/tmss_webapp/src/components/Timeline/CalendarTimeline.js
@@ -843,14 +843,14 @@ export class CalendarTimeline extends Component {
      * @param {moment} endTime 
      */
     async changeDateRange(startTime, endTime, refreshData) {
-        if (this.props.showSunTimings && this.state.viewType===UIConstants.timeline.types.NORMAL) {
+        if (this.props.showSunTimings && this.state.viewType===UIConstants.timeline.types.NORMAL && !this.loadingNormalSuntimes) {
             this.setNormalSuntimings(startTime, endTime);
         }
         const result = await this.props.dateRangeCallback(startTime, endTime, refreshData);
         if (!this.props.showSunTimings && this.state.viewType === UIConstants.timeline.types.NORMAL && !this.loadingStationSunTimes) {
             result.items = await this.addStationSunTimes(startTime, endTime, result.group, result.items);
             result.items = _.orderBy(result.items, ['type'], ['desc']);
-        }   else if (this.state.viewType === UIConstants.timeline.types.WEEKVIEW) {
+        }   else if (this.state.viewType === UIConstants.timeline.types.WEEKVIEW && !this.loadingWeekSunTimes) {
             let group = DEFAULT_GROUP.concat(result.group);
             result.items = await this.addWeekSunTimes(startTime, endTime, group, result.items);
         }
@@ -862,14 +862,16 @@ export class CalendarTimeline extends Component {
      * @param {moment} startTime 
      * @param {moment} endTime 
      */
-    setNormalSuntimings(startTime, endTime) {
+    async setNormalSuntimings(startTime, endTime) {
         let sunRiseTimings = [], sunSetTimings = [], sunTimeMap={};
         const noOfDays = endTime.diff(startTime, 'days');
-        for (const number of _.range(noOfDays+3)) {                     // Added 3 to have suntimes of day before start time and day after end time so that for small time duration also, suntimes will be available.
-            let prevStartTime = startTime.clone().add(-1, 'days');
-            const date = prevStartTime.clone().add(number, 'days').hours(12).minutes(0).seconds(0);
-            const formattedDate = date.format("YYYY-MM-DD");
-            UtilService.getSunTimings(formattedDate).then(timings => {
+        if (!this.loadingNormalSuntimes) {
+            this.loadingNormalSuntimes = true;
+            for (const number of _.range(noOfDays+3)) {                     // Added 3 to have suntimes of day before start time and day after end time so that for small time duration also, suntimes will be available.
+                let prevStartTime = startTime.clone().add(-1, 'days');
+                const date = prevStartTime.clone().add(number, 'days').hours(12).minutes(0).seconds(0);
+                const formattedDate = date.format("YYYY-MM-DD");
+                let timings = await UtilService.getSunTimings(formattedDate);
                 if (timings) {
                     const sunriseStartTime = moment.utc(timings.sun_rise.start.split('.')[0]);
                     const sunriseEndTime = moment.utc(timings.sun_rise.end.split('.')[0]);
@@ -886,7 +888,10 @@ export class CalendarTimeline extends Component {
                     sunTimeMap[formattedDate] = {sunrise: sunriseTime, sunset: sunsetTime};
                     this.setState({sunRiseTimings: sunRiseTimings, sunSetTimings: sunSetTimings, sunTimeMap: sunTimeMap});
                 }
-            });
+                if (number === (noOfDays+2)) {
+                    this.loadingNormalSuntimes = false;
+                }
+            }
         }
     }
 
@@ -1267,7 +1272,7 @@ export class CalendarTimeline extends Component {
             let group =  DEFAULT_GROUP.concat(props.group);
             if (!this.props.showSunTimings && this.state.viewType === UIConstants.timeline.types.NORMAL && !this.loadingStationSunTimes) {
                 props.items = await this.addStationSunTimes(this.state.defaultStartTime, this.state.defaultEndTime, props.group, props.items);
-            }   else if(this.props.showSunTimings && this.state.viewType === UIConstants.timeline.types.NORMAL) {
+            }   else if(this.props.showSunTimings && this.state.viewType === UIConstants.timeline.types.NORMAL && !this.loadingNormalSuntimes) {
                 this.setNormalSuntimings(this.state.defaultStartTime, this.state.defaultEndTime);
             }   else if (this.state.viewType === UIConstants.timeline.types.WEEKVIEW && !this.loadingWeekSunTimes) {
                 props.items = await this.addWeekSunTimes(this.state.defaultStartTime, this.state.defaultEndTime, group, props.items);
-- 
GitLab