Created
March 21, 2023 14:55
-
-
Save rileyrichter/1ef9514bde523d8736cf6f761a7eeab5 to your computer and use it in GitHub Desktop.
Greenhouse code with location filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, merge, | |
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | |
to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or | |
substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | |
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
// Global variables defined here | |
const departmentIds = []; | |
const root = document.getElementById("root"); | |
const loading = document.getElementById("loading"); | |
const jobFilter = document.getElementById("filter"); | |
const locationFilter = document.getElementById("location-filter"); | |
const errorWrapper = document.getElementById("errwrapper"); | |
const errorText = document.getElementById("errtext"); | |
// Filtering function for location select element | |
locationFilter.onchange = function () { | |
const jobListings = document.querySelectorAll(".job-listing"); | |
let selectedLocation = this.value; | |
if (selectedLocation == "all") { | |
jobListings.forEach((jobListing) => { | |
jobListing.style.display = "flex"; | |
console.log("all jobs"); | |
}); | |
} else if (selectedLocation == "San Francisco") { | |
jobListings.forEach((jobListing) => { | |
let location = jobListing.querySelector(".job-location"); | |
if (location.textContent.includes("San Francisco")) { | |
jobListing.style.display = "flex"; | |
} else { | |
jobListing.style.display = "none"; | |
} | |
}); | |
} else if (selectedLocation == "US Remote") { | |
jobListings.forEach((jobListing) => { | |
let location = jobListing.querySelector(".job-location"); | |
if (location.textContent.includes("US Remote")) { | |
jobListing.style.display = "flex"; | |
} else { | |
jobListing.style.display = "none"; | |
} | |
}); | |
} else if (selectedLocation == "ON/BC, Canada") { | |
jobListings.forEach((jobListing) => { | |
let location = jobListing.querySelector(".job-location"); | |
if (location.textContent.includes("ON/BC, Canada")) { | |
jobListing.style.display = "flex"; | |
} else { | |
jobListing.style.display = "none"; | |
} | |
}); | |
} | |
}; | |
// Filtering function for select element | |
jobFilter.onchange = function () { | |
let selectedSection = this.value; | |
if (selectedSection == "all") { | |
let filtered = document.querySelectorAll(".department-section"); | |
filtered.forEach((filtered) => { | |
filtered.style.display = "block"; | |
}); | |
} else { | |
let filtered = document.querySelectorAll(".department-section"); | |
filtered.forEach((filtered) => { | |
filtered.style.display = "none"; | |
}); | |
document.getElementById(selectedSection).style.display = "block"; | |
} | |
}; | |
// Triggers when the DOM is ready | |
window.addEventListener("DOMContentLoaded", (event) => { | |
const handleError = (response) => { | |
if (!response.ok) { | |
throw Error(` ${response.status} ${response.statusText}`); | |
} else { | |
return response.json(); | |
} | |
}; | |
fetch("https://boards-api.greenhouse.io/v1/boards/" + ghSlug + "/departments/") | |
.then(handleError) | |
.then((data) => { | |
data.departments.forEach((department) => { | |
if (department.jobs.length !== 0) { | |
departmentIds.push(department.id); | |
let sectionWrapper = document.getElementById("section"); | |
let sectionClone = sectionWrapper.cloneNode(true); | |
sectionClone.id = department.id; | |
root.appendChild(sectionClone); | |
let option = document.createElement("option"); | |
option.text = department.name; | |
option.value = department.id; | |
jobFilter.add(option); | |
} else { | |
null; | |
} | |
}); | |
}) | |
.catch(function writeError(err) { | |
console.error(err); | |
}) | |
.finally(() => { | |
writeJobs(); | |
}); | |
}); | |
// Triggered in finally above | |
function writeJobs() { | |
departmentIds.forEach((departmentId) => { | |
const handleError = (response) => { | |
if (!response.ok) { | |
throw Error(` ${response.status} ${response.statusText}`); | |
} else { | |
return response.json(); | |
} | |
}; | |
fetch("https://boards-api.greenhouse.io/v1/boards/" + ghSlug + "/departments/" + departmentId) | |
.then(handleError) | |
.then((data) => { | |
let parent = document.getElementById(data.id); | |
let parentContainer = parent.getElementsByClassName("container")[0]; | |
let sectionHeading = document.getElementById("dname"); | |
let sectionTitle = sectionHeading.cloneNode(true); | |
sectionTitle.innerText = data.name; | |
parentContainer.appendChild(sectionTitle); | |
data.jobs.forEach((job) => { | |
let listing = document.getElementById("listing"); | |
let ghListing = listing.cloneNode(true); | |
ghListing.id = job.id; | |
let jobTitle = ghListing.getElementsByClassName("job-title")[0]; | |
jobTitle.innerText = job.title; | |
jobTitle.setAttribute("href", job.absolute_url); | |
let jobLocation = ghListing.getElementsByClassName("job-location")[0]; | |
jobLocation.innerText = job.location.name; | |
parentContainer.appendChild(ghListing); | |
}); | |
}) | |
.catch(function writeError(err) { | |
console.error(err); | |
}) | |
.finally(() => { | |
loading.classList.add("invisible"); | |
loading.remove(); | |
root.classList.add("visible"); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment