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

mendersoftware / gui / 944676341

pending completion
944676341

Pull #3875

gitlab-ci

mzedel
chore: aligned snapshots with updated design

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3875: MEN-5414

4469 of 6446 branches covered (69.33%)

230 of 266 new or added lines in 43 files covered. (86.47%)

1712 existing lines in 161 files now uncovered.

8406 of 10170 relevant lines covered (82.65%)

196.7 hits per line

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

54.89
/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
  getFeatures,
46
  getGroups as getGroupsSelector,
47
  getIsEnterprise,
48
  getIsPreview,
49
  getLimitMaxed,
50
  getSelectedGroupInfo,
51
  getSortedFilteringAttributes,
52
  getTenantCapabilities,
53
  getUserCapabilities
54
} from '../../selectors';
55
import { useLocationParams } from '../../utils/liststatehook';
56
import Global from '../settings/global';
57
import AuthorizedDevices from './authorized-devices';
58
import DeviceStatusNotification from './devicestatusnotification';
59
import MakeGatewayDialog from './dialogs/make-gateway-dialog';
60
import PreauthDialog, { DeviceLimitWarning } from './dialogs/preauth-dialog';
61
import CreateGroup from './group-management/create-group';
62
import CreateGroupExplainer from './group-management/create-group-explainer';
63
import RemoveGroup from './group-management/remove-group';
64
import Groups from './groups';
65
import DeviceAdditionWidget from './widgets/deviceadditionwidget';
66

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

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

99
  const [locationParams, setLocationParams] = useLocationParams('devices', {
2✔
100
    filteringAttributes,
101
    filters,
102
    defaults: { sort: { direction: SORTING_OPTIONS.desc } }
103
  });
104

105
  const { refreshTrigger, selectedId, state: selectedState } = deviceListState;
2✔
106

107
  useEffect(() => {
2✔
108
    if (!deviceTimer.current) {
2!
109
      return;
2✔
110
    }
UNCOV
111
    setLocationParams({ pageState: deviceListState, filters, selectedGroup });
×
112
  }, [
113
    deviceListState.detailsTab,
114
    deviceListState.page,
115
    deviceListState.perPage,
116
    deviceListState.selectedIssues,
117
    JSON.stringify(deviceListState.sort),
118
    selectedId,
119
    filters,
120
    selectedGroup,
121
    selectedState
122
  ]);
123

124
  useEffect(() => {
2✔
125
    if (locationParams.groupName) {
1!
UNCOV
126
      dispatch(selectGroup(locationParams.groupName));
×
127
    }
128
    let listState = { setOnly: true };
1✔
129
    if (locationParams.open && locationParams.id.length) {
1!
UNCOV
130
      listState = { ...listState, selectedId: locationParams.id[0], detailsTab: locationParams.detailsTab };
×
131
    }
132
    if (!locationParams.id?.length && selectedId) {
1!
UNCOV
133
      listState = { ...listState, detailsTab: 'identity' };
×
134
    }
135
    dispatch(setDeviceListState(listState));
1✔
136
  }, [locationParams.detailsTab, locationParams.groupName, JSON.stringify(locationParams.id), locationParams.open]);
137

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

157
  /*
158
   * Groups
159
   */
160
  const removeCurrentGroup = () => {
2✔
UNCOV
161
    const request = groupFilters.length ? dispatch(removeDynamicGroup(selectedGroup)) : dispatch(removeStaticGroup(selectedGroup));
×
UNCOV
162
    return request.then(toggleGroupRemoval).catch(console.log);
×
163
  };
164

165
  // Edit groups from device selection
166
  const addDevicesToGroup = tmpDevices => {
2✔
167
    // (save selected devices in state, open dialog)
UNCOV
168
    setTmpDevices(tmpDevices);
×
UNCOV
169
    setModifyGroupDialog(toggle);
×
170
  };
171

172
  const createGroupFromDialog = (devices, group) => {
2✔
UNCOV
173
    let request = fromFilters ? dispatch(addDynamicGroup(group, filters)) : dispatch(addStaticGroup(group, devices));
×
UNCOV
174
    return request.then(() => {
×
175
      // reached end of list
UNCOV
176
      setCreateGroupExplanation(false);
×
UNCOV
177
      setModifyGroupDialog(false);
×
UNCOV
178
      setFromFilters(false);
×
179
    });
180
  };
181

182
  const onGroupClick = () => {
2✔
UNCOV
183
    if (selectedGroup && groupFilters.length) {
×
UNCOV
184
      return dispatch(updateDynamicGroup(selectedGroup, filters));
×
185
    }
UNCOV
186
    setModifyGroupDialog(true);
×
UNCOV
187
    setFromFilters(true);
×
188
  };
189

190
  const onRemoveDevicesFromGroup = devices => {
2✔
UNCOV
191
    const isGroupRemoval = devices.length >= groupCount;
×
192
    let request;
UNCOV
193
    if (isGroupRemoval) {
×
UNCOV
194
      request = dispatch(removeStaticGroup(selectedGroup));
×
195
    } else {
UNCOV
196
      request = dispatch(removeDevicesFromGroup(selectedGroup, devices));
×
197
    }
UNCOV
198
    return request.catch(console.log);
×
199
  };
200

201
  const openSettingsDialog = e => {
2✔
UNCOV
202
    e.preventDefault();
×
UNCOV
203
    setOpenIdDialog(toggle);
×
204
  };
205

206
  const onCreateGroupClose = () => {
2✔
UNCOV
207
    setModifyGroupDialog(false);
×
UNCOV
208
    setFromFilters(false);
×
UNCOV
209
    setTmpDevices([]);
×
210
  };
211

212
  const onPreauthSaved = addMore => {
2✔
UNCOV
213
    setOpenPreauth(!addMore);
×
UNCOV
214
    dispatch(setDeviceListState({ page: 1, refreshTrigger: !refreshTrigger }));
×
215
  };
216

217
  const onShowDeviceStateClick = state => {
2✔
UNCOV
218
    dispatch(selectGroup());
×
UNCOV
219
    dispatch(setDeviceListState({ state }));
×
220
  };
221

222
  const onGroupSelect = groupName => {
2✔
UNCOV
223
    dispatch(selectGroup(groupName));
×
UNCOV
224
    dispatch(setDeviceListState({ page: 1, refreshTrigger: !refreshTrigger, selection: [] }));
×
225
  };
226

227
  const onShowAuthRequestDevicesClick = () => {
2✔
UNCOV
228
    dispatch(setDeviceFilters([]));
×
UNCOV
229
    dispatch(setDeviceListState({ selectedIssues: [DEVICE_ISSUE_OPTIONS.authRequests.key], page: 1 }));
×
230
  };
231

232
  const toggleGroupRemoval = () => setRemoveGroup(toggle);
2✔
233

234
  const toggleMakeGatewayClick = () => setShowMakeGateway(toggle);
2✔
235

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

321
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