-
Notifications
You must be signed in to change notification settings - Fork 0
/
Details.js
85 lines (81 loc) · 2.18 KB
/
Details.js
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
import React from "react";
import pet from "@frontendmasters/pet";
import { navigate } from "@reach/router";
import Carousel from "./Carousel";
import Modal from "./Modal";
import ErrorBoundary from "./ErrorBoundary";
import ThemeContext from "./ThemeContext";
class Details extends React.Component {
state = { loading: true, showModal: false };
componentDidMount() {
pet
.animal(this.props.id)
.then(({ animal }) => {
this.setState({
url: animal.url,
name: animal.name,
animal: animal.type,
location: `${animal.contact.address.city}, ${
animal.contact.address.state
}`,
description: animal.description,
media: animal.photos,
breed: animal.breeds.primary,
loading: false
});
})
.catch(err => this.setState({ error: err }));
}
toggleModal = () => this.setState({ showModal: !this.state.showModal });
adopt = () => navigate(this.state.url);
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
onClick={this.toggleModal}
style={{ backgroundColor: theme }}
>
Adopt {name}
</button>
)}
</ThemeContext.Consumer>
<p>{description}</p>
{showModal ? (
<Modal>
<h1>Would you like to adopt {name}?</h1>
<div className="buttons">
<button onClick={this.adopt}>Yes</button>
<button onClick={this.toggleModal}>No</button>
</div>
</Modal>
) : null}
</div>
</div>
);
}
}
export default function DetailsErrorBoundary(props) {
return (
<ErrorBoundary>
<Details {...props} />
</ErrorBoundary>
);
}