-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
266 lines (230 loc) · 7.33 KB
/
app.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
// 1. import modules
import h from "./builder-function.js";
import navBarChange from "./navbar.js";
import { loginSubmit, signupSubmit, addHarvest } from "./api.js";
import { authorisationFunction } from "./auhorisation.js"
const login = document.getElementById("log-in");
const signUp = document.getElementById("sign-up");
const main = document.querySelector("main");
const homeButton = document.getElementById("home");
let app = document.querySelector("#app");
let seeAll = document.querySelector(".see-all");
let addHarvestButton = document.querySelector(".add-harvest");
// 2. function creates forms (signup/login)
const createForm = (parameter, routes) => {
main.innerHTML = "";
const url = "https://week7-chjm.herokuapp.com/" + routes;
const title = h("h2", {}, parameter);
// elements inside the form
const usernameLabel = h("label", { htmlFor: "username" }, "Username");
const username = h("input", {
type: "text",
id: "username",
name: "username",
required: "true",
});
const passwordLabel = h("label", { htmlFor: "password" }, "Password");
const password = h("input", {
type: "password",
id: "password",
name: "password",
required: "true",
});
const submitButton = h("input", { type: "submit" });
const form = h(
"form",
{
onsubmit: (event) => {
event.preventDefault();
const username = event.target.elements.username.value;
const password = event.target.password.value;
if (parameter === "Login") {
loginSubmit(username, password, url).then((user) => {
// save the access token in localStorage so the user stays logged in
window.localStorage.setItem("access_token", user.access_token);
createHome();
});
} else {
const email = event.target.elements.email.value;
signupSubmit(username, email, password, url).then((user) => {
window.localStorage.setItem("access_token", user.access_token);
createHome();
});
}
},
},
usernameLabel,
username,
passwordLabel,
password,
submitButton
);
if (parameter === "Sign Up") {
const emailLabel = h("label", { htmlFor: "email" }, "Email address");
const email = h("input", {
type: "email",
id: "email",
name: "email",
required: "true",
});
form.insertBefore(emailLabel, form.childNodes[0]);
form.insertBefore(email, form.childNodes[1]);
}
// create form with above elements as children
main.append(title, form);
return app;
};
login.addEventListener("click", () => createForm("Login", "login")); //both params here must be lowercase
signUp.addEventListener("click", () => createForm("Sign Up", "signup"));
//3. Create form to post new food
export const addHarvestFunction = () => {
app.innerHTML = "";
const url = "https://week7-chjm.herokuapp.com/harvest";
const harvestTitle = h("h2", {className: "add-crop-title"}, "Add a new crop");
// elements inside the form
const foodTypeLabel = h("label", { htmlFor: "foodType" }, "Food Type");
const foodType = h(
"input",
{ type: "text", id: "foodType", name: "foodType", required: "true" },
""
);
const tasteLabel = h("label", { htmlFor: "taste" }, "Taste");
const taste = h(
"input",
{ type: "text", id: "taste", name: "taste", required: "true" },
""
);
const harvestTimeLabel = h("label", { htmlFor: "harvestTime" }, "Harvest Time");
const harvestTime = h(
"input",
{ type: "text", id: "harvestTime", name: "harvestTime", required: "true" },
""
);
const locationLabel = h("label", { htmlFor: "location" }, "Location");
const location = h(
"input",
{ type: "text", id: "location", name: "location", required: "true" },
""
);
const dateLabel = h("label", { htmlFor: "date" }, "Date");
const date = h(
"input",
{ type: "text", id: "date", name: "date", required: "true" },
""
);
const submitHarvestButton = h("input", { type: "submit", }, "Add");
// create form with above elements as children
const harvestForm = h(
"form",
{
onsubmit: (event) => {
event.preventDefault();
const foodType = event.target.elements.foodType.value;
const taste = event.target.elements.taste.value;
const harvestTime = event.target.elements.harvestTime.value;
const location = event.target.elements.location.value;
const date = event.target.elements.date.value;
addHarvest(foodType, taste, harvestTime, location, date, url);
}
},
foodTypeLabel,
foodType,
tasteLabel,
taste,
harvestTimeLabel,
harvestTime,
locationLabel,
location,
dateLabel,
date,
submitHarvestButton
);
app.append(harvestTitle, harvestForm);
return app;
};
//On button click, getAllHarvest is called
//Will go to model and pull out all entries
//Return a call to displayAllHarvest which will place them on the page
function getAllHarvest(url) {
return fetch(url)
.then((response) => {
return response.json();
})
.then((jsonObj) => {
return displayAllHarvest(jsonObj);
});
}
// 4. function creates elements
const displayAllHarvest = (jsonObject) => {
app.innerHTML = "";
const searchButton = h("input", { type: "button" }, "search");
//let post = "";
jsonObject.forEach((data) => {
const post = h("div", { className: "harvestPost" }, "");
const fruit = h("p", {}, `Fruit: ${data.food_type}`);
const taste = h("p", {}, `Taste: ${data.taste}`);
const harvestTime = h("p", {}, `Harvest Time: ${data.harvest_time}`);
const location = h("p", {}, `Location: ${data.location}`);
const date = h("p", {}, `Date: ${data.date}`);
const deleteButton = h(
"button",
{ type: "button", className: "delete-button" },
"delete"
);
const updateButton = h(
"button",
{ type: "button", className: "update-button" },
"edit"
);
post.append(
fruit,
taste,
harvestTime,
location,
date,
deleteButton,
updateButton
);
app.append(post);
});
};
// nav bar update on logged-in status
window.onload = navBarChange;
// display all harvests on click
seeAll.addEventListener("click", () =>
getAllHarvest("https://week7-chjm.herokuapp.com/harvest")
);
const createHome = () => {
const homeHTML = `
<main>
<embed src="apple.svg" class="apple" width="250em" height="250em" />
<span class="logo"><p>Urban Harvest</p></span>
<section class="about">
<p class="h1-subtitle">
Urban Harvest is a celebration of the overlooked bounty grown in and
around our cities. It's a database to help foragers find produce in
their neighborhoods.
</p>
</section>
<section class="harvest-links">
<button class="add-harvest">Add a discovery</button>
<button class="search-harvest">Search the field</button>
<button class="see-all">See all</button>
</section>
<div id="app">
</div>
<script src="./app.js" type="module"></script>
</main>
`;
main.innerHTML = "";
main.innerHTML = homeHTML;
seeAll = document.querySelector(".see-all");
app = document.querySelector("#app");
seeAll.addEventListener("click", () =>
getAllHarvest("https://week7-chjm.herokuapp.com/harvest")
);
authorisationFunction();
navBarChange();
};
homeButton.onclick = createHome;
authorisationFunction();