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

mendersoftware / gui / 993759026

05 Sep 2023 09:01PM UTC coverage: 82.384% (-17.6%) from 99.964%
993759026

Pull #4020

gitlab-ci

mender-test-bot
chore: Types update

Signed-off-by: Mender Test Bot <mender@northern.tech>
Pull Request #4020: chore: Types update

4346 of 6321 branches covered (0.0%)

8259 of 10025 relevant lines covered (82.38%)

192.76 hits per line

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

69.44
/src/js/components/devices/device-details/devicetags.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 React, { useEffect, useState } from 'react';
15
import { useDispatch } from 'react-redux';
16

17
import { Edit as EditIcon } from '@mui/icons-material';
18
import { Button } from '@mui/material';
19
import { useTheme } from '@mui/material/styles';
20

21
import { setDeviceTags } from '../../../actions/deviceActions';
22
import { toggle } from '../../../helpers';
23
import Tracking from '../../../tracking';
24
import ConfigurationObject from '../../common/configurationobject';
25
import KeyValueEditor from '../../common/forms/keyvalueeditor';
26
import { HELPTOOLTIPS, MenderHelpTooltip } from '../../helptips/helptooltips';
27
import DeviceDataCollapse from './devicedatacollapse';
28

29
const NameTipComponent = props => <MenderHelpTooltip id={HELPTOOLTIPS.nameTagTip.id} {...props} />;
11✔
30

31
const configHelpTipsMap = {
11✔
32
  name: { component: NameTipComponent, position: 'right' }
33
};
34

35
export const DeviceTags = ({ device, setSnackbar, userCapabilities }) => {
11✔
36
  const { canWriteDevices } = userCapabilities;
6✔
37
  const theme = useTheme();
6✔
38
  const [changedTags, setChangedTags] = useState({});
6✔
39
  const [editableTags, setEditableTags] = useState();
6✔
40
  const [isEditDisabled, setIsEditDisabled] = useState(!canWriteDevices);
6✔
41
  const [isEditing, setIsEditing] = useState(false);
6✔
42
  const [shouldUpdateEditor, setShouldUpdateEditor] = useState(false);
6✔
43
  const dispatch = useDispatch();
6✔
44

45
  const { tags = {} } = device;
6✔
46
  const hasTags = !!Object.keys(tags).length;
6✔
47

48
  useEffect(() => {
6✔
49
    setShouldUpdateEditor(toggle);
4✔
50
  }, [isEditing]);
51

52
  useEffect(() => {
6✔
53
    if (canWriteDevices) {
2!
54
      setIsEditing(!hasTags);
2✔
55
    }
56
  }, [hasTags, canWriteDevices]);
57

58
  const onCancel = () => {
6✔
59
    setIsEditing(false);
×
60
    setChangedTags(tags);
×
61
  };
62

63
  const onStartEdit = e => {
6✔
64
    e.stopPropagation();
×
65
    setEditableTags(tags);
×
66
    setChangedTags(tags);
×
67
    setIsEditing(true);
×
68
  };
69

70
  const onSubmit = () => {
6✔
71
    Tracking.event({ category: 'devices', action: 'modify_tags' });
×
72
    setIsEditDisabled(true);
×
73
    return dispatch(setDeviceTags(device.id, changedTags))
×
74
      .then(() => setIsEditing(false))
×
75
      .finally(() => setIsEditDisabled(false));
×
76
  };
77

78
  const helpTipsMap = Object.entries(configHelpTipsMap).reduce((accu, [key, value]) => {
6✔
79
    accu[key] = {
6✔
80
      ...value,
81
      props: { deviceId: device.id }
82
    };
83
    return accu;
6✔
84
  }, {});
85
  return (
6✔
86
    <DeviceDataCollapse
87
      title={
88
        <div className="two-columns">
89
          <div className="flexbox center-aligned">
90
            <h4 className="margin-right">Tags</h4>
91
            {!isEditing && canWriteDevices && (
10✔
92
              <Button onClick={onStartEdit} startIcon={<EditIcon />} size="small">
93
                Edit
94
              </Button>
95
            )}
96
          </div>
97
        </div>
98
      }
99
    >
100
      <div className="relative" style={{ maxWidth: 700 }}>
101
        {isEditing ? (
6✔
102
          <>
103
            <KeyValueEditor
104
              disabled={isEditDisabled}
105
              errortext=""
106
              initialInput={editableTags}
107
              inputHelpTipsMap={helpTipsMap}
108
              onInputChange={setChangedTags}
109
              reset={shouldUpdateEditor}
110
            />
111
            <div className="flexbox center-aligned margin-bottom-small" style={{ justifyContent: 'flex-end' }}>
112
              <Button color="primary" onClick={onSubmit} variant="contained" style={{ marginRight: theme.spacing(2) }}>
113
                Save
114
              </Button>
115
              <Button onClick={onCancel}>Cancel</Button>
116
            </div>
117
          </>
118
        ) : (
119
          hasTags && <ConfigurationObject config={tags} setSnackbar={setSnackbar} />
2!
120
        )}
121
      </div>
122
    </DeviceDataCollapse>
123
  );
124
};
125

126
export default DeviceTags;
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