-
Notifications
You must be signed in to change notification settings - Fork 639
/
script.js
131 lines (105 loc) · 3.68 KB
/
script.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
const projectsArea = document.querySelector(".project-list-area");
const searchBar = document.getElementById("search-bar");
let allProjects = []
function getImagePath(id) {
return `public/assets/${id}.png`;
}
function getProjectUrl(name) {
return `./projects/${name.toLowerCase().replace(/ /g, '-')}/`;
}
function getGithubUrl(name) {
const repoName = name.toLowerCase().replace(/ /g, '-');
return `https://github.com/swapnilsparsh/30DaysOfJavaScript/tree/master/projects/${repoName}`;
}
function getProjectImg(project) {
const imgParent = document.createElement("div");
imgParent.className = "project-img-cont";
const imgElm = document.createElement("img");
imgElm.src = project.image;
imgElm.setAttribute("alt", project.name);
imgParent.appendChild(imgElm);
return imgParent;
}
function createAnchorElm(href, value, className, attrName, attrValue) {
const anchorElm = document.createElement("a");
anchorElm.innerText = value;
anchorElm.href = href;
anchorElm.className = className;
anchorElm.setAttribute(attrName, attrValue);
return anchorElm;
}
function getProjectLinks(project) {
const linkContainer = document.createElement("div");
linkContainer.className = "links";
const websiteLink = createAnchorElm(project.url, 'Live', "btn", "target", "_blank");
const githubLink = createAnchorElm(project.github, 'Github', "btn", "target", "_blank");
linkContainer.append(websiteLink, githubLink);
return linkContainer;
}
function getProjectContent(project) {
const contentContainer = document.createElement("div");
contentContainer.className = "project-detail";
const contentElm = document.createElement("div");
contentElm.className = "project-content";
const projectName = document.createElement("h2");
projectName.innerText = project.name;
const projectDescription = document.createElement("p");
projectDescription.innerText = project.description;
contentElm.append(projectName, projectDescription);
contentContainer.appendChild(contentElm);
return contentContainer;
}
function renderProjectList(projects) {
projectsArea.innerHTML = ""
if (projects.length === 0) {
const noResultsMessage = document.createElement("p");
noResultsMessage.className = "no-results";
noResultsMessage.innerText = "No results found";
projectsArea.appendChild(noResultsMessage);
return;
}
projects.forEach((project) => {
const projectCard = document.createElement("div");
projectCard.className = "project-card";
const projectImg = getProjectImg({
...project,
image: getImagePath(project.id),
});
const projectContent = getProjectContent(project);
const projectLinks = getProjectLinks({
...project,
url: getProjectUrl(project.name),
github: getGithubUrl(project.name),
});
projectCard.append(projectImg, projectContent, projectLinks);
projectsArea.appendChild(projectCard);
});
}
function filterProjects(query) {
const filteredProjects = allProjects.filter(project =>
project.name.toLowerCase().includes(query.toLowerCase()) ||
project.description.toLowerCase().includes(query.toLowerCase())
);
renderProjectList(filteredProjects);
}
function fetchProjects() {
fetch("./data.json")
.then((res) => res.json())
.then((projects) => {
allProjects = projects;
renderProjectList(projects);
searchBar.addEventListener( "input", () => {
const query = searchBar.value;
if (query) {
filterProjects(query)
}
else {
renderProjectList(projects);
}
});
})
.catch((err) => {
console.log("Error fetching project data:", err);
});
}
fetchProjects();