1
0
mirror of https://github.com/kremalicious/umami.git synced 2024-12-22 09:13:37 +01:00
umami/hooks/useCountryNames.js

35 lines
770 B
JavaScript
Raw Normal View History

2020-10-01 01:27:27 +02:00
import { useState, useEffect } from 'react';
2020-10-01 07:34:16 +02:00
import { useRouter } from 'next/router';
2020-10-01 01:27:27 +02:00
import { get } from 'lib/web';
import enUS from 'public/country/en-US.json';
const countryNames = {
'en-US': enUS,
};
export default function useCountryNames(locale) {
const [list, setList] = useState(countryNames[locale] || enUS);
2020-10-01 07:34:16 +02:00
const { basePath } = useRouter();
2020-10-01 01:27:27 +02:00
async function loadData(locale) {
2020-10-01 07:34:16 +02:00
const { ok, data } = await get(`${basePath}/country/${locale}.json`);
2020-10-01 01:27:27 +02:00
if (ok) {
countryNames[locale] = data;
setList(countryNames[locale]);
} else {
setList(enUS);
}
}
useEffect(() => {
if (!countryNames[locale]) {
loadData(locale);
} else {
setList(countryNames[locale]);
}
}, [locale]);
return list;
}