-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
366 lines (352 loc) · 11.9 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { useReducer } from "react";
import Head from "next/head";
import Image from "next/image";
import Ripples from "react-ripples";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import gitHubPic from "../public/GitHub.png";
const initialState = {
food: null,
loading: false,
err: "",
foodRestriction: {
vegan: false,
vegetarian: false,
glutenFree: false,
keto: false,
peanutFree: false,
},
};
function reducer(state, action) {
switch (action.type) {
case "FIND_FOOD_CLICKED":
return { ...state, loading: true, error: "" };
case "FOOD_FETCHED":
return { ...state, food: action.food, loading: false };
case "FOOD_FETCH_ERROR":
return { ...state, loading: false };
case "RETURN_MENU":
return { ...state, loading: true };
case "NO_GEO_LOCALISATION":
return { ...state, error: action.error };
case "FOOD_RESTRICTION_VEGAN_SELECTED":
return {
...state,
foodRestriction: {
...state.foodRestriction,
vegan: !state.foodRestriction.vegan,
},
};
case "FOOD_RESTRICTION_VEGETARIAN_SELECTED":
return {
...state,
foodRestriction: {
...state.foodRestriction,
vegetarian: !state.foodRestriction.vegetarian,
},
};
case "FOOD_RESTRICTION_GLUTEN_FREE_SELECTED":
return {
...state,
foodRestriction: {
...state.foodRestriction,
glutenFree: !state.foodRestriction.glutenFree,
},
};
case "FOOD_RESTRICTION_KETO_SELECTED":
return {
...state,
foodRestriction: {
...state.foodRestriction,
keto: !state.foodRestriction.keto,
},
};
case "FOOD_RESTRICTION_PEANUT_FREE_SELECTED":
return {
...state,
foodRestriction: {
...state.foodRestriction,
peanutFree: !state.foodRestriction.peanutFree,
},
};
}
}
export async function getServerSideProps(context) {
return {
props: {
clientId: process.env.GOOGLE_AD_SENSE_CLIENT,
slot: process.env.GOOGLE_AD_SENSE_SLOT,
},
};
}
export default function Home({ clientId, slot }) {
const [state, dispatch] = useReducer(reducer, initialState);
const findYelpFood = async (position) => {
let response = await fetch(
position && !position.code
? "/api/yelp?" +
new URLSearchParams({
long: position.coords.longitude,
lat: position.coords.latitude,
foodRestriction: JSON.stringify(state.foodRestriction),
})
: "/api"
);
try {
if (response.ok) {
const result = await response.json();
dispatch({ type: "FOOD_FETCHED", food: result.foodType });
} else {
toast.error("Error while finding what to order. Please retry!", {
theme: "colored",
});
dispatch({ type: "FOOD_FETCH_ERROR" });
}
} catch (e) {
toast.error("Error while finding what to order. Please retry!", {
theme: "colored",
});
dispatch({ type: "FOOD_FETCH_ERROR" });
}
};
const askLocation = async () => {
dispatch({ type: "FIND_FOOD_CLICKED" });
if ("geolocation" in navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(findYelpFood, () => {
dispatch({
type: "NO_GEO_LOCALISATION",
error:
"Can't show recommendations. Please be sure to be in secure connection using https.",
});
findYelpFood();
});
} else {
dispatch({
type: "NO_GEO_LOCALISATION",
error:
"Can't show recommendations because your browser doesn't support Geolocation. (Facebook browser)",
});
findYelpFood();
}
};
const askRecipe = async () => {
dispatch({ type: "FIND_FOOD_CLICKED" });
let response = await fetch(
"/api/edamam?" +
new URLSearchParams({
foodRestriction: JSON.stringify(state.foodRestriction),
})
);
try {
if (response.ok) {
const result = await response.json();
dispatch({ type: "FOOD_FETCHED", food: result.foodType });
} else {
toast.error("Error while finding the recipe. Please retry!", {
theme: "colored",
});
dispatch({ type: "FOOD_FETCH_ERROR" });
}
} catch (e) {
toast.error("Error while finding the recipe. Please retry!", {
theme: "colored",
});
dispatch({ type: "FOOD_FETCH_ERROR" });
}
};
return (
<div className="container">
<div className={`loading ${state.loading ? "" : "hide"}`}>
<div className="loading-text">
<div className="lds-ring">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
</div>
<Head>
<title>Whatoeat?</title>
<meta name="title" content="What to eat?" />
<meta
name="description"
content="What to eat? is an application that makes the hard decision for us and decide where to eat for the user. It also gives suggestion of restaurants you could go eating."
/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="google" content="nositelinkssearchbox" key="sitelinks" />
<meta name="google" content="notranslate" key="notranslate" />
<meta name="twitter:card" content="summary"></meta>
<meta name="twitter:site" content="@whatoeat"></meta>
<meta name="twitter:creator" content="Whassa"></meta>
<meta name="twitter:image" content="" />
<meta property="og:title" content="What to eat?" />
<meta
property="og:description"
content="What to eat? is an application that makes the hard decision for us and decide where to eat for the user. It also gives suggestion of restaurants you could go eating."
/>
<meta property="og:image" content="/static/images/logo.png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta property="og:url" content="https://whatoeat.ca" />
<link rel="icon" href="/favicon.ico" />
<meta name="robots" content="all" />
</Head>
<main className="main">
<div className="content">
<div className="content-left">
<h1 className="title">What to eat ?</h1>
<h2 className="food-restriction">
Do you have any dietary restriction * ?
</h2>
<span className="food-restriction-subtitle">
* This only affects recipes recommendations
</span>
<div className="food-restriction-options">
<input
type="checkbox"
id="vegan"
name="vegan"
value={state.foodRestriction.vegan}
onClick={() => {
dispatch({ type: "FOOD_RESTRICTION_VEGAN_SELECTED" });
}}
/>
<label htmlFor="vegan">Vegan</label>
<input
type="checkbox"
id="vegetarian"
name="vegetarian"
value={state.foodRestriction.vegetarian}
onClick={() => {
dispatch({ type: "FOOD_RESTRICTION_VEGETARIAN_SELECTED" });
}}
/>
<label htmlFor="vegetarian">Vegetarian</label>
<input
type="checkbox"
id="gluten-free"
name="gluten-free"
value={state.foodRestriction.glutenFree}
onClick={() => {
dispatch({ type: "FOOD_RESTRICTION_GLUTEN_FREE_SELECTED" });
}}
/>
<label htmlFor="gluten-free">Gluten free</label>
<input
type="checkbox"
id="keto"
name="keto"
value={state.foodRestriction.keto}
onClick={() => {
dispatch({ type: "FOOD_RESTRICTION_KETO_SELECTED" });
}}
/>
<label htmlFor="keto">Keto</label>
<input
type="checkbox"
id="peanutFree"
name="peanutFree"
value={state.foodRestriction.peanutFree}
onClick={() => {
dispatch({ type: "FOOD_RESTRICTION_PEANUT_FREE_SELECTED" });
}}
/>
<label htmlFor="peanutFree">Peanut free</label>
</div>
{state.food ? (
<>
<h2 className="food-title">{state.food.name}</h2>
{state.food.urls && state.food.urls.length > 0 && (
<div className="food-url-text">
Here is some place you could order :
</div>
)}
{state.food.urls &&
state.food.urls.map((restaurant) => (
<div key={restaurant.name} className="food-url">
<a
href={restaurant.url}
target="_blank"
rel="noopener noreferrer"
>
{" "}
{restaurant.name}
</a>
</div>
))}
{state.food.recipes && state.food.recipes.length > 0 && (
<div className="food-url-text">
Here is some things you could cook :
</div>
)}
{state.food.recipes &&
state.food.recipes.map((recipe) => (
<div key={recipe.label} className="food-url">
<a
href={recipe.url}
target="_blank"
rel="noopener noreferrer"
>
{" "}
{recipe.label}
</a>
</div>
))}
{(!state.food.recipes && !state.food.urls && !state.error) && <div>No recommendations for you</div>}
{state.error && <div className="food-error">{state.error}</div>}
<p
className={`description ${
state.food && state.food.urls ? "mt-5" : ""
}`}
>
Not satisfied ? You can retry by clicking one of the buttons
below.
</p>
</>
) : (
<p className="description">
Ask no more we resolve that issue for you by clicking on the
button below.
</p>
)}
<div className="buttons">
<Ripples>
<button className="button ripple" onClick={askLocation}>
Find me what food to order!!!
</button>
</Ripples>
<Ripples>
<button className="button ripple" onClick={askRecipe}>
Find me what food to cook!!!
</button>
</Ripples>
</div>
</div>
<div className="content-right"></div>
</div>
</main>
<ToastContainer
position="bottom-center"
autoClose={2000}
hideProgressBar
newestOnTop={false}
closeOnClick
rtl={false}
pauseOnFocusLoss={false}
draggable
pauseOnHover
/>
<footer className="footer">
<a
href="https://github.com/whassa"
target="_blank"
rel="noopener noreferrer"
>
<span className="footer-text">Made by whassa</span>
<Image src={gitHubPic} alt="Git Hub Logo" />
</a>
</footer>
</div>
);
}