From 376c9e67d12f034ffe2f13bd484c70b7c4b63ac0 Mon Sep 17 00:00:00 2001 From: Ricardo Dahis Date: Thu, 7 Nov 2024 20:16:10 +1100 Subject: [PATCH 1/4] fix: remove calls to getArea --- .../components/organisms/DatasetSearchCard.js | 42 ++----------------- next/components/organisms/TablePage.js | 33 +++------------ next/pages/api/datasets/getDataset.js | 3 +- next/pages/api/tables/getTable.js | 1 + next/pages/api/tables/getTableColumns.js | 3 +- next/pages/dataset/[dataset].js | 37 +++------------- next/pages/search.js | 6 ++- 7 files changed, 22 insertions(+), 103 deletions(-) diff --git a/next/components/organisms/DatasetSearchCard.js b/next/components/organisms/DatasetSearchCard.js index c9a0abec..7acd9796 100644 --- a/next/components/organisms/DatasetSearchCard.js +++ b/next/components/organisms/DatasetSearchCard.js @@ -21,9 +21,9 @@ import { useState, useEffect } from "react"; export default function Dataset({ id, name, - spatialCoverage, - temporalCoverageText, organizations, + temporalCoverageText, + spatialCoverage, tables, rawDataSources, informationRequests, @@ -31,42 +31,8 @@ export default function Dataset({ locale }) { 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; - - const coverageArray = Array.isArray(spatialCoverage) - ? spatialCoverage - : typeof spatialCoverage === 'string' - ? spatialCoverage.split(',').map(item => item.trim()) - : Object.values(spatialCoverage); - - const promises = coverageArray.map(slug => - axios.get(`/api/areas/getArea?slug=${slug}&locale=${locale}`) - ); - - try { - const responses = await Promise.all(promises); - const 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); - } catch (error) { - console.error('Error fetching area names:', error); - } - }; - - fetchAreaNames(); - }, [spatialCoverage, locale]); - const Tables = () => { let tablesNumber = tables.number if(tables.number === undefined) tablesNumber = 0 @@ -325,9 +291,7 @@ export default function Dataset({ lineHeight="20px" color="#71757A" > - {spatialCoverageNames.length > 0 - ? spatialCoverageNames.join(', ') - : spatialCoverage ? spatialCoverage : t('notProvided')} + {spatialCoverage ? spatialCoverage : t('notProvided')} } diff --git a/next/components/organisms/TablePage.js b/next/components/organisms/TablePage.js index 2fa99eb4..42687473 100644 --- a/next/components/organisms/TablePage.js +++ b/next/components/organisms/TablePage.js @@ -36,7 +36,6 @@ export default function TablePage({ id }) { const [isLoading, setIsLoading] = useState(true) const [resource, setResource] = useState({}) const [isError, setIsError] = useState(false) - const [spatialCoverageNames, setSpatialCoverageNames] = useState([]); const allowedURLs = ["https://basedosdados.org", "https://staging.basedosdados.org"] @@ -47,30 +46,6 @@ export default function TablePage({ id }) { const result = await response.json() if (result.success) { - 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); setIsError(false); } else { @@ -367,7 +342,7 @@ export default function TablePage({ id }) { - {spatialCoverageNames.length > 0 - ? spatialCoverageNames.join(', ') + {resource?.[`spatialCoverageName${capitalize(locale)}`] + ? Object.values(resource[`spatialCoverageName${capitalize(locale)}`]) + .sort((a, b) => a.localeCompare(b, locale)) + .join(', ') : t('table.notProvided')} diff --git a/next/pages/api/datasets/getDataset.js b/next/pages/api/datasets/getDataset.js index a3dd182c..482ca97a 100644 --- a/next/pages/api/datasets/getDataset.js +++ b/next/pages/api/datasets/getDataset.js @@ -20,8 +20,9 @@ export default async function getDataset(id, locale = 'pt') { name${capitalize(locale)} description description${capitalize(locale)} - spatialCoverage temporalCoverage + spatialCoverage + spatialCoverageName${capitalize(locale)} themes { edges { node { diff --git a/next/pages/api/tables/getTable.js b/next/pages/api/tables/getTable.js index ead6eb75..e012a7a1 100644 --- a/next/pages/api/tables/getTable.js +++ b/next/pages/api/tables/getTable.js @@ -53,6 +53,7 @@ async function getTable(id, locale='pt') { isDeprecated temporalCoverage spatialCoverage + spatialCoverageName${capitalize(locale)} fullTemporalCoverage rawDataSource { edges { diff --git a/next/pages/api/tables/getTableColumns.js b/next/pages/api/tables/getTableColumns.js index 0cf44eae..39b076b5 100644 --- a/next/pages/api/tables/getTableColumns.js +++ b/next/pages/api/tables/getTableColumns.js @@ -55,8 +55,9 @@ async function getTableColumns(id, locale = 'pt') { bigqueryType { name } - spatialCoverage temporalCoverage + spatialCoverage + spatialCoverageName${capitalize(locale)} measurementUnit containsSensitiveData observations diff --git a/next/pages/dataset/[dataset].js b/next/pages/dataset/[dataset].js index eb941706..4b63ebb8 100644 --- a/next/pages/dataset/[dataset].js +++ b/next/pages/dataset/[dataset].js @@ -36,39 +36,11 @@ import { export async function getStaticProps(context) { const { locale, params } = context; let dataset = null; - let spatialCoverageNames = []; const allowedURLs = ["https://basedosdados.org", "https://staging.basedosdados.org"] try { dataset = await getDataset(params.dataset, locale || 'pt'); - 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)); - } - } } catch (error) { console.error("Fetch error:", error.message); } @@ -76,7 +48,6 @@ export async function getStaticProps(context) { const props = { ...(await serverSideTranslations(locale, ['dataset', 'common', 'menu', 'prices'])), dataset, - ...(allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND) ? {} : { spatialCoverageNames }), }; return { @@ -96,7 +67,7 @@ export async function getStaticPaths(context) { } } -export default function DatasetPage ({ dataset, spatialCoverageNames = [] }) { +export default function DatasetPage ({ dataset }) { const { t } = useTranslation('dataset', 'common'); const router = useRouter() const { locale } = router @@ -263,8 +234,10 @@ export default function DatasetPage ({ dataset, spatialCoverageNames = [] }) { lineHeight="20px" color="#464A51" > - {spatialCoverageNames.length > 0 - ? spatialCoverageNames.join(', ') + {dataset?.[`spatialCoverageName${capitalize(locale)}`] + ? Object.values(dataset[`spatialCoverageName${capitalize(locale)}`]) + .sort((a, b) => a.localeCompare(b, locale)) + .join(', ') : t('notProvided')} diff --git a/next/pages/search.js b/next/pages/search.js index 66a3da33..9057cf2f 100644 --- a/next/pages/search.js +++ b/next/pages/search.js @@ -16,6 +16,7 @@ import { useEffect, useState } from "react"; import cookies from "js-cookie"; import { useTranslation } from 'next-i18next'; import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; +import capitalize from "lodash/capitalize"; import Link from "../components/atoms/Link"; import { isMobileMod, useCheckMobile } from "../hooks/useCheckMobile.hook"; import { triggerGAEvent } from "../utils"; @@ -299,8 +300,9 @@ export default function SearchDatasetPage() { themes={data?.themes} name={data?.name || t('noName')} temporalCoverageText={(data?.temporal_coverage && data.temporal_coverage[0]) || ""} - spatialCoverage={(data?.spatial_coverage?.map(coverage => coverage.slug) || []) - .sort((a, b) => a.localeCompare(b)) + spatialCoverage={data?.spatial_coverage + ?.map(coverage => coverage.name) + .sort((a, b) => a.localeCompare(b, locale)) .join(', ')} organizations={data.organizations} tables={{ From ef64b61b701f0bf997a7d6ef0d0254b4b5cbfbd4 Mon Sep 17 00:00:00 2001 From: Ricardo Dahis Date: Thu, 7 Nov 2024 20:32:19 +1100 Subject: [PATCH 2/4] fix dependency --- next/pages/search.js | 1 - 1 file changed, 1 deletion(-) diff --git a/next/pages/search.js b/next/pages/search.js index 9057cf2f..1ea95799 100644 --- a/next/pages/search.js +++ b/next/pages/search.js @@ -16,7 +16,6 @@ import { useEffect, useState } from "react"; import cookies from "js-cookie"; import { useTranslation } from 'next-i18next'; import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; -import capitalize from "lodash/capitalize"; import Link from "../components/atoms/Link"; import { isMobileMod, useCheckMobile } from "../hooks/useCheckMobile.hook"; import { triggerGAEvent } from "../utils"; From aefbe42073277b197a24b32d722b762d077c8941 Mon Sep 17 00:00:00 2001 From: aldemirLucas Date: Thu, 7 Nov 2024 11:52:36 -0300 Subject: [PATCH 3/4] fix: fix text in dataUpdateFrequency --- next/components/organisms/TablePage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/next/components/organisms/TablePage.js b/next/components/organisms/TablePage.js index 2fa99eb4..0e85e721 100644 --- a/next/components/organisms/TablePage.js +++ b/next/components/organisms/TablePage.js @@ -490,7 +490,7 @@ export default function TablePage({ id }) { color="#464A51" > {resource?.rawDataSource?.[0]?.updates?.[0]?.latest ? - `${formatDate(resource.rawDataSource[0].updates[0].latest)}:` + `${formatDate(resource.rawDataSource[0].updates[0].latest)}` : t('table.notProvided') }: {t('table.lastUpdateRawDataSource')} @@ -543,7 +543,7 @@ export default function TablePage({ id }) { color="#464A51" > {resource?.rawDataSource?.[0]?.polls?.[0]?.latest ? - `${formatDate(resource.rawDataSource[0].polls[0].latest)}:` + `${formatDate(resource.rawDataSource[0].polls[0].latest)}` : t('table.notProvided') }: {t('table.lastCheckRawDataSource')} From 7a52f7688b27784e028e6891b4493f9586e112d6 Mon Sep 17 00:00:00 2001 From: aldemirLucas Date: Thu, 7 Nov 2024 11:59:32 -0300 Subject: [PATCH 4/4] fix: removing unused import and const --- next/components/organisms/TablePage.js | 1 - next/pages/dataset/[dataset].js | 1 - 2 files changed, 2 deletions(-) diff --git a/next/components/organisms/TablePage.js b/next/components/organisms/TablePage.js index 4a6451fd..642898ed 100644 --- a/next/components/organisms/TablePage.js +++ b/next/components/organisms/TablePage.js @@ -27,7 +27,6 @@ import TwitterIcon from "../../public/img/icons/twitterIcon"; import InfoIcon from "../../public/img/icons/infoIcon"; import DownloadIcon from "../../public/img/icons/downloadIcon"; import RedirectIcon from "../../public/img/icons/redirectIcon"; -import axios from "axios"; export default function TablePage({ id }) { const { t } = useTranslation('dataset', 'prices'); diff --git a/next/pages/dataset/[dataset].js b/next/pages/dataset/[dataset].js index 4b63ebb8..223dffa5 100644 --- a/next/pages/dataset/[dataset].js +++ b/next/pages/dataset/[dataset].js @@ -36,7 +36,6 @@ import { export async function getStaticProps(context) { const { locale, params } = context; let dataset = null; - const allowedURLs = ["https://basedosdados.org", "https://staging.basedosdados.org"] try { dataset = await getDataset(params.dataset, locale || 'pt');