• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1081664682

22 Nov 2023 02:11PM UTC coverage: 82.798% (-17.2%) from 99.964%
1081664682

Pull #4214

gitlab-ci

tranchitella
fix: Fixed the infinite page redirects when the back button is pressed

Remove the location and navigate from the useLocationParams.setValue callback
dependencies as they change the set function that is presented in other
useEffect dependencies. This happens when the back button is clicked, which
leads to the location changing infinitely.

Changelog: Title
Ticket: MEN-6847
Ticket: MEN-6796

Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
Pull Request #4214: fix: Fixed the infinite page redirects when the back button is pressed

4319 of 6292 branches covered (0.0%)

8332 of 10063 relevant lines covered (82.8%)

191.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/js/actions/monitorActions.js
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import Api, { apiUrl, headerNames } from '../api/general-api';
15
import { TIMEOUTS } from '../constants/appConstants';
16
import * as DeviceConstants from '../constants/deviceConstants';
17
import * as MonitorConstants from '../constants/monitorConstants';
18
import { getDeviceFilters } from '../selectors';
19
import { commonErrorFallback, commonErrorHandler, setSnackbar } from './appActions';
20
import { convertDeviceListStateToFilters, getSearchEndpoint } from './deviceActions';
21

22
export const monitorApiUrlv1 = `${apiUrl.v1}/devicemonitor`;
183✔
23

24
const { page: defaultPage, perPage: defaultPerPage } = DeviceConstants.DEVICE_LIST_DEFAULTS;
183✔
25

26
const cutoffLength = 75;
183✔
27
const ellipsis = '...';
183✔
28

29
const longTextTrimmer = text => (text.length >= cutoffLength + ellipsis.length ? `${text.substring(0, cutoffLength + ellipsis.length)}${ellipsis}` : text);
183!
30

31
const sanitizeDeviceAlerts = alerts => alerts.map(alert => ({ ...alert, fullName: alert.name, name: longTextTrimmer(alert.name) }));
183✔
32

33
export const setAlertListState = selectionState => (dispatch, getState) =>
183✔
34
  Promise.resolve(
1✔
35
    dispatch({
36
      type: MonitorConstants.SET_ALERT_LIST_STATE,
37
      value: { ...getState().monitor.alerts.alertList, ...selectionState }
38
    })
39
  );
40

41
export const getDeviceAlerts =
42
  (id, config = {}) =>
183✔
43
  dispatch => {
2✔
44
    const { page = defaultPage, perPage = defaultPerPage, issuedBefore, issuedAfter, sortAscending = false } = config;
2✔
45
    const issued_after = issuedAfter ? `&issued_after=${issuedAfter}` : '';
2!
46
    const issued_before = issuedBefore ? `&issued_before=${issuedBefore}` : '';
2!
47
    return Api.get(`${monitorApiUrlv1}/devices/${id}/alerts?page=${page}&per_page=${perPage}${issued_after}${issued_before}&sort_ascending=${sortAscending}`)
2✔
48
      .catch(err => commonErrorHandler(err, `Retrieving device alerts for device ${id} failed:`, dispatch))
×
49
      .then(res =>
50
        Promise.all([
1✔
51
          dispatch({
52
            type: MonitorConstants.RECEIVE_DEVICE_ALERTS,
53
            deviceId: id,
54
            alerts: sanitizeDeviceAlerts(res.data)
55
          }),
56
          dispatch(setAlertListState({ total: Number(res.headers[headerNames.total]) }))
57
        ])
58
      );
59
  };
60

61
export const getLatestDeviceAlerts =
62
  (id, config = {}) =>
183✔
63
  dispatch => {
2✔
64
    const { page = defaultPage, perPage = 10 } = config;
2✔
65
    return Api.get(`${monitorApiUrlv1}/devices/${id}/alerts/latest?page=${page}&per_page=${perPage}`)
2✔
66
      .catch(err => commonErrorHandler(err, `Retrieving device alerts for device ${id} failed:`, dispatch))
×
67
      .then(res =>
68
        Promise.resolve(
1✔
69
          dispatch({
70
            type: MonitorConstants.RECEIVE_LATEST_DEVICE_ALERTS,
71
            deviceId: id,
72
            alerts: sanitizeDeviceAlerts(res.data)
73
          })
74
        )
75
      );
76
  };
77

78
export const getIssueCountsByType =
79
  (type, options = {}) =>
183✔
80
  (dispatch, getState) => {
7✔
81
    const state = getState();
7✔
82
    const { filters = getDeviceFilters(state), group, status, ...remainder } = options;
7✔
83
    const { applicableFilters: nonMonitorFilters, filterTerms } = convertDeviceListStateToFilters({
7✔
84
      ...remainder,
85
      filters,
86
      group,
87
      offlineThreshold: getState().app.offlineThreshold,
88
      selectedIssues: [type],
89
      status
90
    });
91
    return Api.post(getSearchEndpoint(state.app.features.hasReporting), {
7✔
92
      page: 1,
93
      per_page: 1,
94
      filters: filterTerms,
95
      attributes: [{ scope: 'identity', attribute: 'status' }]
96
    })
97
      .catch(err => commonErrorHandler(err, `Retrieving issue counts failed:`, dispatch, commonErrorFallback))
×
98
      .then(res => {
99
        const total = nonMonitorFilters.length ? state.monitor.issueCounts.byType[type].total : Number(res.headers[headerNames.total]);
7!
100
        const filtered = nonMonitorFilters.length ? Number(res.headers[headerNames.total]) : total;
7!
101
        if (total === state.monitor.issueCounts.byType[type].total && filtered === state.monitor.issueCounts.byType[type].filtered) {
7✔
102
          return Promise.resolve();
4✔
103
        }
104
        return Promise.resolve(
3✔
105
          dispatch({
106
            counts: { filtered, total },
107
            issueType: type,
108
            type: MonitorConstants.RECEIVE_DEVICE_ISSUE_COUNTS
109
          })
110
        );
111
      });
112
  };
113

114
export const getDeviceMonitorConfig = id => dispatch =>
183✔
115
  Api.get(`${monitorApiUrlv1}/devices/${id}/config`)
2✔
116
    .catch(err => commonErrorHandler(err, `Retrieving device monitor config for device ${id} failed:`, dispatch))
×
117
    .then(({ data }) => {
118
      let tasks = [
1✔
119
        dispatch({
120
          type: MonitorConstants.RECEIVE_DEVICE_MONITOR_CONFIG,
121
          device: { id, monitors: data }
122
        })
123
      ];
124
      tasks.push(Promise.resolve(data));
1✔
125
      return Promise.all(tasks);
1✔
126
    });
127

128
export const changeNotificationSetting =
129
  (enabled, channel = MonitorConstants.alertChannels.email) =>
183✔
130
  dispatch => {
1✔
131
    return Api.put(`${monitorApiUrlv1}/settings/global/channel/alerts/${channel}/status`, { enabled })
1✔
132
      .catch(err => commonErrorHandler(err, `${enabled ? 'En' : 'Dis'}abling  ${channel} alerts failed:`, dispatch))
×
133
      .then(() =>
134
        Promise.all([
1✔
135
          dispatch({
136
            type: MonitorConstants.CHANGE_ALERT_CHANNEL,
137
            channel,
138
            enabled
139
          }),
140
          dispatch(setSnackbar(`Successfully ${enabled ? 'en' : 'dis'}abled ${channel} alerts`, TIMEOUTS.fiveSeconds))
1!
141
        ])
142
      );
143
  };
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc