forked from daftFunc/planEats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecipeSearch.js
166 lines (157 loc) · 4.25 KB
/
RecipeSearch.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React, { Component } from "react";
import swal from "sweetalert2";
import { Link } from "react-router-dom";
import "./RecipeSearch.css";
import axios from "axios";
import RecipeSearchResult from "./RecipeSearchResult";
import {
Form,
FieldGroup,
ControlLabel,
FormControl,
Button,
HelpBlock,
Panel
} from "react-bootstrap";
class RecipeSearch extends Component {
constructor(props) {
super(props);
this.state = {
username: JSON.parse(localStorage.profile).email,
recipes: null,
searchValue: null
};
}
componentDidMount() {
axios.defaults.headers.username = this.state.username;
axios
.get(
"https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/random?limitLicense=false&number=10",
{
headers: {
"X-Mashape-Key": process.env.REACT_APP_SPOONACULAR_API_KEY,
Accept: "application/json"
}
}
)
.then(res => {
this.setState({
recipes: res.data.recipes
});
});
}
handleChange(ignore, event) {
this.setState({ searchValue: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
axios
.get(
`https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/search?offset=0&query=${
this.state.searchValue
}`,
{
headers: {
"X-Mashape-Key": process.env.REACT_APP_SPOONACULAR_API_KEY,
Accept: "application/json"
}
}
)
.then(res => {
this.setState({
searchValue: null,
recipes: res.data.results
});
});
}
addToRecipeBook(recipeID) {
axios
.get(
`https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/${recipeID}/information?includeNutrition=true`,
{
headers: {
"X-Mashape-Key": process.env.REACT_APP_SPOONACULAR_API_KEY,
Accept: "application/json"
}
}
)
.then(res => {
axios
.post("/api/recipe", {
username: JSON.parse(localStorage.profile).email,
name: res.data.title,
recipe: res.data
})
.then(
swal("Added to your recipe book!", "We can taste it already. Yum!")
);
});
event.preventDefault();
}
displayRecipeSummary(recipe) {
axios
.get(
`https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/${recipe}/summary`,
{
headers: {
"X-Mashape-Key": process.env.REACT_APP_SPOONACULAR_API_KEY,
Accept: "application/json"
}
}
)
.then(res =>
swal({
imageWidth: 312,
imageHeight: 231,
title: res.data.title,
html: res.data.summary,
confirmButtonText: "Add To Recipe Book",
showCancelButton: true,
cancelButtonText: "Nah, not this one",
animation: true,
customClass: "animated bounceIn"
}).then(() => {
this.addToRecipeBook(res.data.id);
})
);
}
render() {
return (
<div className="RScontainer">
<h1 id="recipeBook">Search for a recipe.</h1>
<form>
<FormControl
id="recipeSearchBox"
type="text"
onChange={this.handleChange.bind(this, "searchValue")}
label="Recipe Seach"
placeholder="Natural language search. (ex: Gluten and dairy free brownies)"
/>
<Button
className="recipeSearchSubmitButton"
type="submit"
bsStyle="info"
onClick={this.handleSubmit.bind(this)}
>
Submit
</Button>
</form>
<h1>Results</h1>
<div>
{this.state.recipes
? this.state.recipes.map((recipe, i) => (
<RecipeSearchResult
className="item recipe"
recipe={recipe}
addToRecipeBook={this.addToRecipeBook.bind(this)}
displayRecipeSummary={this.displayRecipeSummary.bind(this)}
key={i}
/>
))
: null}
</div>
</div>
);
}
}
export default RecipeSearch;