Latest solutions
Vue 3, Typescript, Pinia for state management and plain old css for UI
#pinia#sass/scss#typescript#vueSubmitted over 2 years ago
Latest comments
- @NicoDeLaFuente@lloydjohnlandeza
One solution to make the border links work, is by fetching the official name of the border country from the API. You can use the following API endpoint to retrieve the official name based on the border code you pass:
https://restcountries.com/v3.1/alpha/${border}?fields=name
Since you already have your BorderButton component in a separate file, you can perform the fetch operation there. Here's an example implementation:
import { Link } from "react-router-dom" import { useEffect, useState } from "react"; const BorderButton = ({border, borderCountryHandler}) => { const [borderName, setBorderName] = useState('') useEffect(() => { fetchName() }, []) const fetchName = () => { const fetching = fetch(`https://restcountries.com/v3.1/alpha/${border}?fields=name`) fetching.then( res => res.json()) .then(data => setBorderName(data?.name?.official)) } return <Link to={`/detail/${borderName}`} className="btn-border btn" id={border}>{border}</Link> } export default BorderButton
I hope this clarification helps you resolve the issue with the border links in the country detail view. Let me know if you have any further questions!
Marked as helpful - @notMoustee@lloydjohnlandeza
Nice pure javascript solution! It's nostalgic to see a javascript code without any framework/libraries.
Anyway to fix your issue on the border toggle you should check if the body has a class "dark-mode" before generating the html template for borderCountries.
The solution for it is below but I suggest you try doing it first before checking it.
- Spoiler Alert
- Spoiler Alert
- Spoiler Alert
- Spoiler Alert
const isInDarkMode = document.body.classList.contains('dark-mode') const borderDarkMode = isInDarkMode ? 'border-dark' : '' borderCountries = borders.length > 0 ? borders.map((border) => `<span class="border ${borderDarkMode}">${border}</span>`).join('') : 'None'
Marked as helpful - P@nityagulati@lloydjohnlandeza
Well done doing this challenged! Regarding the issue you mentioned with losing route params on refreshing the Country Info page, it's actually a server-side problem with SPA (Single Page Applications) and not directly related to your code. Since you're using GitHub Pages (github.io), your options for resolving this issue are somewhat limited.
The first and easiest option is to change your router's history mode to use web hash. You can achieve this by modifying your router setup as follows:
const router = createRouter({ mode: 'history', history: createWebHashHistory(), // previous value: createWebHistory() routes, });
By adding a hash at the beginning of your URLs, this approach ensures that your SPA always loads correctly.
The second option is more complex, requiring you to update your 404.html file to load the code from index.html. This way, even when encountering a 404 error, your SPA's code will still be loaded properly.
The third option involves using Netlify instead of GitHub Pages (don't worry netlify is free as well). With Netlify, you can easily resolve this issue by utilizing redirects. You can find more information about using redirects with Netlify here.
- @ToenSh@lloydjohnlandeza
Nice solution! I solved the limiting of upvote/downvote by adding an upVoted and downVoted boolean flag to the comment object. You can check those flag before changing the score. But you can make it a step further by making that boolean into an array of username that upVoted or downVoted the comment to make it work with multiple users!