Skip to content

Instantly share code, notes, and snippets.

@skflowne
Last active August 18, 2020 13:57
Show Gist options
  • Save skflowne/484f277853adb184f5950779605fbe2b to your computer and use it in GitHub Desktop.
Save skflowne/484f277853adb184f5950779605fbe2b to your computer and use it in GitHub Desktop.
import { selector, selectorFamily } from "recoil"
export const API_DATE_FORMAT = "yyyy-MM-dd"
export const countriesQuery = selector({
key: "countries",
get: async () => {
try {
const res = await fetch("https://covid19-api.org/api/countries")
const countries = await res.json()
return countries.reduce((dict, country) => {
dict[country.alpha2] = country
return dict
}, {})
} catch (e) {
console.error("ERROR GET /countries", e)
}
},
})
export const statusByDateQuery = selectorFamily({
key: "status-by-date",
get: (formattedDate) => async ({ get }) => {
try {
const res = await fetch(`https://covid19-api.org/api/status?date=${formattedDate}`)
const status = await res.json()
return status
} catch (e) {
console.error("ERROR GET /statusByDate", e)
}
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment