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

mendersoftware / gui / 919001084

pending completion
919001084

Pull #3839

gitlab-ci

mzedel
revert: "chore: bump node from 20.2.0-alpine to 20.3.1-alpine"

This reverts commit cbfcd7663.

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3839: Combined PRs

4399 of 6397 branches covered (68.77%)

8302 of 10074 relevant lines covered (82.41%)

162.96 hits per line

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

55.38
/src/js/components/devices/device-groups.js
1
// Copyright 2018 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 React, { useEffect, useRef, useState } from 'react';
15
import { useDispatch, useSelector } from 'react-redux';
16
import { useParams } from 'react-router-dom';
17

18
import { AddCircle as AddIcon } from '@mui/icons-material';
19
import { Dialog, DialogContent, DialogTitle } from '@mui/material';
20

21
import pluralize from 'pluralize';
22

23
import { setOfflineThreshold, setSnackbar } from '../../actions/appActions';
24
import {
25
  addDynamicGroup,
26
  addStaticGroup,
27
  preauthDevice,
28
  removeDevicesFromGroup,
29
  removeDynamicGroup,
30
  removeStaticGroup,
31
  selectGroup,
32
  setDeviceFilters,
33
  setDeviceListState,
34
  updateDynamicGroup
35
} from '../../actions/deviceActions';
36
import { setShowConnectingDialog } from '../../actions/userActions';
37
import { SORTING_OPTIONS } from '../../constants/appConstants';
38
import { DEVICE_FILTERING_OPTIONS, DEVICE_ISSUE_OPTIONS, DEVICE_STATES, emptyFilter } from '../../constants/deviceConstants';
39
import { toggle } from '../../helpers';
40
import {
41
  getAcceptedDevices,
42
  getDeviceCountsByStatus,
43
  getDeviceFilters,
44
  getDeviceLimit,
45
  getDocsVersion,
46
  getFeatures,
47
  getGroups as getGroupsSelector,
48
  getIsEnterprise,
49
  getIsPreview,
50
  getLimitMaxed,
51
  getSelectedGroupInfo,
52
  getShowHelptips,
53
  getTenantCapabilities,
54
  getUserCapabilities
55
} from '../../selectors';
56
import { useLocationParams } from '../../utils/liststatehook';
57
import Global from '../settings/global';
58
import AuthorizedDevices from './authorized-devices';
59
import DeviceStatusNotification from './devicestatusnotification';
60
import MakeGatewayDialog from './dialogs/make-gateway-dialog';
61
import PreauthDialog, { DeviceLimitWarning } from './dialogs/preauth-dialog';
62
import CreateGroup from './group-management/create-group';
63
import CreateGroupExplainer from './group-management/create-group-explainer';
64
import RemoveGroup from './group-management/remove-group';
65
import Groups from './groups';
66
import DeviceAdditionWidget from './widgets/deviceadditionwidget';
67

68
export const DeviceGroups = () => {
4✔
69
  const [createGroupExplanation, setCreateGroupExplanation] = useState(false);
1✔
70
  const [fromFilters, setFromFilters] = useState(false);
1✔
71
  const [modifyGroupDialog, setModifyGroupDialog] = useState(false);
1✔
72
  const [openIdDialog, setOpenIdDialog] = useState(false);
1✔
73
  const [openPreauth, setOpenPreauth] = useState(false);
1✔
74
  const [showMakeGateway, setShowMakeGateway] = useState(false);
1✔
75
  const [removeGroup, setRemoveGroup] = useState(false);
1✔
76
  const [tmpDevices, setTmpDevices] = useState([]);
1✔
77
  const deviceTimer = useRef();
1✔
78
  const { status: statusParam } = useParams();
1✔
79

80
  const { groupCount, selectedGroup, groupFilters = [] } = useSelector(getSelectedGroupInfo);
1!
81
  const filteringAttributes = useSelector(state => ({
1✔
82
    ...state.devices.filteringAttributes,
83
    identityAttributes: [...state.devices.filteringAttributes.identityAttributes, 'id']
84
  }));
85
  const { canManageDevices } = useSelector(getUserCapabilities);
1✔
86
  const tenantCapabilities = useSelector(getTenantCapabilities);
1✔
87
  const { groupNames, ...groupsByType } = useSelector(getGroupsSelector);
1✔
88
  const groups = groupNames;
1✔
89
  const { total: acceptedCount = 0 } = useSelector(getAcceptedDevices);
1!
90
  const authRequestCount = useSelector(state => state.monitor.issueCounts.byType[DEVICE_ISSUE_OPTIONS.authRequests.key].total);
1✔
91
  const canPreview = useSelector(getIsPreview);
1✔
92
  const deviceLimit = useSelector(getDeviceLimit);
1✔
93
  const deviceListState = useSelector(state => state.devices.deviceList);
1✔
94
  const docsVersion = useSelector(getDocsVersion);
1✔
95
  const features = useSelector(getFeatures);
1✔
96
  const { hasReporting } = features;
1✔
97
  const filters = useSelector(getDeviceFilters);
1✔
98
  const limitMaxed = useSelector(getLimitMaxed);
1✔
99
  const { pending: pendingCount } = useSelector(getDeviceCountsByStatus);
1✔
100
  const showDeviceConnectionDialog = useSelector(state => state.users.showConnectDeviceDialog);
1✔
101
  const showHelptips = useSelector(getShowHelptips);
1✔
102
  const isEnterprise = useSelector(getIsEnterprise);
1✔
103
  const dispatch = useDispatch();
1✔
104

105
  const [locationParams, setLocationParams] = useLocationParams('devices', {
1✔
106
    filteringAttributes,
107
    filters,
108
    defaults: { sort: { direction: SORTING_OPTIONS.desc } }
109
  });
110

111
  const { refreshTrigger, selectedId, state: selectedState } = deviceListState;
1✔
112

113
  useEffect(() => {
1✔
114
    if (!deviceTimer.current) {
1!
115
      return;
1✔
116
    }
117
    setLocationParams({ pageState: deviceListState, filters, selectedGroup });
×
118
  }, [
119
    deviceListState.detailsTab,
120
    deviceListState.page,
121
    deviceListState.perPage,
122
    deviceListState.selectedIssues,
123
    JSON.stringify(deviceListState.sort),
124
    selectedId,
125
    filters,
126
    selectedGroup,
127
    selectedState
128
  ]);
129

130
  useEffect(() => {
1✔
131
    if (locationParams.groupName) {
1!
132
      dispatch(selectGroup(locationParams.groupName));
×
133
    }
134
    let listState = { setOnly: true };
1✔
135
    if (locationParams.open && locationParams.id.length) {
1!
136
      listState = { ...listState, selectedId: locationParams.id[0], detailsTab: locationParams.detailsTab };
×
137
    }
138
    if (!locationParams.id?.length && selectedId) {
1!
139
      listState = { ...listState, detailsTab: 'identity' };
×
140
    }
141
    dispatch(setDeviceListState(listState));
1✔
142
  }, [locationParams.detailsTab, locationParams.groupName, JSON.stringify(locationParams.id), locationParams.open]);
143

144
  useEffect(() => {
1✔
145
    const { groupName, filters = [], id = [], ...remainder } = locationParams;
1!
146
    const { hasFullFiltering } = tenantCapabilities;
1✔
147
    if (groupName) {
1!
148
      dispatch(selectGroup(groupName, filters));
×
149
    } else if (filters.length) {
1!
150
      dispatch(setDeviceFilters(filters));
×
151
    }
152
    const state = statusParam && Object.values(DEVICE_STATES).some(state => state === statusParam) ? statusParam : selectedState;
1!
153
    let listState = { ...remainder, state, refreshTrigger: !refreshTrigger };
1✔
154
    if (id.length === 1 && Boolean(locationParams.open)) {
1!
155
      listState.selectedId = id[0];
×
156
    } else if (id.length && hasFullFiltering) {
1!
157
      dispatch(setDeviceFilters([...filters, { ...emptyFilter, key: 'id', operator: DEVICE_FILTERING_OPTIONS.$in.key, value: id }]));
×
158
    }
159
    dispatch(setDeviceListState(listState));
1✔
160
    dispatch(setOfflineThreshold());
1✔
161
  }, []);
162

163
  /*
164
   * Groups
165
   */
166
  const removeCurrentGroup = () => {
1✔
167
    const request = groupFilters.length ? dispatch(removeDynamicGroup(selectedGroup)) : dispatch(removeStaticGroup(selectedGroup));
×
168
    return request.then(toggleGroupRemoval).catch(console.log);
×
169
  };
170

171
  // Edit groups from device selection
172
  const addDevicesToGroup = tmpDevices => {
1✔
173
    // (save selected devices in state, open dialog)
174
    setTmpDevices(tmpDevices);
×
175
    setModifyGroupDialog(toggle);
×
176
  };
177

178
  const createGroupFromDialog = (devices, group) => {
1✔
179
    let request = fromFilters ? dispatch(addDynamicGroup(group, filters)) : dispatch(addStaticGroup(group, devices));
×
180
    return request.then(() => {
×
181
      // reached end of list
182
      setCreateGroupExplanation(false);
×
183
      setModifyGroupDialog(false);
×
184
      setFromFilters(false);
×
185
    });
186
  };
187

188
  const onGroupClick = () => {
1✔
189
    if (selectedGroup && groupFilters.length) {
×
190
      return dispatch(updateDynamicGroup(selectedGroup, filters));
×
191
    }
192
    setModifyGroupDialog(true);
×
193
    setFromFilters(true);
×
194
  };
195

196
  const onRemoveDevicesFromGroup = devices => {
1✔
197
    const isGroupRemoval = devices.length >= groupCount;
×
198
    let request;
199
    if (isGroupRemoval) {
×
200
      request = dispatch(removeStaticGroup(selectedGroup));
×
201
    } else {
202
      request = dispatch(removeDevicesFromGroup(selectedGroup, devices));
×
203
    }
204
    return request.catch(console.log);
×
205
  };
206

207
  const openSettingsDialog = e => {
1✔
208
    e.preventDefault();
×
209
    setOpenIdDialog(toggle);
×
210
  };
211

212
  const onCreateGroupClose = () => {
1✔
213
    setModifyGroupDialog(false);
×
214
    setFromFilters(false);
×
215
    setTmpDevices([]);
×
216
  };
217

218
  const onPreauthSaved = addMore => {
1✔
219
    setOpenPreauth(!addMore);
×
220
    dispatch(setDeviceListState({ page: 1, refreshTrigger: !refreshTrigger }));
×
221
  };
222

223
  const onShowDeviceStateClick = state => {
1✔
224
    dispatch(selectGroup());
×
225
    dispatch(setDeviceListState({ state }));
×
226
  };
227

228
  const onGroupSelect = groupName => {
1✔
229
    dispatch(selectGroup(groupName));
×
230
    dispatch(setDeviceListState({ page: 1, refreshTrigger: !refreshTrigger, selection: [] }));
×
231
  };
232

233
  const onShowAuthRequestDevicesClick = () => {
1✔
234
    dispatch(setDeviceFilters([]));
×
235
    dispatch(setDeviceListState({ selectedIssues: [DEVICE_ISSUE_OPTIONS.authRequests.key], page: 1 }));
×
236
  };
237

238
  const toggleGroupRemoval = () => setRemoveGroup(toggle);
1✔
239

240
  const toggleMakeGatewayClick = () => setShowMakeGateway(toggle);
1✔
241

242
  return (
1✔
243
    <>
244
      <div className="tab-container with-sub-panels margin-bottom-small" style={{ padding: 0, minHeight: 'initial' }}>
245
        <h3 style={{ marginBottom: 0 }}>Devices</h3>
246
        <div className="flexbox space-between margin-left-large margin-right center-aligned padding-bottom padding-top-small">
247
          {hasReporting && !!authRequestCount && (
1!
248
            <a className="flexbox center-aligned margin-right-large" onClick={onShowAuthRequestDevicesClick}>
249
              <AddIcon fontSize="small" style={{ marginRight: 6 }} />
250
              {authRequestCount} new device authentication {pluralize('request', authRequestCount)}
251
            </a>
252
          )}
253
          {!!pendingCount && !selectedGroup && selectedState !== DEVICE_STATES.pending ? (
3!
254
            <DeviceStatusNotification deviceCount={pendingCount} state={DEVICE_STATES.pending} onClick={onShowDeviceStateClick} />
255
          ) : (
256
            <div />
257
          )}
258
          {canManageDevices && (
2✔
259
            <DeviceAdditionWidget
260
              docsVersion={docsVersion}
261
              features={features}
262
              onConnectClick={() => dispatch(setShowConnectingDialog(true))}
×
263
              onMakeGatewayClick={toggleMakeGatewayClick}
264
              onPreauthClick={setOpenPreauth}
265
              tenantCapabilities={tenantCapabilities}
266
            />
267
          )}
268
        </div>
269
      </div>
270
      <div className="tab-container with-sub-panels" style={{ padding: 0, height: '100%' }}>
271
        <Groups
272
          className="leftFixed"
273
          acceptedCount={acceptedCount}
274
          changeGroup={onGroupSelect}
275
          groups={groupsByType}
276
          openGroupDialog={setCreateGroupExplanation}
277
          selectedGroup={selectedGroup}
278
          showHelptips={showHelptips}
279
        />
280
        <div className="rightFluid relative" style={{ paddingTop: 0 }}>
281
          {limitMaxed && <DeviceLimitWarning acceptedDevices={acceptedCount} deviceLimit={deviceLimit} />}
1!
282
          <AuthorizedDevices
283
            addDevicesToGroup={addDevicesToGroup}
284
            onGroupClick={onGroupClick}
285
            onGroupRemoval={toggleGroupRemoval}
286
            onMakeGatewayClick={toggleMakeGatewayClick}
287
            onPreauthClick={setOpenPreauth}
288
            openSettingsDialog={openSettingsDialog}
289
            removeDevicesFromGroup={onRemoveDevicesFromGroup}
290
            showsDialog={showDeviceConnectionDialog || removeGroup || modifyGroupDialog || createGroupExplanation || openIdDialog || openPreauth}
6✔
291
          />
292
        </div>
293
        {removeGroup && <RemoveGroup onClose={toggleGroupRemoval} onRemove={removeCurrentGroup} />}
1!
294
        {modifyGroupDialog && (
1!
295
          <CreateGroup
296
            addListOfDevices={createGroupFromDialog}
297
            fromFilters={fromFilters}
298
            isCreation={fromFilters || !groups.length}
×
299
            selectedDevices={tmpDevices}
300
            onClose={onCreateGroupClose}
301
          />
302
        )}
303
        {createGroupExplanation && <CreateGroupExplainer isEnterprise={isEnterprise} onClose={() => setCreateGroupExplanation(false)} />}
×
304
        {openIdDialog && (
1!
305
          <Dialog open>
306
            <DialogTitle>Default device identity attribute</DialogTitle>
307
            <DialogContent style={{ overflow: 'hidden' }}>
308
              <Global dialog closeDialog={openSettingsDialog} />
309
            </DialogContent>
310
          </Dialog>
311
        )}
312
        {openPreauth && (
1!
313
          <PreauthDialog
314
            acceptedDevices={acceptedCount}
315
            deviceLimit={deviceLimit}
316
            limitMaxed={limitMaxed}
317
            preauthDevice={authset => dispatch(preauthDevice(authset))}
×
318
            onSubmit={onPreauthSaved}
319
            onCancel={() => setOpenPreauth(false)}
×
320
            setSnackbar={message => dispatch(setSnackbar(message))}
×
321
          />
322
        )}
323
        {showMakeGateway && <MakeGatewayDialog docsVersion={docsVersion} isPreRelease={canPreview} onCancel={toggleMakeGatewayClick} />}
1!
324
      </div>
325
    </>
326
  );
327
};
328

329
export default DeviceGroups;
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