Skip to content

Commit

Permalink
Merge pull request #1019 from basedosdados/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
AldemirLucas authored Nov 7, 2024
2 parents 52d3d9a + 7a52f76 commit fd5de73
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 107 deletions.
42 changes: 3 additions & 39 deletions next/components/organisms/DatasetSearchCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,18 @@ import { useState, useEffect } from "react";
export default function Dataset({
id,
name,
spatialCoverage,
temporalCoverageText,
organizations,
temporalCoverageText,
spatialCoverage,
tables,
rawDataSources,
informationRequests,
contains,
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
Expand Down Expand Up @@ -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')}
</Text>
</Stack>
}
Expand Down
38 changes: 7 additions & 31 deletions next/components/organisms/TablePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -36,7 +35,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"]

Expand All @@ -47,30 +45,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 {
Expand Down Expand Up @@ -367,7 +341,7 @@ export default function TablePage({ id }) {

<StackSkeleton
height="20px"
width={resource?.spatialCoverageNames ? "100%" : "200px"}
width={resource?.[`spatialCoverageName${capitalize(locale)}`] ? "100%" : "200px"}
>
<Text
fontFamily="Roboto"
Expand All @@ -376,8 +350,10 @@ export default function TablePage({ id }) {
lineHeight="20px"
color="#464A51"
>
{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')}
</Text>
</StackSkeleton>
Expand Down Expand Up @@ -490,7 +466,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')}
Expand Down Expand Up @@ -543,7 +519,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')}
Expand Down
3 changes: 2 additions & 1 deletion next/pages/api/datasets/getDataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions next/pages/api/tables/getTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function getTable(id, locale='pt') {
isDeprecated
temporalCoverage
spatialCoverage
spatialCoverageName${capitalize(locale)}
fullTemporalCoverage
rawDataSource {
edges {
Expand Down
3 changes: 2 additions & 1 deletion next/pages/api/tables/getTableColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ async function getTableColumns(id, locale = 'pt') {
bigqueryType {
name
}
spatialCoverage
temporalCoverage
spatialCoverage
spatialCoverageName${capitalize(locale)}
measurementUnit
containsSensitiveData
observations
Expand Down
38 changes: 5 additions & 33 deletions next/pages/dataset/[dataset].js
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,17 @@ 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);
}

const props = {
...(await serverSideTranslations(locale, ['dataset', 'common', 'menu', 'prices'])),
dataset,
...(allowedURLs.includes(process.env.NEXT_PUBLIC_BASE_URL_FRONTEND) ? {} : { spatialCoverageNames }),
};

return {
Expand All @@ -96,7 +66,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
Expand Down Expand Up @@ -263,8 +233,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')}
</Text>
</GridItem>
Expand Down
5 changes: 3 additions & 2 deletions next/pages/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,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={{
Expand Down

0 comments on commit fd5de73

Please sign in to comment.