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

...
Kai Reinhard
10.01.2018 5c6ef91b43f0b0cc225b1a5b8abdc08b670a9317
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React from 'react'
import './RepoListView.css';
import {CardDeck} from 'reactstrap';
import {PageHeader} from '../../general/BootstrapComponents';
import {getRestServiceUrl} from '../../../utilities/global';
import ErrorAlert from '../../general/ErrorAlert';
import TemplateCard from './TemplateCard';
import {IconRefresh} from "../../general/IconComponents";
import I18n from "../../general/translation/I18n";
 
class RepoListView extends React.Component {
 
 
    path = getRestServiceUrl('repos');
    state = {
        isFetching: false
    };
 
    componentDidMount = () => {
        this.fetchTemplates();
    };
 
    fetchTemplates = () => {
        this.setState({
            isFetching: true,
            failed: false,
            definitions: undefined,
            templates: undefined
        });
        fetch(`${this.path}/list`, {
            method: 'GET',
            headers: {
                'Accept': 'application/json'
            }
        })
            .then(response => response.json())
            .then(json => {
                const definitions = json.templateDefinitions.reduce((accumulator, current) => ({
                    ...accumulator,
                    [current.refId]: current
                }), {});
 
                const templates = json.templates.map(template => {
                    if (typeof template.templateDefinition === 'object') {
                        //console.log('refId: ' + template.templateDefinition.refId + ', templateDefinition: ' + JSON.stringify(template.templateDefinition))
                        definitions[template.templateDefinition.refId] = template.templateDefinition;
                        template.templateDefinition = template.templateDefinition.refId;
                    }
 
                    return {
                        id: template.id,
                        primaryKey: template.fileDescriptor.primaryKey,
                        filename: template.fileDescriptor.filename,
                        lastModified: template.fileDescriptor.lastModified,
                        templateDefinitionId: template.templateDefinitionId,
                        templateDefinition: template.templateDefinition
                    };
                });
 
                this.setState({
                    isFetching: false,
                    definitions, templates
                })
            })
            .catch(() => this.setState({isFetching: false, failed: true}));
    };
 
    render = () => {
        let content = undefined;
 
        if (this.state.isFetching) {
 
            content = <i>Loading...</i>;
 
        } else if (this.state.failed) {
 
            content = <ErrorAlert
                title={'Cannot load Templates'}
                description={'Something went wrong during contacting the rest api.'}
                action={{
                    handleClick: this.fetchTemplates,
                    title: 'Try again'
                }}
            />;
 
        } else if (this.state.templates) {
 
            content = <React.Fragment>
                <div
                    className={'btn btn-outline-primary refresh-button-right'}
                    onClick={this.fetchTemplates}
                >
                    <IconRefresh/>
                </div>
                <CardDeck>
                {this.state.templates.map(template => {
                    const definition = this.state.definitions[template.templateDefinition];
 
                    return <TemplateCard
                        key={template.primaryKey}
                        template={template}
                        definition={definition}
                    />;
                })}
                </CardDeck>
            </React.Fragment>;
 
        }
 
        return <React.Fragment>
            <PageHeader>
                <I18n name={'templates'}/>
            </PageHeader>
            {content}
        </React.Fragment>;
    };
 
    constructor(props) {
        super(props);
 
        this.fetchTemplates = this.fetchTemplates.bind(this);
    }
}
 
export default RepoListView;