forked from btholt/complete-intro-to-react-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetails.js
More file actions
97 lines (91 loc) · 2.48 KB
/
Details.js
File metadata and controls
97 lines (91 loc) · 2.48 KB
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
import React, { lazy } from "react";
import pf from "petfinder-client";
import Carousel from "./Carousel";
import ErrorBoundary from "./ErrorBoundary";
import ThemeContext from "./ThemeContext";
const Modal = lazy(() => import("./Modal"));
const petfinder = pf({
key: process.env.API_KEY,
secret: process.env.API_SECRET
});
class Details extends React.Component {
state = { loading: true, showModal: false };
componentDidMount() {
petfinder.pet
.get({
output: "full",
id: this.props.id
})
.then(data => {
let breed;
if (Array.isArray(data.petfinder.pet.breeds.breed)) {
breed = data.petfinder.pet.breeds.breed.join(", ");
} else {
breed = data.petfinder.pet.breeds.breed;
}
this.setState({
name: data.petfinder.pet.name,
animal: data.petfinder.pet.animal,
location: `${data.petfinder.pet.contact.city}, ${
data.petfinder.pet.contact.state
}`,
description: data.petfinder.pet.description,
media: data.petfinder.pet.media,
breed,
loading: false
});
})
.catch(err => this.setState({ error: err }));
}
toggleModal = () => this.setState({ showModal: !this.state.showModal });
render() {
if (this.state.loading) {
return <h1>loading … </h1>;
}
const {
animal,
breed,
location,
description,
media,
name,
showModal
} = this.state;
return (
<div className="details">
<Carousel media={media} />
<div>
<h1>{name}</h1>
<h2>{`${animal} — ${breed} — ${location}`}</h2>
<ThemeContext.Consumer>
{([theme]) => (
<button
style={{ backgroundColor: theme }}
onClick={this.toggleModal}
>
Adopt {name}
</button>
)}
</ThemeContext.Consumer>
<p>{description}</p>
{showModal ? (
<Modal>
<h1>Would you like to adopt {name}?</h1>
<div className="buttons">
<button onClick={this.toggleModal}>Yes</button>
<button onClick={this.toggleModal}>No</button>
</div>
</Modal>
) : null}
</div>
</div>
);
}
}
export default function DetailsErrorBoundary(props) {
return (
<ErrorBoundary>
<Details {...props} />
</ErrorBoundary>
);
}