Commit b1913300 authored by Elisabeth Reuhl's avatar Elisabeth Reuhl
Browse files

Filters for periods and regions: extract fetching of filter values to utils

parent e8425952
import React, { useEffect, useState } from 'react';
import {
Checkbox, CircularProgress, FormControlLabel, FormGroup, FormLabel, Grid, IconButton, Input, Radio, RadioGroup,
Switch,
Checkbox, CircularProgress, FormControlLabel, FormGroup, FormLabel, Grid, IconButton, Radio, RadioGroup, Switch,
TextField, Tooltip, Typography
} from '@material-ui/core';
import Autocomplete, { createFilterOptions } from '@material-ui/lab/Autocomplete';
import ClearIcon from "@material-ui/icons/Clear";
import { useStyles } from '../../styles';
import { neutralColor, useStyles } from '../../styles';
import { useTranslation } from "react-i18next";
import { arachneCategories, catalogs, gazetteerProvenances } from "../../config";
import { sortByCount, sortByLabel } from "../../utils";
import { neutralColor } from "../../styles";
import { getPeriodFilterData, getRegionFilterData } from "../../utils";
export const Filters = (props) => {
......@@ -45,19 +43,8 @@ export const Filters = (props) => {
if (periods.length!==0) return;
let data;
//in the url below, the geographical focus on results located in Africa is achieved with the country facet
// ("facet_land:[country in Africa]") the period names are taken from the results' values for the period
// facet ("facet_datierungepoche")
//NOTE: in case data is added to iDAI.objects that is localized in an African country not previously present in
// iDAI.objects, the url below needs to be updated with an additional "&fq=facet_land:%22[new country]%22"
fetch("https://arachne.dainst.org/data/index?fq=facet_land:%22%C3%84gypten%22&fq=facet_land:%22Algerien%22&fq=facet_land:%22%C3%84thiopien%22&fq=facet_land:%22Burkina%20Faso%22&fq=facet_land:%22Elfenbeink%C3%BCste%22&fq=facet_land:%22Kongo%22&fq=facet_land:%22Libyen%22&fq=facet_land:%22Madagaskar%22&fq=facet_land:%22Marokko%22&fq=facet_land:%22Niger%22&fq=facet_land:%22Nigeria%22&fq=facet_land:%22Republic%20of%20Namibia%22&fq=facet_land:%22Republik%20Tunesien%22&fq=facet_land:%22Somalia%22&fq=facet_land:%22Sudan%22&fq=facet_land:%22Tunesien%22&fq=facet_land:%22Tschad%22&q=*&limit=0&fl=1000")
.then(response => response.json())
.then((jsonData) => {
let periodFacet = jsonData.filter(facet => facet.name === "facet_datierungepoche")[0].values;
//data is sorted by count, i.e., number of results associated with the period
data = periodFacet.sort(sortByCount);
})
getPeriodFilterData()
.then((response) => data = response)
.then(() => setPeriods(data))
.catch((error) => {
console.error(error)
......@@ -68,26 +55,8 @@ export const Filters = (props) => {
if (regions.length!==0) return;
let data;
//in the url below, the results are restricted to either children of 2042601 ("Africa") or 2293101
// ("_Geographische Regionen (Afrika)") OR results of type "archaeological area" that are located within 2042601
fetch("https://gazetteer.dainst.org/search.json?q=(parent:(2042601 OR 2293101)) OR (types:archaeological-area AND (ancestors:2042601))&offset=0&limit=1000")
.then(response => response.json())
.then((jsonData) => {
//extract id and prefName from JSON, afterwards filter out some results that are undefined because they
// do not have a prefName because they are not publicly visible in the iDAI.gazetteer
let regionResult = jsonData.result.map(
result => {
return result.prefName && {
id: result.gazId,
label: result.prefName.title,
names: result.names //fetch all names, pick the name(s) in the current language elsewhere
}
}
).filter(result => result!==undefined);
//data is sorted alphabetically by label
data = regionResult.sort(sortByLabel);
})
getRegionFilterData()
.then((response) => data = response)
.then(() => setRegions(data))
.catch((error) => {
console.error(error)
......
......@@ -670,6 +670,53 @@ function getNumberOfResults (searchString, coordinates, periods, catalogIds, ent
});
}
const getPeriodFilterData = () => {
//in the url below, the geographical focus on results located in Africa is achieved with the country facet
// ("facet_land:[country in Africa]") the period names are taken from the results' values for the period
// facet ("facet_datierungepoche")
//NOTE: in case data is added to iDAI.objects that is localized in an African country not previously present in
// iDAI.objects, the url below needs to be updated with an additional "&fq=facet_land:%22[new country]%22"
return fetch("https://arachne.dainst.org/data/index?fq=facet_land:%22%C3%84gypten%22&fq=facet_land:%22Algerien%22&fq=facet_land:%22%C3%84thiopien%22&fq=facet_land:%22Burkina%20Faso%22&fq=facet_land:%22Elfenbeink%C3%BCste%22&fq=facet_land:%22Kongo%22&fq=facet_land:%22Libyen%22&fq=facet_land:%22Madagaskar%22&fq=facet_land:%22Marokko%22&fq=facet_land:%22Niger%22&fq=facet_land:%22Nigeria%22&fq=facet_land:%22Republic%20of%20Namibia%22&fq=facet_land:%22Republik%20Tunesien%22&fq=facet_land:%22Somalia%22&fq=facet_land:%22Sudan%22&fq=facet_land:%22Tunesien%22&fq=facet_land:%22Tschad%22&q=*&limit=0&fl=1000")
.then(response => response.json())
.then((jsonData) => {
let periodFacet = jsonData.filter(facet => facet.name === "facet_datierungepoche")[0].values;
//data is sorted by count, i.e., number of results associated with the period
return periodFacet.sort(sortByCount);
})
.catch((error) => {
console.error(error)
});
};
const getRegionFilterData = () => {
//in the url below, the results are restricted to either children of 2042601 ("Africa") or 2293101
// ("_Geographische Regionen (Afrika)") OR results of type "archaeological area" that are located within 2042601.
//NOTE: this selection of regions to show in the filter could be improved by restricting it to only those which
// are not 'empty' but actually contain any places of type "archaeological site" (this is what the hub queries for)
return fetch("https://gazetteer.dainst.org/search.json?q=(parent:(2042601 OR 2293101)) OR (types:archaeological-area AND (ancestors:2042601))&offset=0&limit=1000")
.then(response => response.json())
.then((jsonData) => {
//extract id and prefName from JSON, afterwards filter out some results that are undefined because they
// do not have a prefName because they are not publicly visible in the iDAI.gazetteer
let regionResult = jsonData.result.map(
result => {
return result.prefName && {
id: result.gazId,
label: result.prefName.title,
names: result.names //fetch all names, pick the name(s) in the current language elsewhere
}
}
).filter(result => result!==undefined);
//data is sorted alphabetically by label
return regionResult.sort(sortByLabel);
})
.catch((error) => {
console.error(error)
});
}
export {
useDebounce,
......@@ -687,5 +734,7 @@ export {
sortByLabel,
sortByImportance,
sortByCount,
getNumberOfResults
getNumberOfResults,
getPeriodFilterData,
getRegionFilterData
};
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment