Jaime Ramírez
2020-06-11 d4efcf556bee5599b87a18da9420df2143e1c757
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
import React from "react";
import { AnimalService } from "../Services/AnimalService";
import {
    PageSection, PageSectionVariants, Text, TextContent, Button
} from "@patternfly/react-core";
import AdoptableAnimalList from "../Components/AdoptableAnimalList";
import { AdoptionService } from "../Services/AdoptionService";
import { Animal } from "../Models/Animal";
import { RESTConnectionError } from "../Services/RESTService";
import LoadingData from "../Components/LoadingData";
import { Link } from "react-router-dom";
 
 
type AnimalsViewProps = {
    animalService: AnimalService;
    adoptionService: AdoptionService;
}
 
type AnimalsViewState = {
    animals: Animal[],
    loading: boolean,
    error: {
        isActive: boolean,
        header: string,
        message: string
    }
}
 
 
export default class AnimalsView extends React.Component<AnimalsViewProps, AnimalsViewState> {
 
    constructor(props: AnimalsViewProps) {
        super(props);
        this.state = {
            animals: [],
            loading: false,
            error: {
                isActive: false,
                header: "",
                message: ""
            }
        };
    }
 
    public async componentDidMount() {
        this.setState({ loading: true });
 
        try {
            const animals = await this.props.animalService.getAllAdoptable();
            this.setState({ animals });
        } catch (error) {
            if (error instanceof RESTConnectionError) {
                this.showConnectionError(error);
            }
        } finally {
            this.setState({ loading: false });
        }
    }
 
    private showConnectionError(error: RESTConnectionError) {
        this.setState({
            error: {
                isActive: true,
                header: error.message,
                message: error.description,
            }
        });
    }
 
    private closeErrorAlert = () => {
        this.setState({
            error: {
                isActive: false,
                message: "",
                header: ""
            }
        });
    }
 
    public render() {
        const { error, loading } = this.state;
 
        return (
            <React.Fragment>
                <PageSection variant={PageSectionVariants.light}>
                    <TextContent>
                        <Text component="h1">Animals</Text>
                        <Text component="p">
                            These animals are waiting for you!
                        </Text>
                    </TextContent>
                </PageSection>
                <PageSection>
                    <LoadingData
                        showLoader={loading}
                        showError={error.isActive}
                        errorTitle={error.header}
                        errorDescription={error.message}
                        onErrorClosed={this.closeErrorAlert}
                        title="Adoptable Animals"
                    >
                        <AdoptableAnimalList animals={this.state.animals} />
                    </LoadingData>
                </PageSection>
                <PageSection variant={PageSectionVariants.light}>
                    <TextContent>
                        <Text component="h2">
                            Add new animal
                        </Text>
                        <Link to="/manage/animals/create">
                            <Button>Add</Button>
                        </Link>
                    </TextContent>
                </PageSection>
            </React.Fragment>
        );
    }
 
}