mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
11.31.2019 403fb2eb98deb2e352f700b2503fdf5b27679c96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React from 'react';
import {Alert} from 'reactstrap';
import {FormButton, FormField, FormGroup, FormLabel} from '../../general/forms/FormComponents';
import PropTypes from "prop-types";
import {getRestServiceUrl} from "../../../utilities/global";
import ErrorAlert from "../../general/ErrorAlert";
 
class RepoConfigTestPanel extends React.Component {
 
    constructor(props) {
        super(props);
        this.state = {
            testStatus: undefined,
            testResult: undefined
        };
        this.onTest = this.onTest.bind(this);
    }
 
    onTest(event) {
        this.setState({
            testStatus: 'fetching',
            testResult: undefined
        });
        fetch(getRestServiceUrl("repoConfig/check"), {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(this.props.repoConfig)
        })
            .then(response => response.text())
            .then(result => {
                //console.log("test-result: " + result);
                this.setState({
                    testStatus: result === 'OK' ? 'OK' : 'error',
                    testResult: result
                });
            })
            .catch((error) => {
                console.log("error", error);
                this.setState({
                    testStatus: 'exception',
                    testResult: undefined
                });
            });
    }
 
    render() {
        let testButtonColor = 'outline-info';
        if (this.state.testStatus === 'OK') {
            testButtonColor = 'outline-success';
        } else if (this.state.testStatus === 'error' || this.state.testStatus === 'exception' || this.props.repoError) {
            testButtonColor = 'outline-danger';
        } else {
            testButtonColor = 'outline-info';
        }
        let testResult = undefined;
        if (!this.state.testStatus) {
            // No test available.
        } else if (this.state.testStatus === 'exception') {
            testResult = <ErrorAlert title={'Unknown error'} description={'Internal error while calling Rest API.'}/>;
        } else if (this.state.testStatus === 'OK') {
            testResult = <Alert color={'success'}>
                OK
            </Alert>;
        } else if (this.state.testStatus === 'fetching') {
            testResult = <Alert color={'warning'}>
                Testing...
            </Alert>;
        } else {
            testResult = <ErrorAlert
                title={'Error while testing repo configuration'}
                description={this.state.testResult}
            />
        }
        return <FormGroup>
            <FormLabel length={2}>
            </FormLabel>
            <FormField length={10}>
                <FormButton onClick={this.onTest} disabled={this.state.testStatus === 'fetching'}
                            bsStyle={testButtonColor}
                            hint={'Tries to connect to the repo and to get info from.'}>Test
                </FormButton>
                {testResult}
            </FormField>
        </FormGroup>;
    }
}
 
RepoConfigTestPanel.propTypes = {
    repoConfig: PropTypes.object.isRequired,
    repoError: PropTypes.bool
};
 
RepoConfigTestPanel.defaultProps = {
    repoError: false
};
 
export default RepoConfigTestPanel;