forked from ishandeveloper/Coursify-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
117 lines (92 loc) · 3.15 KB
/
main.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
const allCourseTags = data ? [...data.reduce((acc, course) => (acc = [...acc, ...course.tags]), [])] : [];
const uniqueCourseTags = Array.from(new Set(allCourseTags));
// Course per page
const perPage = 9;
// determine if we're filtering by tag
const courseTagFilterVal = getParameterByName('courseTag');
const isValidTag = uniqueCourseTags.includes(courseTagFilterVal);
const coursesFilteredByTag = isValidTag ?
data.filter(
(course) => course.tags && course.tags.includes(courseTagFilterVal)
) :
data;
// If we have a valid tag, display a message to clear it
if (isValidTag) {
const message = `<h3 class="filter__message">
Filtering by tag <span class="filter__message--tagname">${courseTagFilterVal}</span> —
<a class="filter__clear" href="index.html">✗ Clear</a></h3>`;
$(".courses__list").append(message);
}
// Get current page number from URL
let currentPage = getParameterByName('page');
currentPage = currentPage ? currentPage : 1;
// Display filtered courses with pagination
display(currentPage, coursesFilteredByTag);
pagination(currentPage, coursesFilteredByTag);
/**
* Display Provided course for particular page
*
* @param number page
* @param Array data
*/
function display(page = 1, data = []) {
let limit = data.length;
const totalPages = Math.ceil(limit / perPage);
// Check if page is in bounds
if (page > totalPages || page <= 0) page = 1;
let offset = (page - 1) * perPage;
let i = offset;
while (i < limit && i < offset + perPage) {
const course = data[i];
let html = `<div class="courses__card">
<img
src="./courses/images/${course.image}"
alt="Course Image"
class="course__image"
onerror="this.onerror=null; this.src='./assets/images/default.png'"
/>
<div class="course__info">
<div class="course__tags">
${course.tags
.map((tag) => `<a href="?courseTag=${tag}"><div class="course__tag">${tag}</div></a>`)
.join("")}
</div>
<div class="course__name">${course.name}</div>
<div class="course__instructor">${course.instructor}</div>
<div class="course__description">
${course.description.substring(0, 100)}..
</div>
<a target="_blank" class="course__call_to_action" href="${course.url}"> Learn More </a>
</div>
</div>`;
$(".courses__list").append(html);
i++;
}
}
/**
* Create pagination links
*
* @param number page
* @param Array data
*/
function pagination(page = 1, data = []) {
const totalCourses = data.length;
const totalPages = Math.ceil(totalCourses / perPage);
// Check if page is in bounds
if (page > totalPages || page <= 0) page = 1;
let paginationHTML = '';
for (let i = 1; i <= totalPages; i++) {
let activeClass = '';
if (i == page) activeClass = "active";
paginationHTML += `<a href="#" class="pagination__link ${activeClass}" data-page="${i}">${i}</a>`;
}
$('.pagination').html(paginationHTML);
}
// Handle Pagination click event
$('.pagination .pagination__link').on('click', function (e) {
e.preventDefault();
const page = $(this).data('page');
const urlParams = new URLSearchParams(window.location.search);
urlParams.set('page', page);
window.location.search = urlParams;
});