From 675e29fec46cee3cb9c21a3bdeb58d04eaee1673 Mon Sep 17 00:00:00 2001 From: aldemirLucas Date: Wed, 6 Nov 2024 11:27:59 -0300 Subject: [PATCH] fix: separation of spatial_coverage feature into Prod and Staging --- next/components/organisms/DatasetResource.js | 83 ++++++++++++------- .../components/organisms/DatasetSearchCard.js | 7 +- next/components/organisms/TablePage.js | 45 +++++----- next/pages/api/areas/getCountries.js | 4 - next/pages/dataset/[dataset].js | 68 ++++++++------- next/pages/search.js | 4 +- next/public/locales/en/dataset.json | 1 + next/public/locales/es/dataset.json | 1 + next/public/locales/pt/dataset.json | 1 + 9 files changed, 130 insertions(+), 84 deletions(-) diff --git a/next/components/organisms/DatasetResource.js b/next/components/organisms/DatasetResource.js index 9bd37577..126d78b8 100644 --- a/next/components/organisms/DatasetResource.js +++ b/next/components/organisms/DatasetResource.js @@ -27,12 +27,12 @@ import ChevronIcon from "../../public/img/icons/chevronIcon"; export default function DatasetResource({ dataset }) { + const { t } = useTranslation('dataset'); const router = useRouter() const { query, locale } = router const [tables, setTables] = useState([]) const [rawDataSources, setRawDataSources] = useState([]) const [informationRequests, setInformationRequests] = useState([]) - const { t } = useTranslation('dataset'); const displayScreen = useBreakpointValue({ base: "mobile", lg: "desktop" }) const pushQuery = (key, value) => { @@ -55,7 +55,6 @@ export default function DatasetResource({ } useEffect(() => { - let dataset_tables = dataset?.tables?.edges.map((elm) => elm.node) .filter((elm) => elm?.status?.slug !== "under_review") .filter((elm) => elm?.slug !== "dicionario") @@ -194,10 +193,24 @@ export default function DatasetResource({ ) } - function SelectResource() { - const [widthScreen, setWidthScreen] = useState(0) + function SelectResource ({ selectedResource }) { + const { t } = useTranslation('dataset'); + const [widthScreen, setWidthScreen] = useState(0); + const [value, setValue] = useState("") + const {table, raw_data_source, information_request} = selectedResource; + + const findResourceName = (source, id) => { + const resource = source.find(item => item._id === id); + return resource ? resource[`name${capitalize(locale)}`] || resource.name : ""; + }; useEffect(() => { + setValue( + table ? findResourceName(tables, table) : + raw_data_source ? findResourceName(rawDataSources, raw_data_source) : + information_request ? findResourceName(informationRequests, information_request) : "" + ); + const updateWidthScreen = () => { setWidthScreen(window.innerWidth - 48) } @@ -209,35 +222,49 @@ export default function DatasetResource({ return () => { window.removeEventListener('resize', updateWidthScreen) } - }, []) + }, [table, raw_data_source, information_request]); return ( - - - Selecione uma tabela ou fonte original - - - + + {value} + + + + : - + } diff --git a/next/components/organisms/DatasetSearchCard.js b/next/components/organisms/DatasetSearchCard.js index 51c41ecd..c9a0abec 100644 --- a/next/components/organisms/DatasetSearchCard.js +++ b/next/components/organisms/DatasetSearchCard.js @@ -33,7 +33,12 @@ export default function Dataset({ const { t } = useTranslation('dataset'); const [spatialCoverageNames, setSpatialCoverageNames] = useState([]); + const allowedURLs = ["https://basedosdados.org", "https://staging.basedosdados.org"] + + useEffect(() => { + if(allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND)) return + const fetchAreaNames = async () => { if (!spatialCoverage) return; @@ -299,7 +304,7 @@ export default function Dataset({ - {process.env.NEXT_PUBLIC_BASE_URL_FRONTEND !== "https://basedosdados.org" && + {!allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND) && { const fetchData = async () => { try { @@ -45,28 +47,31 @@ export default function TablePage({ id }) { const result = await response.json() if (result.success) { - let areaNames = []; - - if (result.resource?.spatialCoverage) { - const coverageArray = Array.isArray(result.resource.spatialCoverage) - ? result.resource.spatialCoverage - : typeof result.resource.spatialCoverage === 'string' - ? result.resource.spatialCoverage.split(',').map(item => item.trim()) - : Object.values(result.resource.spatialCoverage); - - const promises = coverageArray.map(slug => - axios.get(`/api/areas/getArea?slug=${slug}&locale=${locale}`) - ); - - const responses = await Promise.all(promises); - areaNames = responses - .map(res => res.data.resource[0]?.node[`name${capitalize(locale)}`] || res.data.resource[0]?.node.name) - .filter(Boolean) - .sort((a, b) => a.localeCompare(b, locale)); + if(!allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND)) { + let areaNames = []; + + if (result.resource?.spatialCoverage) { + const coverageArray = Array.isArray(result.resource.spatialCoverage) + ? result.resource.spatialCoverage + : typeof result.resource.spatialCoverage === 'string' + ? result.resource.spatialCoverage.split(',').map(item => item.trim()) + : Object.values(result.resource.spatialCoverage); + + const promises = coverageArray.map(slug => + axios.get(`/api/areas/getArea?slug=${slug}&locale=${locale}`) + ); + + const responses = await Promise.all(promises); + areaNames = responses + .map(res => res.data.resource[0]?.node[`name${capitalize(locale)}`] || res.data.resource[0]?.node.name) + .filter(Boolean) + .sort((a, b) => a.localeCompare(b, locale)); + } + + setSpatialCoverageNames(areaNames); } setResource(result.resource); - setSpatialCoverageNames(areaNames); setIsError(false); } else { console.error(result.error) @@ -346,7 +351,7 @@ export default function TablePage({ id }) { - {process.env.NEXT_PUBLIC_BASE_URL_FRONTEND !== "https://basedosdados.org" && + {!allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND) && item.trim()) - : Object.values(dataset.spatialCoverage); - const promises = coverageArray.map(slug => { - const url = `${process.env.NEXT_PUBLIC_BASE_URL_FRONTEND}/api/areas/getArea?slug=${slug}&locale=${locale}`; - return axios.get(url).catch(error => { - console.error(`Error fetching ${url}:`, error.message); - return null; + if(!allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND)) { + if (dataset?.spatialCoverage) { + const coverageArray = Array.isArray(dataset.spatialCoverage) + ? dataset.spatialCoverage + : typeof dataset.spatialCoverage === 'string' + ? dataset.spatialCoverage.split(',').map(item => item.trim()) + : Object.values(dataset.spatialCoverage); + + const promises = coverageArray.map(slug => { + const url = `${process.env.NEXT_PUBLIC_BASE_URL_FRONTEND}/api/areas/getArea?slug=${slug}&locale=${locale}`; + return axios.get(url).catch(error => { + console.error(`Error fetching ${url}:`, error.message); + return null; + }); }); - }); - - const responses = await Promise.all(promises); - spatialCoverageNames = responses - .filter(res => res !== null) - .map(res => { - const name = res.data.resource[0]?.node[`name${capitalize(locale)}`] || res.data.resource[0]?.node.name; - return name; - }) - .filter(Boolean) - .sort((a, b) => a.localeCompare(b, locale)); + + const responses = await Promise.all(promises); + spatialCoverageNames = responses + .filter(res => res !== null) + .map(res => { + const name = res.data.resource[0]?.node[`name${capitalize(locale)}`] || res.data.resource[0]?.node.name; + return name; + }) + .filter(Boolean) + .sort((a, b) => a.localeCompare(b, locale)); + } } } catch (error) { console.error("Fetch error:", error.message); } + const props = { + ...(await serverSideTranslations(locale, ['dataset', 'common', 'menu', 'prices'])), + dataset, + ...(allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND) ? {} : { spatialCoverageNames }), + }; + return { - props: { - ...(await serverSideTranslations(locale, ['dataset', 'common', 'menu', 'prices'])), - dataset, - spatialCoverageNames, - }, + props, revalidate: 30, }; } @@ -91,13 +96,16 @@ export async function getStaticPaths(context) { } } -export default function DatasetPage ({ dataset, spatialCoverageNames }) { +export default function DatasetPage ({ dataset, spatialCoverageNames = [] }) { const { t } = useTranslation('dataset', 'common'); const router = useRouter() const { locale } = router const { query } = router const [tabIndex, setTabIndex] = useState(0) + const allowedURLs = ["https://basedosdados.org", "https://staging.basedosdados.org"] + + const isDatasetEmpty = dataset === null || Object.keys(dataset).length === 0 if(isDatasetEmpty) return @@ -236,7 +244,7 @@ export default function DatasetPage ({ dataset, spatialCoverageNames }) { - {process.env.NEXT_PUBLIC_BASE_URL_FRONTEND !== "https://basedosdados.org" && + {!allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND) && - {process.env.NEXT_PUBLIC_BASE_URL_FRONTEND !== "https://basedosdados.org" ? + {!allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND) ? <>