-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
110 lines (94 loc) · 2.91 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
import View from "./view/View.js";
import Main from "./view/Home/Main.js";
import NotFound from "./view/NotFound.js";
import { $ } from "./utils/dom.js";
import Search from "./view/Home/Search.js";
import uploadModel from "./Model/uploadModel.js";
import Uploads from "./view/Upload/Uploads.js";
import { productDetailModel } from "./Model/productDetailModel.js";
import productDetail from "./view/productDetail.js";
import { variables } from "./constants.js";
class app {
constructor() {
this.render();
}
router = async () => {
$("#app").innerHTML = "";
const routes = [
{ path: "/", view: View },
{ path: "/uploads", view: Uploads },
{ path: "/search", view: Search },
];
const pageMatches = routes.map((route) => {
return {
route, // route: route
isMatch: route.path === location.pathname,
};
});
let match = pageMatches.find((pageMatch) => pageMatch.isMatch);
if (!match) {
match = {
route: location.pathname,
isMatch: true,
};
const page = new NotFound();
$("#app").innerHTML = page.getHtml();
}
if (location.pathname === "/uploads") {
new Uploads($("#app"));
new uploadModel();
}
if (location.pathname === "/") {
const getProducts = await axios.get("http://127.0.0.1:8080/products");
if (variables.initialApproach) {
new View();
variables.initialApproach = false;
}
this.makeProductsFrame();
new Main($("#product-list"), getProducts);
this.initEventListener();
}
if (location.pathname === "/search") {
new Search(null);
}
};
makeProductsFrame = () => {
let productList = document.createElement("div");
productList.id = "product-list";
$("#app").appendChild(productList);
};
initEventListener = () => {
$("#product-list").addEventListener("click", async (e) => {
history.pushState(null, null, `./products/${e.target.id}`);
const getProductDetail = await productDetailModel(e.target.id);
new productDetail($("#app"), getProductDetail);
});
$("#input-search").addEventListener("click", () => {
history.pushState(null, null, "/search");
this.router();
});
$("#logo").addEventListener("click", () => {
history.pushState(null, null, "/");
this.router();
});
window.addEventListener("popstate", this.router);
$("#input-search").addEventListener("keypress", async (e) => {
if (e.key !== "Enter") {
return;
}
e.preventDefault();
let value = e.target.value;
const getProducts = await axios.get("http://127.0.0.1:8080/products");
new Search(getProducts, value);
});
$("#upload-btn").addEventListener("click", (e) => {
e.preventDefault();
history.pushState(null, null, e.target.href);
this.router();
});
};
render = async () => {
await this.router();
};
}
new app();