Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (140 loc) · 4.65 KB
/
Copy pathindex.js
File metadata and controls
157 lines (140 loc) · 4.65 KB
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
import { Header, Nav, Main, Footer } from "./components";
import * as store from "./store";
import Navigo from "navigo";
import { capitalize } from "lodash";
import axios from "axios";
const router = new Navigo("/");
function render(state = store.Home) {
document.querySelector("#root").innerHTML = `
${Header(state)}
${Nav(store.Links)}
${Main(state)}
${Footer()}
`;
router.updatePageLinks();
afterRender(state);
}
function afterRender(state) {
document.querySelector(".fa-bars").addEventListener("click", () => {
document.querySelector("nav > ul").classList.toggle("hidden--mobile");
});
if (state.view === "Browse") {
// Fetch artist data from the server
axios
.get("/api/artists")
.then(response => {
const artists = response.data;
const container = document.getElementById("artistsContainer");
container.innerHTML = ""; // Clear existing content
artists.forEach(artist => {
// Create artist div
const artistDiv = document.createElement("div");
artistDiv.className = "artist";
// Add username
const username = document.createElement("h3");
username.textContent = artist.username;
artistDiv.appendChild(username);
// Add medium
const medium = document.createElement("p");
medium.textContent = `Medium: ${artist.medium}`;
artistDiv.appendChild(medium);
// Add email
const email = document.createElement("p");
email.textContent = `Email: ${artist.email}`;
artistDiv.appendChild(email);
// Add description
const description = document.createElement("p");
description.textContent = `Description: ${artist.description}`;
artistDiv.appendChild(description);
// Append the artist div to the container
container.appendChild(artistDiv);
});
})
.catch(error => {
console.error("Error fetching artists:", error);
});
}
if (state.view === "CreateProfile") {
document.querySelector("form").addEventListener("submit", event => {
event.preventDefault();
const inputList = event.target.elements;
console.log("Input Element List", inputList);
const requestData = {
medium: inputList.medium.value,
username: inputList.username.value,
email: inputList.email.value,
experience: inputList.experience.value,
description: inputList.description.value
};
console.log("Request Body", requestData);
axios
.post(`${process.env.ARTIST_API_URL}/artists`, requestData)
.then(response => {
store.Artist.artists.push(response.data);
router.navigate("/Browse");
})
.catch(error => {
console.log("Error creating artist profile:", error);
});
});
}
}
router.hooks({
before: (done, params) => {
const view =
params && params.data && params.data.view
? capitalize(params.data.view)
: "Home";
// Add a switch case statement to handle multiple routes
switch (view) {
// New Case for the Home View
case "Home":
axios
// Get request to retrieve the current weather data using the API key and providing a city name
.get(
`https://api.openweathermap.org/data/2.5/weather?appid=${process.env.OPEN_WEATHER_MAP_API_KEY}&q=st%20louis`
)
.then(response => {
// Convert Kelvin to Fahrenheit since OpenWeatherMap does provide otherwise
const kelvinToFahrenheit = kelvinTemp =>
Math.round((kelvinTemp - 273.15) * (9 / 5) + 32);
// Create an object to be stored in the Home state from the response
store.Home.weather = {
city: response.data.name,
temp: kelvinToFahrenheit(response.data.main.temp),
feelsLike: kelvinToFahrenheit(response.data.main.feels_like),
description: response.data.weather[0].main
};
done();
})
.catch(err => {
console.log(err);
done();
});
break;
default:
done();
}
params => {
const view =
params && params.data && params.data.view
? capitalize(params.data.view)
: "Home";
render(store[view]);
};
}
});
router
.on({
"/": () => render(),
":view": params => {
let view = capitalize(params.data.view);
if (view in store) {
render(store[view]);
} else {
render(store.Viewnotfound);
console.log(`View ${view} not defined`);
}
}
})
.resolve();