-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CountryLookup Project Submission #13
Comments
Looks great and well thought out. Feel free to get started. |
How are things so far? Any updates to share? |
It's pretty much done & deployed now: https://countrylookup.now.sh/ |
Would you like a UI review or a code review? |
Mainly a code review, as I've not done Vanilla JS in a long time now. |
UI Review
|
Code Review ChecklistREADME
HTML
JavaScript
CSS
RefactorCould possibly refactor const propertyNames = ["name", "capital", "region", "population", "demonym", "nativeName", "numericCode", "topLevelDomain"];
// This contains results
const resultContainer = {
self: sel("#result-container"),
children: {
name: sel("#result-name"),
flag: sel("#result-flag"),
topLevelDomain: {
self: sel("#result-tlds"),
transform: (value) => value.join(", ") || ""
},
codes: sel("#result-codes"),
callc: sel("#result-callc"),
capital: sel("#result-capi"),
aka: sel("#result-aka"),
region: sel("#result-regio"),
population: sel("#result-pop"),
demonym: sel("#result-demonym"),
time: sel("#result-time"),
border: sel("#result-border"),
nativeName: sel("#result-native"),
numericCode: sel("#result-numeric"),
lang: sel("#result-lang")
}
}
async function getResult(query) {
try {
const res = await fetch("https://restcountries.eu/rest/v2/name/" + String(query))
const body = await res.json()
return body
} catch (error) {
error.title = "Failed to parse response"
error.description = "Please make sure your query is valid."
throw error
}
}
/**
* initializes a search
* @param {String} query
* @returns {void}
*/
const initializeSearch = async (query) => {
try {
resultContainer.self.style.display = "none"
errorContainer.self.style.display = "none"
if (!query) {
const queryError = new Error()
queryError.title = "Searches cannot be empty"
queryError.description = "Fun fact: This tool supports about 240 countries."
throw queryError;
}
const body = await getResult(query)
// WARNING: The following code is not great...
const country = body[0]
if (!country) {
const notFoundError = new Error()
notFoundError.title = "Not found"
notFoundError.description = "No results were found to your query."
throw notFoundError
}
console.log(country);
resultContainer.self.style.display = "inherit"
propertyNames.forEach(property => {
const item = resultContainer.children[property]
if (item.self) {
item.self.innerText = item.transform(country[property])
} else {
item.innerText = country[property]
}
});
// header
resultContainer.children.flag.setAttribute("src", country.flag)
// column 1
resultContainer.children.codes.innerText = `2-Char: ${country.alpha2Code} / 3-Char: ${country.alpha3Code}`
resultContainer.children.callc.innerText = country.callingCodes.map(x => "+" + x).join(", ") || ""
// column 2
resultContainer.children.aka.innerText = country.altSpellings.join(", ") || ""
// column 3
// just 2 sections, as those can get long already
resultContainer.children.time.innerText = "UTC: " + country.timezones
.map(x =>
x // remove unneeded info
.replace("UTC", "")
.replace(":00", "")
).join(", ")
|| ""
resultContainer.children.border.innerText = country.borders.join(", ") || "<nobody>"
// column 4
let languages = []
country.languages.forEach(language => {
languages.push(language.name)
})
resultContainer.children.lang.innerText = languages.join(", ") || ""
} catch (e) {
error(e.title, e.description);
}
} |
Project Name / Title
CountryLookup
Your Name / Title
Infi
Project Description
CountryLookup helps you to find the most important information about a country without reading an entire wall of text. Among others, you quickly get things like languages, currencies, calling codes, and timezones.
What 3rd Party Web API do you plan to use?
REST Countries
Which of the following describes you:
The text was updated successfully, but these errors were encountered: