-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
91 lines (86 loc) · 3.31 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
// Global object to store DOM elements
const domElements = {
root: document.querySelector('[data-gh="root"]'),
loading: document.querySelector('[data-gh="loading"]'),
jobFilter: document.querySelector('[data-gh="filter"]'),
placeHolder: document.querySelector('[data-gh="placeholder"]'),
sectionWrapper: document.querySelector('[data-gh="section-wrapper"]'),
};
const departmentIds = [];
// Fetch function we'll use to get data from the Greenhouse endpoints
const fetchData = (url) =>
fetch(url).then((response) => {
if (!response.ok) {
throw Error(` ${response.status} ${response.statusText}`);
}
return response.json();
});
// Filtering function for select element
domElements.jobFilter.onchange = () => {
const selectedSection = domElements.jobFilter.value;
const sections = document.querySelectorAll('[data-gh="section-wrapper"]');
sections.forEach((section) => {
section.style.display =
selectedSection === "all" || section.id === selectedSection
? "block"
: "none";
});
};
// When the DOM is ready, fetch the data and write it to the page
// Start by showing the loader and getting the department data
window.addEventListener("DOMContentLoaded", () => {
domElements.loading.classList.remove("hidden");
fetchData(`https://boards-api.greenhouse.io/v1/boards/${ghSlug}/departments/`)
.then((data) => {
data.departments.forEach((department) => {
if (department.jobs.length > 0) {
departmentIds.push(department.id);
let sectionClone = domElements.sectionWrapper.cloneNode(true);
sectionClone.id = department.id;
sectionClone.querySelector('[data-gh="section-heading"]').innerText =
department.name;
sectionClone
.querySelector('[data-gh="container"]')
.setAttribute("aria-label", department.name);
domElements.root.appendChild(sectionClone);
let option = new Option(department.name, department.id);
domElements.jobFilter.add(option);
}
});
})
.catch(console.error)
.finally(writeJobs);
});
// Triggered in finally above
// Writes all the jobs to the page
function writeJobs() {
departmentIds.forEach((departmentId) => {
fetchData(
`https://boards-api.greenhouse.io/v1/boards/${ghSlug}/departments/${departmentId}`
)
.then((data) => {
let parent = document.getElementById(data.id);
let parentContainer = parent.querySelector('[data-gh="container"]');
data.jobs.forEach((job) => {
let ghListing = parentContainer
.querySelector('[data-gh="listing"]')
.cloneNode(true);
ghListing.id = job.id;
let jobTitle = ghListing.querySelector('[data-gh="job-title"]');
jobTitle.innerText = job.title;
jobTitle.setAttribute("href", job.absolute_url);
ghListing.querySelector('[data-gh="job-location"]').innerText =
job.location.name;
parentContainer.appendChild(ghListing);
});
parentContainer.querySelector('[data-gh="listing"]').remove();
})
.catch(console.error)
.finally(() => {
domElements.loading.classList.add("hidden");
domElements.loading.remove();
domElements.placeHolder.remove();
domElements.root.classList.remove("hidden");
});
});
}