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

mendersoftware / gui / 1081664682

22 Nov 2023 02:11PM UTC coverage: 82.798% (-17.2%) from 99.964%
1081664682

Pull #4214

gitlab-ci

tranchitella
fix: Fixed the infinite page redirects when the back button is pressed

Remove the location and navigate from the useLocationParams.setValue callback
dependencies as they change the set function that is presented in other
useEffect dependencies. This happens when the back button is clicked, which
leads to the location changing infinitely.

Changelog: Title
Ticket: MEN-6847
Ticket: MEN-6796

Signed-off-by: Ihor Aleksandrychiev <ihor.aleksandrychiev@northern.tech>
Signed-off-by: Fabio Tranchitella <fabio.tranchitella@northern.tech>
Pull Request #4214: fix: Fixed the infinite page redirects when the back button is pressed

4319 of 6292 branches covered (0.0%)

8332 of 10063 relevant lines covered (82.8%)

191.0 hits per line

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

70.0
/src/js/components/settings/organization/ssoeditor.js
1
// Copyright 2022 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, { useCallback, useEffect, useRef, useState } from 'react';
15
import Dropzone from 'react-dropzone';
16

17
// material ui
18
import { Close as CloseIcon, CloudUpload, FileCopyOutlined as CopyPasteIcon } from '@mui/icons-material';
19
import { Button, Divider, Drawer, IconButton } from '@mui/material';
20

21
import Editor, { loader } from '@monaco-editor/react';
22
import copy from 'copy-to-clipboard';
23

24
import { createFileDownload } from '../../../helpers';
25
import Loader from '../../common/loader';
26

27
loader.config({ paths: { vs: '/ui/vs' } });
7✔
28

29
const editorProps = {
7✔
30
  defaultLanguage: 'xml',
31
  height: 700,
32
  language: 'xml',
33
  loading: <Loader show />,
34
  options: {
35
    autoClosingOvertype: 'auto',
36
    codeLens: false,
37
    contextmenu: false,
38
    enableSplitViewResizing: false,
39
    formatOnPaste: true,
40
    lightbulb: { enabled: false },
41
    lineNumbers: 'off',
42
    minimap: { enabled: false },
43
    quickSuggestions: false,
44
    readOnly: true,
45
    renderOverviewRuler: false,
46
    scrollBeyondLastLine: false,
47
    wordWrap: 'on'
48
  }
49
};
50

51
export const SSOEditor = ({ config, fileContent, hasSSOConfig, open, onCancel, onClose, onSave, setFileContent, token }) => {
7✔
52
  const [isEditing, setIsEditing] = useState(false);
21✔
53
  const [isMetadataValid, setIsMetadataValid] = useState(false);
21✔
54
  const editorRef = useRef();
21✔
55

56
  useEffect(() => {
21✔
57
    if (!fileContent) {
10✔
58
      return;
5✔
59
    }
60
    const parser = new DOMParser();
5✔
61
    const theDom = parser.parseFromString(fileContent, 'application/xml');
5✔
62
    setIsMetadataValid(!theDom.getElementsByTagName('parsererror').length);
5✔
63
  }, [fileContent]);
64

65
  const onEditClick = () => setIsEditing(true);
21✔
66

67
  const onDownloadClick = () => createFileDownload(fileContent, 'metadata.xml', token);
21✔
68

69
  const onCancelClick = useCallback(() => {
21✔
70
    if (isEditing) {
×
71
      setFileContent(config);
×
72
      if (!hasSSOConfig) {
×
73
        return onCancel();
×
74
      }
75
      return setIsEditing(false);
×
76
    }
77
    onCancel();
×
78
  }, [config, hasSSOConfig, isEditing, onCancel, setFileContent]);
79

80
  const onSubmitClick = () => {
21✔
81
    onSave();
×
82
    setIsEditing(false);
×
83
  };
84

85
  const onCopyClick = () => copy(fileContent);
21✔
86

87
  const onDrop = acceptedFiles => {
21✔
88
    let reader = new FileReader();
1✔
89
    reader.fileName = acceptedFiles[0].name;
1✔
90
    reader.onerror = error => {
1✔
91
      console.log('Error: ', error);
×
92
      setIsEditing(false);
×
93
    };
94
    reader.onload = () => {
1✔
95
      setFileContent(reader.result);
1✔
96
      setIsEditing(true);
1✔
97
    };
98
    reader.readAsBinaryString(acceptedFiles[0]);
1✔
99
  };
100

101
  const handleEditorDidMount = (editor, monaco) => {
21✔
102
    monaco.languages.html.registerHTMLLanguageService('xml', {}, { documentFormattingEdits: true });
×
103
    editorRef.current = { editor, monaco, modifiedEditor: editor };
×
104
  };
105

106
  return (
21✔
107
    <Drawer className={`${open ? 'fadeIn' : 'fadeOut'}`} anchor="right" open={open} onClose={onClose} PaperProps={{ style: { minWidth: '75vw' } }}>
21✔
108
      <div className="flexbox margin-bottom-small space-between">
109
        <h3>SAML metadata</h3>
110
        <div className="flexbox center-aligned">
111
          <Dropzone multiple={false} onDrop={onDrop}>
112
            {({ getRootProps, getInputProps }) => (
113
              <div {...getRootProps()}>
11✔
114
                <input {...getInputProps()} />
115
                <Button color="secondary" startIcon={<CloudUpload fontSize="small" />}>
116
                  Import from a file
117
                </Button>
118
              </div>
119
            )}
120
          </Dropzone>
121

122
          <IconButton onClick={onClose} size="large">
123
            <CloseIcon />
124
          </IconButton>
125
        </div>
126
      </div>
127
      <Divider light />
128
      <Editor
129
        {...editorProps}
130
        options={{
131
          ...editorProps.options,
132
          readOnly: hasSSOConfig && !isEditing
35✔
133
        }}
134
        className="editor modified"
135
        onChange={setFileContent}
136
        onMount={handleEditorDidMount}
137
        value={fileContent}
138
      />
139
      {!isMetadataValid && fileContent.length > 4 && <div className="error">There was an error parsing the metadata.</div>}
46✔
140
      <Divider className="margin-top-large margin-bottom" light />
141
      <div>
142
        {hasSSOConfig && !isEditing ? (
56✔
143
          <div className="flexbox center-aligned">
144
            <Button onClick={onEditClick}>Edit</Button>
145
            <Button onClick={onDownloadClick}>Download file</Button>
146
            <Button onClick={onCopyClick} startIcon={<CopyPasteIcon />}>
147
              Copy to clipboard
148
            </Button>
149
          </div>
150
        ) : (
151
          <>
152
            <Button onClick={onCancelClick}>Cancel</Button>
153
            <Button variant="contained" disabled={!isMetadataValid} onClick={onSubmitClick} color="secondary" style={{ marginLeft: 10 }}>
154
              Save
155
            </Button>
156
          </>
157
        )}
158
      </div>
159
    </Drawer>
160
  );
161
};
162

163
export default SSOEditor;
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