Skip to content
Snippets Groups Projects
Commit 51d29b4f authored by esikkala's avatar esikkala
Browse files

Refactor facet functions

parent f119630a
No related branches found
No related tags found
No related merge requests found
...@@ -151,9 +151,8 @@ export const fetchPlaceFailed = (error) => ({ ...@@ -151,9 +151,8 @@ export const fetchPlaceFailed = (error) => ({
}); });
// Facet // Facet
export const fetchFacet = (property) => ({ export const fetchFacet = () => ({
type: FETCH_FACET, type: FETCH_FACET,
property
}); });
export const updateFacet = ({ facetValues }) => ({ export const updateFacet = ({ facetValues }) => ({
type: UPDATE_FACET, type: UPDATE_FACET,
......
...@@ -40,14 +40,9 @@ class FacetDialog extends React.Component { ...@@ -40,14 +40,9 @@ class FacetDialog extends React.Component {
} }
} }
handleClickOpen = () => { handleClickOpen = () => this.setState({ open: true });
this.props.fetchFacet(this.props.property);
this.setState({ open: true });
};
handleClose = () => { handleClose = () => this.setState({ open: false });
this.setState({ open: false });
};
render() { render() {
const { classes, propertyLabel, facet } = this.props; const { classes, propertyLabel, facet } = this.props;
...@@ -75,6 +70,7 @@ class FacetDialog extends React.Component { ...@@ -75,6 +70,7 @@ class FacetDialog extends React.Component {
: :
<Tree <Tree
data={facet.facetValues.creationPlace} data={facet.facetValues.creationPlace}
fetchFacet={this.props.fetchFacet}
updateFilter={this.props.updateFilter} updateFilter={this.props.updateFilter}
/>} />}
</DialogContent> </DialogContent>
......
...@@ -57,6 +57,7 @@ class ResultTable extends React.Component { ...@@ -57,6 +57,7 @@ class ResultTable extends React.Component {
componentDidMount = () => { componentDidMount = () => {
this.props.fetchResults(); this.props.fetchResults();
this.props.fetchManuscripts(0); this.props.fetchManuscripts(0);
this.props.fetchFacet();
} }
idRenderer = (row) => { idRenderer = (row) => {
......
...@@ -37,12 +37,14 @@ class Tree extends Component { ...@@ -37,12 +37,14 @@ class Tree extends Component {
}; };
} }
handleCheckboxChange = name => event => { handleCheckboxChange = name => () => {
console.log(name) //console.log(name)
console.log(event.target.checked) //console.log(event.target.checked)
// this.props.updateFilter({ this.props.updateFilter({
// property: 'creationPlace',
// }) value: name
});
this.props.fetchFacet();
}; };
render() { render() {
...@@ -170,6 +172,7 @@ class Tree extends Component { ...@@ -170,6 +172,7 @@ class Tree extends Component {
Tree.propTypes = { Tree.propTypes = {
classes: PropTypes.object.isRequired, classes: PropTypes.object.isRequired,
data: PropTypes.array.isRequired, data: PropTypes.array.isRequired,
fetchFacet: PropTypes.func.isRequired,
updateFilter: PropTypes.func.isRequired, updateFilter: PropTypes.func.isRequired,
}; };
......
...@@ -89,6 +89,8 @@ let MapApp = (props) => { ...@@ -89,6 +89,8 @@ let MapApp = (props) => {
// browser // browser
// error, // error,
// console.log(props.facet)
return ( return (
<div className={classes.root}> <div className={classes.root}>
<div className={classes.appFrame}> <div className={classes.appFrame}>
......
import { ajax } from 'rxjs/ajax'; import { ajax } from 'rxjs/ajax';
import { mergeMap, map } from 'rxjs/operators'; import { mergeMap, map, withLatestFrom } from 'rxjs/operators';
import { combineEpics, ofType } from 'redux-observable'; import { combineEpics, ofType } from 'redux-observable';
import { import {
updateManuscripts, updateManuscripts,
...@@ -57,9 +57,10 @@ const getPlace = action$ => action$.pipe( ...@@ -57,9 +57,10 @@ const getPlace = action$ => action$.pipe(
const getFacet = action$ => action$.pipe( const getFacet = action$ => action$.pipe(
ofType(FETCH_FACET), ofType(FETCH_FACET),
mergeMap((action) => { mergeMap(() => {
const searchUrl = apiUrl + 'facet'; const requestUrl = `${apiUrl}facet`;
const requestUrl = `${searchUrl}?property=${action.property}`; //const facetFilters = state$.getState().facet.facetFilters;
//let str = Object.entries(facetFilters).map(([key, set]) => `${key}=${Array.from(set)}`).join('&');
return ajax.getJSON(requestUrl).pipe( return ajax.getJSON(requestUrl).pipe(
map(response => updateFacet({ facetValues: response })) map(response => updateFacet({ facetValues: response }))
); );
......
import { import {
FETCH_FACET, FETCH_FACET,
UPDATE_FACET, UPDATE_FACET,
UPDATE_FILTER
} from '../actions'; } from '../actions';
export const INITIAL_STATE = { export const INITIAL_STATE = {
...@@ -16,6 +17,10 @@ export const INITIAL_STATE = { ...@@ -16,6 +17,10 @@ export const INITIAL_STATE = {
creationPlace: [], creationPlace: [],
author: [] author: []
}, },
facetFilters: {
creationPlace: new Set(),
author: new Set(),
},
fetchingFacet : false fetchingFacet : false
}; };
...@@ -29,9 +34,30 @@ const facet = (state = INITIAL_STATE, action) => { ...@@ -29,9 +34,30 @@ const facet = (state = INITIAL_STATE, action) => {
facetValues: action.facetValues, facetValues: action.facetValues,
fetchingFacet: false fetchingFacet: false
}; };
case UPDATE_FILTER:
return updateFilter(state, action);
default: default:
return state; return state;
} }
}; };
const updateFilter = (state, action) => {
const { property, value } = action.filter;
let nSet = state.facetFilters[property];
if (nSet.has(value)) {
nSet.delete(value);
} else {
nSet.add(value);
}
const newFilter = updateObject(state.filters, { [property]: nSet });
return updateObject(state, { facetFilters: newFilter });
};
const updateObject = (oldObject, newValues) => {
// Encapsulate the idea of passing a new object as the first parameter
// to Object.assign to ensure we correctly copy data instead of mutating
//console.log(Object.assign({}, oldObject, newValues));
return Object.assign({}, oldObject, newValues);
};
export default facet; export default facet;
...@@ -15,7 +15,6 @@ import { ...@@ -15,7 +15,6 @@ import {
CLEAR_PLACES, CLEAR_PLACES,
UPDATE_PLACE, UPDATE_PLACE,
SORT_RESULTS, SORT_RESULTS,
UPDATE_FILTER
} from '../actions'; } from '../actions';
export const INITIAL_STATE = { export const INITIAL_STATE = {
...@@ -34,10 +33,6 @@ export const INITIAL_STATE = { ...@@ -34,10 +33,6 @@ export const INITIAL_STATE = {
'selected': false 'selected': false
}, },
}, },
filters: {
creationPlace: new Set(),
author: new Set(),
},
suggestions: [], suggestions: [],
suggestionsQuery: '', suggestionsQuery: '',
fetchingSuggestions: false, fetchingSuggestions: false,
...@@ -132,10 +127,7 @@ const search = (state = INITIAL_STATE, action) => { ...@@ -132,10 +127,7 @@ const search = (state = INITIAL_STATE, action) => {
'places': {}, 'places': {},
fetchingPlaces: false fetchingPlaces: false
}; };
case UPDATE_FILTER:
return updateFilter(state, action);
case SORT_RESULTS: case SORT_RESULTS:
//console.log(action)
return { return {
...state, ...state,
sortBy: action.options.sortBy, sortBy: action.options.sortBy,
...@@ -146,23 +138,4 @@ const search = (state = INITIAL_STATE, action) => { ...@@ -146,23 +138,4 @@ const search = (state = INITIAL_STATE, action) => {
} }
}; };
const updateFilter = (state, action) => {
const { property, value } = action.filter;
let nSet = state.filters[property];
if (nSet.has(value)) {
nSet.delete(value);
} else {
nSet.add(value);
}
const newFilter = updateObject(state.filters, { [property]: nSet });
return updateObject(state, { filters: newFilter });
};
const updateObject = (oldObject, newValues) => {
// Encapsulate the idea of passing a new object as the first parameter
// to Object.assign to ensure we correctly copy data instead of mutating
//console.log(Object.assign({}, oldObject, newValues));
return Object.assign({}, oldObject, newValues);
};
export default search; export default search;
...@@ -85,13 +85,12 @@ app.get('/places/:placeId?', (req, res) => { ...@@ -85,13 +85,12 @@ app.get('/places/:placeId?', (req, res) => {
}); });
app.get('/facet', (req, res) => { app.get('/facet', (req, res) => {
const property = req.query.property; return getFacet().then((data) => {
return getFacet(property).then((data) => {
const facetValues = { const facetValues = {
creationPlace: data, creationPlace: data,
author: [] author: []
}; };
//console.log(data); // console.log(data);
res.json(facetValues); res.json(facetValues);
}) })
.catch((err) => { .catch((err) => {
......
...@@ -305,6 +305,52 @@ module.exports = { ...@@ -305,6 +305,52 @@ module.exports = {
#} #}
} }
`, `,
'facetQuery': `
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX text: <http://jena.apache.org/text#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX sch: <http://schema.org/>
PREFIX geosparql: <http://www.opengis.net/ont/geosparql#>
PREFIX frbroo: <http://erlangen-crm.org/efrbroo/>
PREFIX mmm-schema: <http://ldf.fi/mmm/schema/>
SELECT DISTINCT ?cnt ?facet_text ?value ?parent
WHERE {
SELECT DISTINCT ?cnt ?value ?facet_text ?parent {
{ SELECT DISTINCT (count(DISTINCT ?id) as ?cnt) ?value ?parent
{
?id a frbroo:F4_Manifestation_Singleton .
?id skos:prefLabel ?name .
?id ^frbroo:R18_created/crm:P7_took_place_at ?value .
OPTIONAL { ?value mmm-schema:parent ?parent }
}
GROUP BY ?value ?parent
}
FILTER(BOUND(?value)) BIND(COALESCE(?value, <http://ldf.fi/NONEXISTENT_URI>) AS ?labelValue)
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "fi")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "fi")) . }
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "en")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "en")) . }
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "sv")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "sv")) . }
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "")) . }
BIND(COALESCE(?lbl, IF(!ISURI(?value), ?value, "")) AS ?facet_text) }
}
`,
'tgn': { 'tgn': {
// Getty LOD documentation: // Getty LOD documentation:
// http://vocab.getty.edu/queries#Places_by_Type // http://vocab.getty.edu/queries#Places_by_Type
......
...@@ -37,8 +37,8 @@ export const getPlace = (id) => { ...@@ -37,8 +37,8 @@ export const getPlace = (id) => {
return sparqlSearchEngine.doSearch(placeQuery, endpoint, makeObjectList); return sparqlSearchEngine.doSearch(placeQuery, endpoint, makeObjectList);
}; };
export const getFacet = (property) => { export const getFacet = () => {
const { endpoint } = datasetConfig['mmm']; const { endpoint, facetQuery } = datasetConfig['mmm'];
// console.log(facetQuery) // console.log(facetQuery)
return sparqlSearchEngine.doSearch(facetQuery, endpoint, mapFacet); return sparqlSearchEngine.doSearch(facetQuery, endpoint, mapFacet);
}; };
...@@ -53,50 +53,3 @@ const generateFilter = (filterObj) => { ...@@ -53,50 +53,3 @@ const generateFilter = (filterObj) => {
} }
return filterStr; return filterStr;
}; };
const facetQuery = `
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX text: <http://jena.apache.org/text#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX sch: <http://schema.org/>
PREFIX geosparql: <http://www.opengis.net/ont/geosparql#>
PREFIX frbroo: <http://erlangen-crm.org/efrbroo/>
PREFIX mmm-schema: <http://ldf.fi/mmm/schema/>
SELECT DISTINCT ?cnt ?facet_text ?value ?parent
WHERE {
SELECT DISTINCT ?cnt ?value ?facet_text ?parent {
{ SELECT DISTINCT (count(DISTINCT ?id) as ?cnt) ?value ?parent
{
?id a frbroo:F4_Manifestation_Singleton .
?id skos:prefLabel ?name .
?id ^frbroo:R18_created/crm:P7_took_place_at ?value .
OPTIONAL { ?value mmm-schema:parent ?parent }
}
GROUP BY ?value ?parent
}
FILTER(BOUND(?value)) BIND(COALESCE(?value, <http://ldf.fi/NONEXISTENT_URI>) AS ?labelValue)
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "fi")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "fi")) . }
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "en")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "en")) . }
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "sv")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "sv")) . }
OPTIONAL { ?labelValue skos:prefLabel ?lbl .
FILTER(langMatches(lang(?lbl), "")) . }
OPTIONAL { ?labelValue rdfs:label ?lbl .
FILTER(langMatches(lang(?lbl), "")) . }
BIND(COALESCE(?lbl, IF(!ISURI(?value), ?value, "")) AS ?facet_text) }
}
`;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment