-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
194 lines (170 loc) · 5.25 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
// import food from "./icon/mealTime.svg";
import { Nav, Main, Footer } from "./components";
import * as state from "/store";
import Navigo from "navigo";
import { capitalize } from "lodash";
import axios from "axios";
import dotenv from "dotenv";
dotenv.config();
const router = new Navigo(window.location.origin);
//import slides from "./lib/functions/slideShow";
function render(st = state.Home) {
document.querySelector("#root").innerHTML = `
${Nav(state.Links)}
${Main(st)}
${Footer()}
`;
router.updatePageLinks();
addEventListener(st);
}
function addEventListener(st) {
// Burger Functionality
const nav = document.querySelector(".nav-links");
const burger = document.querySelector(".burger");
const links = nav.querySelectorAll("a");
burger.addEventListener("click", () => {
nav.classList.toggle("nav-open");
burger.classList.toggle("toggle");
});
links.forEach(link => {
link.addEventListener("click", () => {
nav.classList.toggle("nav-open");
burger.classList.toggle("toggle");
});
});
// Navbar transition functionality
var prevScrollpos = window.pageYOffset;
window.onscroll = () => {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-100px";
}
prevScrollpos = currentScrollPos;
};
// Slide show functionality
if (st.view === "Home") {
let slidePosition = 0;
const slides = document.getElementsByClassName("carouselItem");
const totalSlides = slides.length;
const updateSlidePosition = () => {
for (let slide of slides) {
slide.classList.remove("carouselItemVisible");
slide.classList.add("carouselItemHidden");
}
slides[slidePosition].classList.add("carouselItemVisible");
};
const moveToPrevSlide = () => {
if (slidePosition === 0) {
slidePosition = totalSlides - 1;
} else {
slidePosition--;
}
updateSlidePosition();
};
const moveToNexSlide = () => {
if (slidePosition === totalSlides - 1) {
slidePosition = 0;
} else {
slidePosition++;
}
updateSlidePosition();
};
document.getElementById("carouselBtnNex").addEventListener("click", () => {
moveToNexSlide();
});
document.getElementById("carouselBtnPrev").addEventListener("click", () => {
moveToPrevSlide();
});
}
// Spoontacular API
if (st.view === "Recipes") {
const form = document.getElementById("myForm");
form.addEventListener("submit", event => {
event.preventDefault();
// form.style.display = "none";
const inputList = event.target.elements;
console.log("elements", event.target.elements);
const mealTimeData = {
meal: inputList.meal.value,
time: inputList.time.value,
diet: inputList.diet.value
};
axios
.get(
`https://api.spoonacular.com/recipes/complexSearch?apiKey=${process.env.RECIPES_API_KEY}&query=${mealTimeData.meal}&diet=${mealTimeData.diet}&maxReadyTime=${inputList.time.value}&addRecipeInformation=true&instructionsRequired/`
)
.then(response => {
state.Recipes.get = {};
state.Recipes.get.data = response.data.results[0].sourceUrl;
// window.location = state.Recipes.get.data;
window.open(state.Recipes.get.data, "_blank");
})
.catch(error => {
console.log("It puked", error);
});
axios
.post(`${process.env.API}/recipes`, mealTimeData)
.then(response => {
state.Recipes.recipes.push(response.data);
router.navigate("/Recipes");
})
.catch(error => {
console.log("It", error);
});
});
}
}
router.hooks({
before: (done, params) => {
const page =
params && params.hasOwnProperty("page")
? capitalize(params.page)
: "Home";
switch (page) {
case "Recipe":
axios
.post(`${process.env.API}/recipes`)
.then(response => {
console.log("response", response.data);
// console.log("state", state.Recipes);
state.Recipes.recipes.push(response.data);
router.navigate("/Recipes");
})
.catch(error => {
console.log("It puked", error);
done();
});
break;
default:
done();
}
}
// switch (page) {
// case "Recipe":
// axios
// .get(
// `https://api.spoonacular.com/recipes/complexSearch?apiKey=${process.env.SPOONTACULAR_KEY}&query=taco&diet=Pescetarian&addRecipeInformation=true&instructionsRequired/posts/recipes`
// )
// .then(response => {
// state[page].recipes = response.data;
// console.log(response.data);
// done();
// // console.log(response, state.Home.weather);
// })
// .catch(error => {
// console.log("Need Instructions Please", error);
// done();
// });
// break;
// default:
// done();
// }
});
router
.on({
"/": () => render(state.Home),
":page": params => render(state[capitalize(params.page)])
})
.resolve();