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

mendersoftware / gui / 901187442

pending completion
901187442

Pull #3795

gitlab-ci

mzedel
feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

Ticket: None
Changelog: None
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3795: feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

4389 of 6365 branches covered (68.96%)

5 of 5 new or added lines in 1 file covered. (100.0%)

1729 existing lines in 165 files now uncovered.

8274 of 10019 relevant lines covered (82.58%)

144.86 hits per line

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

78.99
/src/js/components/deployments/deployments.js
1
// Copyright 2015 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 { Link, useNavigate } from 'react-router-dom';
17

18
import { Button, Tab, Tabs } from '@mui/material';
19

20
import { setSnackbar } from '../../actions/appActions';
21
import { abortDeployment, setDeploymentsState } from '../../actions/deploymentActions';
22
import { getDynamicGroups, getGroups } from '../../actions/deviceActions';
23
import { advanceOnboarding } from '../../actions/onboardingActions';
24
import { DEPLOYMENT_ROUTES, DEPLOYMENT_STATES, listDefaultsByState } from '../../constants/deploymentConstants';
25
import { ALL_DEVICES, UNGROUPED_GROUP } from '../../constants/deviceConstants';
26
import { onboardingSteps } from '../../constants/onboardingConstants';
27
import { getISOStringBoundaries } from '../../helpers';
28
import { getDevicesById, getIsEnterprise, getOnboardingState, getReleasesById, getUserCapabilities } from '../../selectors';
29
import { useLocationParams } from '../../utils/liststatehook';
30
import { getOnboardingComponentFor } from '../../utils/onboardingmanager';
31
import useWindowSize from '../../utils/resizehook';
32
import CreateDeployment from './createdeployment';
33
import Progress from './inprogressdeployments';
34
import Past from './pastdeployments';
35
import Report from './report';
36
import Scheduled from './scheduleddeployments';
37

38
const routes = {
7✔
39
  [DEPLOYMENT_ROUTES.active.key]: {
40
    ...DEPLOYMENT_ROUTES.active,
41
    component: Progress
42
  },
43
  [DEPLOYMENT_ROUTES.scheduled.key]: {
44
    ...DEPLOYMENT_ROUTES.scheduled,
45
    component: Scheduled
46
  },
47
  [DEPLOYMENT_ROUTES.finished.key]: {
48
    ...DEPLOYMENT_ROUTES.finished,
49
    component: Past
50
  }
51
};
52

53
export const defaultRefreshDeploymentsLength = 30000;
7✔
54

55
export const Deployments = () => {
7✔
56
  const groupsById = useSelector(state => {
282✔
57
    // eslint-disable-next-line no-unused-vars
58
    const { [UNGROUPED_GROUP.id]: ungrouped, ...groups } = state.devices.groups.byId;
500✔
59
    return groups;
500✔
60
  });
61
  const devicesById = useSelector(getDevicesById);
281✔
62
  const isEnterprise = useSelector(getIsEnterprise);
281✔
63
  const onboardingState = useSelector(getOnboardingState);
281✔
64
  const pastCount = useSelector(state => state.deployments.byStatus.finished.total);
500✔
65
  const releases = useSelector(getReleasesById);
281✔
66
  const selectionState = useSelector(state => state.deployments.selectionState);
500✔
67
  const userCapabilities = useSelector(getUserCapabilities);
281✔
68
  const dispatch = useDispatch();
281✔
69

70
  const [deploymentObject, setDeploymentObject] = useState({});
281✔
71
  // eslint-disable-next-line no-unused-vars
72
  const size = useWindowSize();
281✔
73
  const tabsRef = useRef();
281✔
74
  const isInitialized = useRef(false);
281✔
75
  const navigate = useNavigate();
281✔
76
  const { reportType, showCreationDialog: createDialog, showReportDialog: reportDialog, state } = selectionState.general;
281✔
77
  const { canDeploy, canReadReleases } = userCapabilities;
281✔
78

79
  const [date] = useState(getISOStringBoundaries(new Date()));
281✔
80
  const { start: today, end: tonight } = date;
281✔
81

82
  const [locationParams, setLocationParams] = useLocationParams('deployments', { today, tonight, defaults: listDefaultsByState });
281✔
83

84
  useEffect(() => {
281✔
85
    if (!isInitialized.current) {
22✔
86
      return;
4✔
87
    }
88
    setLocationParams({ deploymentObject, pageState: selectionState });
18✔
89
  }, [
90
    selectionState.selectedId,
91
    selectionState.general.state,
92
    selectionState.general.showCreationDialog,
93
    selectionState.general.showReportDialog,
94
    selectionState.general.reportType,
95
    selectionState[DEPLOYMENT_STATES.finished].endDate,
96
    selectionState[DEPLOYMENT_STATES.finished].search,
97
    selectionState[DEPLOYMENT_STATES.finished].startDate,
98
    selectionState[DEPLOYMENT_STATES.finished].page,
99
    selectionState[DEPLOYMENT_STATES.finished].perPage,
100
    selectionState[DEPLOYMENT_STATES.finished].type,
101
    selectionState[DEPLOYMENT_STATES.inprogress].page,
102
    selectionState[DEPLOYMENT_STATES.inprogress].perPage,
103
    selectionState[DEPLOYMENT_STATES.pending].page,
104
    selectionState[DEPLOYMENT_STATES.pending].perPage
105
  ]);
106

107
  useEffect(() => {
281✔
108
    dispatch(getGroups());
4✔
109
    if (isEnterprise) {
4✔
110
      dispatch(getDynamicGroups());
3✔
111
    }
112
    const { deploymentObject = {}, id: selectedId = [], ...remainder } = locationParams;
4!
113
    const { devices: selectedDevices = [], release: releaseName } = deploymentObject;
4✔
114
    const release = releaseName ? { ...(releases[releaseName] ?? { Name: releaseName }) } : undefined;
4!
115
    const devices = selectedDevices.length ? selectedDevices.map(device => ({ ...device, ...devicesById[device.id] })) : [];
4!
116
    setDeploymentObject({ devices, release });
4✔
117
    dispatch(setDeploymentsState({ selectedId: selectedId[0], ...remainder }));
4✔
118
    isInitialized.current = true;
4✔
119
  }, []);
120

121
  const retryDeployment = (deployment, deploymentDeviceIds) => {
281✔
UNCOV
122
    const { artifact_name, name, update_control_map = {} } = deployment;
×
UNCOV
123
    const release = releases[artifact_name];
×
UNCOV
124
    const enterpriseSettings = isEnterprise
×
125
      ? {
126
          phases: [{ batch_size: 100, start_ts: undefined, delay: 0 }],
127
          update_control_map: { states: update_control_map.states || {} }
×
128
        }
129
      : {};
UNCOV
130
    const targetDevicesConfig = name === ALL_DEVICES || groupsById[name] ? { group: name } : { devices: [devicesById[name]] };
×
UNCOV
131
    const deploymentObject = {
×
132
      deploymentDeviceIds,
133
      release,
134
      deploymentDeviceCount: deploymentDeviceIds.length,
135
      ...targetDevicesConfig,
136
      ...enterpriseSettings
137
    };
UNCOV
138
    setDeploymentObject(deploymentObject);
×
UNCOV
139
    dispatch(setDeploymentsState({ general: { showCreationDialog: true, showReportDialog: false } }));
×
140
  };
141

142
  const onScheduleSubmit = () => {
281✔
143
    dispatch(setDeploymentsState({ general: { showCreationDialog: false, showReportDialog: false } }));
2✔
144
    setDeploymentObject({});
2✔
145
    // successfully retrieved new deployment
146
    if (routes.active.key !== state) {
2✔
147
      navigate(routes.active.route);
1✔
148
      changeTab(undefined, routes.active.key);
1✔
149
    }
150
  };
151

152
  const onAbortDeployment = id =>
281✔
UNCOV
153
    dispatch(abortDeployment(id)).then(() => {
×
UNCOV
154
      dispatch(setDeploymentsState({ general: { showCreationDialog: false, showReportDialog: false } }));
×
UNCOV
155
      return Promise.resolve();
×
156
    });
157

158
  const changeTab = (_, tabIndex) => {
281✔
159
    dispatch(setDeploymentsState({ general: { state: tabIndex } }));
5✔
160
    dispatch(setSnackbar(''));
5✔
161
    if (pastCount && !onboardingState.complete) {
5✔
162
      dispatch(advanceOnboarding(onboardingSteps.DEPLOYMENTS_PAST));
1✔
163
    }
164
  };
165

166
  const showReport = (reportType, selectedId) => {
281✔
167
    if (!onboardingState.complete) {
1!
168
      dispatch(advanceOnboarding(onboardingSteps.DEPLOYMENTS_INPROGRESS));
1✔
169
    }
170
    dispatch(setDeploymentsState({ general: { reportType, showCreationDialog: false, showReportDialog: true }, selectedId }));
1✔
171
  };
172

173
  const closeReport = () => dispatch(setDeploymentsState({ general: { reportType: undefined, showReportDialog: false }, selectedId: undefined }));
281✔
174

175
  const onCreationDismiss = () => {
281✔
176
    dispatch(setDeploymentsState({ general: { showCreationDialog: false } }));
1✔
177
    setDeploymentObject({});
1✔
178
  };
179

180
  const onCreationShow = () => dispatch(setDeploymentsState({ general: { showCreationDialog: true } }));
281✔
181

182
  let onboardingComponent = null;
281✔
183
  // the pastCount prop is needed to trigger the rerender as the change in past deployments would otherwise not be noticed on this view
184
  if (pastCount && tabsRef.current && !reportDialog) {
281✔
185
    const tabs = tabsRef.current.getElementsByClassName('MuiTab-root');
221✔
186
    const finishedTab = tabs[tabs.length - 1];
221✔
187
    onboardingComponent = getOnboardingComponentFor(onboardingSteps.DEPLOYMENTS_PAST, onboardingState, {
221✔
188
      anchor: {
189
        left: tabsRef.current.offsetLeft + tabsRef.current.offsetWidth - finishedTab.offsetWidth / 2,
190
        top: tabsRef.current.parentElement.offsetTop + finishedTab.offsetHeight
191
      }
192
    });
193
  }
194

195
  const ComponentToShow = routes[state].component;
281✔
196
  return (
281✔
197
    <>
198
      <div className="margin-left-small margin-top" style={{ maxWidth: '80vw' }}>
199
        <div className="flexbox space-between">
200
          <Tabs value={state} onChange={changeTab} ref={tabsRef}>
201
            {Object.values(routes).map(route => (
202
              <Tab component={Link} key={route.route} label={route.title} to={route.route} value={route.key} />
843✔
203
            ))}
204
          </Tabs>
205
          {canDeploy && canReadReleases && (
843✔
206
            <Button color="secondary" variant="contained" onClick={onCreationShow} style={{ height: '100%' }}>
207
              Create a deployment
208
            </Button>
209
          )}
210
        </div>
211
        <ComponentToShow abort={onAbortDeployment} createClick={onCreationShow} openReport={showReport} isShowingDetails={reportDialog} />
212
      </div>
213
      <Report abort={onAbortDeployment} onClose={closeReport} open={reportDialog} retry={retryDeployment} type={reportType} />
214
      <CreateDeployment
215
        open={createDialog}
216
        onDismiss={onCreationDismiss}
217
        deploymentObject={deploymentObject}
218
        onScheduleSubmit={onScheduleSubmit}
219
        setDeploymentObject={setDeploymentObject}
220
      />
221
      {!reportDialog && onboardingComponent}
560✔
222
    </>
223
  );
224
};
225

226
export default Deployments;
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