-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscripts.js
152 lines (132 loc) · 4.67 KB
/
scripts.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
var apiData = '';
const app = document.getElementById('root');
const container1 = document.createElement('div');
container1.setAttribute('class', 'container1');
app.appendChild(container1);
//GET REQUEST WITH USE OF HEROKU AS A PROXY TO SOLVE CORS ERROR
var request = new XMLHttpRequest();
request.open(
'GET',
'https://cors-anywhere.herokuapp.com/https://jobs.github.com/positions.json?&markdown=true&location=united+states&page=1&count=20',
true
);
request.onload = function() {
// CONVERT JSON DATA TO JAVASCRIPT OBJECTS USING JSON.PARSE
var apiData = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
apiData.forEach(job => {
const card = document.createElement('a');
card.setAttribute('class', 'card');
//RENDER RELEVANT DATA - JOB TYPE, LOCATION, DESCRIPTION
card.href = job.url;
const h2 = document.createElement('h2');
job.title = job.title.substring(0, 40);
h2.textContent = `${job.title}...`;
const h3 = document.createElement('h3');
h3.textContent = job.company;
const h4 = document.createElement('h4');
h4.textContent = job.type;
const h5 = document.createElement('h5');
h5.textContent = job.location;
const p = document.createElement('p');
job.description = job.description.substring(0, 300);
p.textContent = `${job.description}...`;
container1.appendChild(card);
card.appendChild(h2);
card.appendChild(h3);
card.appendChild(h4);
card.appendChild(h5);
card.appendChild(p);
});
// ERROR HANDLING
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = `It's not working!`;
app.appendChild(errorMessage);
}
};
// FILTER BY JOB TITLE
var button1 = document.getElementById('keywordSearchButton');
var userInput1 = document.getElementById('userInput1');
button1.addEventListener('click', showResults1);
userInput1.addEventListener('keyup', function(e) {
if (e.keyCode === 13) {
showResults();
}
});
function showResults1() {
var searchKeyword = userInput1.value.toLowerCase();
var cards = document.getElementsByClassName('card'); //this is an array
var regex = new RegExp(searchKeyword, 'g');
for (var i = 0; i < cards.length; i++) {
if (cards[i].textContent.toLowerCase().match(regex)) {
cards[i].style.display = 'block';
} else {
cards[i].style.display = 'none';
}
}
}
// FILTER BY JOB LOCATION
var button2 = document.getElementById('locationSearchButton');
var userInput2 = document.getElementById('userInput2');
button2.addEventListener('click', showResults2);
userInput2.addEventListener('keyup', function(e) {
if (e.keyCode === 13) {
showResults();
}
});
function showResults2() {
var searchKeyword = userInput2.value.toLowerCase();
var cards = document.getElementsByClassName('card');
var jobLocations = document.getElementsByTagName('h5');
var regex = new RegExp(searchKeyword, 'g');
for (var i = 0; i < cards.length; i++) {
if (jobLocations[i].textContent.toLowerCase().match(regex)) {
cards[i].style.display = 'block';
} else {
cards[i].style.display = 'none';
}
}
}
// FILTER BY JOB TYPE
let fullTimeCheckBox = document.getElementById('cbFT');
let partTimeCheckBox = document.getElementById('cbPT');
let contractCheckBox = document.getElementById('cbCT');
let jobType = document.getElementsByTagName('h4');
fullTimeCheckBox.addEventListener('change', showByJobType);
partTimeCheckBox.addEventListener('change', showByJobType);
contractCheckBox.addEventListener('change', showByJobType);
function showByJobType() {
let cards = document.getElementsByClassName('card');
for (var i = 0; i < cards.length; i++) {
if (fullTimeCheckBox.checked === false) {
if (jobType[i].textContent.toLowerCase() == 'full time') {
cards[i].style.display = 'none';
}
} else if (fullTimeCheckBox.checked) {
if (jobType[i].textContent.toLowerCase() == 'full time') {
cards[i].style.display = 'block';
}
}
if (partTimeCheckBox.checked === false) {
if (jobType[i].textContent.toLowerCase() == 'part time') {
cards[i].style.display = 'none';
}
} else if (partTimeCheckBox.checked) {
if (jobType[i].textContent.toLowerCase() == 'part time') {
cards[i].style.display = 'block';
}
}
if (contractCheckBox.checked === false) {
if (jobType[i].textContent.toLowerCase() == 'contract') {
cards[i].style.display = 'none';
}
} else if (contractCheckBox.checked) {
if (jobType[i].textContent.toLowerCase() == 'contract') {
cards[i].style.display = 'block';
}
}
}
}
request.send();