forked from JasonDsouza212/free-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
129 lines (117 loc) · 4.58 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
const inputBar = document.querySelector('.input')
const results = document.querySelector('.results')
// const themeButton = document.querySelector('#theme-btn')
const categoryButton = document.querySelector("#category-btn")
const categoryList = document.querySelector('.category-list')
let websites;
// themeButton.addEventListener('click',changeTheme)
function result(productName,category,image,link,description){
//result element to add html form of the website
let ele = document.createElement('article')
ele.classList.add('result')
ele.addEventListener('click',(e)=>{
if(ele.classList.contains('expand')){
ele.classList.remove('expand')
}else{
Array.from(document.getElementsByClassName('result')).forEach((i)=>{
i.classList.remove('expand')
})
ele.classList.add('expand')
}
})
ele.innerHTML = `
<div class="result-header">
<div class="websiteLogo">
<img src=${image} alt="logo" />
</div>
<div class="result-info">
<span class="websiteName">${productName}</span>
<div class="tags">
<span class="tag">${category}</span>
</div>
</div>
<div class="header-side-icon">
<i class="fa-solid fa-angle-down"></i>
</div>
</div>
<div class="result-body">
<a class="side-link" href=${link}>
<i class="fa-solid fa-link"></i>
visit
</a>
<p class="description">${description}</p>
</div>
`
return ele
}
function changeTheme(e){
e.target.innerHTML = ""
if(document.querySelector('body').classList.contains('dark')){
document.querySelector('body').classList.remove('dark')
e.target.innerHTML = `<i class="fa-solid fa-sun"></i>`
}else{
document.querySelector('body').classList.add('dark')
e.target.innerHTML = `<i class="fa-solid fa-moon"></i>`
}
}
function filterByCategory(e){
Array.from(this.querySelectorAll('li')).forEach(ele=>ele.classList.remove('active'))
e.target.classList.add('active')
results.innerHTML = ''
websites.filter(ele=>ele.category.toLowerCase()==e.target.innerText.toLowerCase()).forEach((element)=>{
results.appendChild(result(element.productName,element.category,element.image,element.link,element.description))
})
}
async function fetchWebsites(results, categoryList) {
try{
var websites;
const response = await fetch('DB/product.json', {credentials: 'omit'})
const data = await response.json()
const categories = new Set()
data.forEach(element => {
categories.add(element.category);
results.appendChild(result(element.productName, element.category, element.image, element.link, element.description));
});
categories.forEach(element => {
categoryList.innerHTML += `<li>${element}</li>`;
});
websites = data
return websites
}catch(err){
console.error(err)
}
}
function handleChange(e,websites){
//function for input change handling
const ele = e.target.value.toLowerCase()
filtered_websites = websites.filter((i)=>(
i.productName.toLowerCase().includes(ele)||i.category.toLowerCase().includes(ele)||i.description.toLowerCase().includes(ele)
))
const results = document.querySelector('.results')
if(filtered_websites.length==0){
results.innerHTML = `<h1 class="info-heading">No website with given keyword is found</h1>`
}
else{
results.innerHTML = ""
filtered_websites.forEach(element => {
results.appendChild(result(element.productName,element.category,element.image,element.link,element.description))
});
}
}
function showCategories(e,categoryList){
if(categoryList.classList.contains('active')){
document.querySelector('body').classList.remove('expand')
categoryList.classList.remove('active')
}else{
document.querySelector('body').classList.add('expand')
categoryList.classList.add('active')
}
}
async function main(){
websites = await fetchWebsites(results,categoryList)
inputBar.addEventListener('keydown',(e)=>handleChange(e,websites))
inputBar.addEventListener('keyup',(e)=>handleChange(e,websites))
categoryButton.addEventListener('click',(e)=>showCategories(e,categoryList))
categoryList.addEventListener('click',filterByCategory)
}
main()