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

mendersoftware / mender-server / 1648176235

30 Jan 2025 09:44AM UTC coverage: 77.589% (+1.0%) from 76.604%
1648176235

Pull #390

gitlab-ci

mzedel
fix(gui): made tenant creation form also work with unset SP device limits

Ticket: None
Changelog: None
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #390: MEN-7961 - SP config fixes

4329 of 6285 branches covered (68.88%)

Branch coverage included in aggregate %.

26 of 34 new or added lines in 6 files covered. (76.47%)

2 existing lines in 1 file now uncovered.

42773 of 54422 relevant lines covered (78.6%)

21.58 hits per line

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

65.96
/frontend/src/js/components/tenants/ExpandedTenant.tsx
1
// Copyright 2024 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, { useState } from 'react';
15
import { useDispatch, useSelector } from 'react-redux';
16
import { Link } from 'react-router-dom';
17

18
import { Button, Checkbox, Divider, Drawer, FormControl, FormControlLabel, FormHelperText, TextField, formControlLabelClasses } from '@mui/material';
19
import { makeStyles } from 'tss-react/mui';
20

21
import { TwoColumns } from '@northern.tech/common-ui/ConfigurationObject';
22
import { ConfirmModal } from '@northern.tech/common-ui/ConfirmModal';
23
import { DrawerTitle } from '@northern.tech/common-ui/DrawerTitle';
24
import actions from '@northern.tech/store/actions';
25
import { getOrganization, getSsoConfig } from '@northern.tech/store/organizationSlice/selectors';
26
import { editTenantDeviceLimit, removeTenant } from '@northern.tech/store/organizationSlice/thunks';
27
import { AppDispatch } from '@northern.tech/store/store';
28
import { generateTenantPathById } from '@northern.tech/utils/locationutils';
29
import copy from 'copy-to-clipboard';
30

31
import { DeviceCount } from '../header/devicecount';
32
import { Tenant } from './types';
33

34
interface ExpandedTenantProps {
35
  onCloseClick: () => void;
36
  tenant: Tenant;
37
}
38

39
const useStyles = makeStyles()(theme => ({
7✔
40
  devLimitInput: { minWidth: 150 },
41
  formWrapper: { display: 'flex', flexDirection: 'column', gap: theme.spacing(2), maxWidth: 750, [`.${formControlLabelClasses.root}`]: { marginTop: 0 } },
42
  ssoLink: {
43
    marginLeft: `calc(1em + ${theme.spacing(1.5)})`, // 1em as the width of the checkbox + the padding around the checkbox
44
    marginTop: theme.spacing(-1)
45
  }
46
}));
47
const { setSnackbar } = actions;
7✔
48

49
export const ExpandedTenant = (props: ExpandedTenantProps) => {
7✔
50
  const { onCloseClick, tenant } = props;
5✔
51
  const { name, id, device_limit, device_count, binary_delta } = tenant;
5✔
52

53
  const [shouldDelete, setShouldDelete] = useState<boolean>(false);
5✔
54
  const [newLimitForm, setNewLimitForm] = useState<boolean>(false);
5✔
55
  const [newLimit, setNewLimit] = useState<number>(device_limit);
5✔
56
  const [hasLimitError, setHasLimitError] = useState<boolean>(false);
5✔
57

58
  const { device_count: spDeviceUtilization, device_limit: spDeviceLimit } = useSelector(getOrganization);
5✔
59
  const ssoConfig = useSelector(getSsoConfig);
5✔
60

61
  const currentLimit = spDeviceLimit - spDeviceUtilization + device_limit;
5✔
62
  const { classes } = useStyles();
5✔
63
  const dispatch = useDispatch<AppDispatch>();
5✔
64

65
  const copyLinkToClipboard = () => {
5✔
66
    const location = window.origin + '/ui';
×
67
    copy(`${location}${generateTenantPathById(id)}`);
×
68
    dispatch(setSnackbar('Link copied to clipboard'));
×
69
  };
70

71
  const onChangeLimit = ({ target: { validity, value } }) => {
5✔
72
    if (validity.valid) {
2!
73
      setNewLimit(value);
2✔
74
      return setHasLimitError(false);
2✔
75
    }
NEW
76
    setHasLimitError(true);
×
77
  };
78

79
  const onNewLimitSubmit = async () => {
5✔
80
    await dispatch(editTenantDeviceLimit({ id, name, newLimit: Number(newLimit) }));
1✔
81
    setNewLimitForm(false);
×
82
  };
83

84
  const deleteTenant = () => dispatch(removeTenant({ id }));
5✔
85

86
  return (
5✔
87
    <Drawer onClose={onCloseClick} open={true} PaperProps={{ style: { minWidth: '67vw' } }} anchor="right">
88
      <DrawerTitle
89
        title={`Tenant Information for ${name}`}
90
        onLinkCopy={copyLinkToClipboard}
91
        preCloser={<Button onClick={() => setShouldDelete(true)}>Delete tenant</Button>}
×
92
        onClose={onCloseClick}
93
      />
94
      <Divider className="margin-bottom-large" />
95
      <div className={classes.formWrapper}>
NEW
96
        <TwoColumns className="align-self-start" setSnackbar={(str: string) => dispatch(setSnackbar(str))} items={{ name, ID: id }} />
×
97
        <FormControlLabel control={<Checkbox color="primary" size="small" disabled checked={binary_delta} />} label="Enable Delta Artifact generation" />
98
        {!!ssoConfig && (
5!
99
          <>
100
            <FormControlLabel
101
              control={<Checkbox color="primary" size="small" checked disabled />}
102
              label="Restrict to Service Provider’s Single Sign-On settings"
103
            />
104
            <Link className={classes.ssoLink} to="/settings/organization-and-billing">
105
              View Single Sign-On settings
106
            </Link>
107
          </>
108
        )}
109
        <div className={`flexbox ${newLimitForm ? '' : 'center-aligned'} margin-top-small`}>
5✔
110
          <DeviceCount current={device_count} max={device_limit} variant="detailed" />
111
          <div className="margin-left">
112
            {newLimitForm ? (
5✔
113
              <FormControl className={classes.formWrapper}>
114
                <div className="flexbox center-aligned">
115
                  <TextField
116
                    className={classes.devLimitInput}
117
                    label="Set device limit"
118
                    type="number"
119
                    onChange={onChangeLimit}
120
                    slotProps={{ htmlInput: { min: device_count, max: currentLimit, 'data-testid': 'dev-limit-input' } }}
121
                    error={hasLimitError}
122
                    value={newLimit}
123
                  />
124
                  <Button
125
                    className="margin-left"
126
                    onClick={() => {
127
                      setNewLimit(device_limit);
×
128
                      setNewLimitForm(false);
×
129
                    }}
130
                  >
131
                    Cancel
132
                  </Button>
133
                  <Button className="margin-left-x-small" onClick={onNewLimitSubmit} color="primary" variant="contained">
134
                    Save
135
                  </Button>
136
                </div>
137
                <FormHelperText className={`${hasLimitError ? 'warning' : 'info'} margin-top-none`}>Maximum limit: {currentLimit}</FormHelperText>
3!
138
                <FormHelperText className="info margin-top-none">
139
                  {spDeviceUtilization} devices assigned of maximum {spDeviceLimit} across all tenants.
140
                  <br />
141
                  <a href="mailto:support@mender.io" target="_blank" rel="noopener noreferrer">
142
                    Contact support
143
                  </a>{' '}
144
                  to increase your total limit
145
                </FormHelperText>
146
              </FormControl>
147
            ) : (
148
              <Button onClick={() => setNewLimitForm(true)}>Edit device limit</Button>
1✔
149
            )}
150
          </div>
151
        </div>
152
        <ConfirmModal
153
          header="Are you sure you want to delete this tenant?"
154
          description="All devices, users, artifacts and audit logs associated with the tenant will be removed."
155
          toType="delete"
156
          open={shouldDelete}
NEW
157
          close={() => setShouldDelete(false)}
×
158
          onConfirm={() => {
NEW
159
            deleteTenant();
×
NEW
160
            setShouldDelete(false);
×
NEW
161
            onCloseClick();
×
162
          }}
163
        />
164
      </div>
165
    </Drawer>
166
  );
167
};
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