Skip to content

Commit

Permalink
Switch to API v2 for the retrieval of keywords, categories, regions a…
Browse files Browse the repository at this point in the history
…nd owners (#179)
  • Loading branch information
MuhweziDeo authored May 24, 2021
1 parent f6990d4 commit 7bb618e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 138 deletions.
129 changes: 0 additions & 129 deletions geonode_mapstore_client/client/js/api/geonode/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
import axios from '@mapstore/framework/libs/ajax';
import { getConfigProp } from '@mapstore/framework/utils/ConfigUtils';
import cookies from 'js-cookie';
import { setFilterById } from '@js/utils/GNSearchUtils';


function addCountToLabel(name, count) {
return `${name} (${count || 0})`;
}

/**
* Api for GeoNode v1
Expand Down Expand Up @@ -58,125 +52,6 @@ export const getUserInfo = () => {
.then(({ data }) => data);
};

export const getCategories = ({ q, idIn, ...params }, filterKey = 'categories') => {
const { endpointV1 = '/api' } = getConfigProp('geoNodeApi') || {};
const queryIn = idIn
? idIn.reduce((query, value, idx) => query + (idx === 0 ? '?' : '&') + 'identifier__in=' + value, '')
: '';
return axios.get(`${endpointV1}/categories${queryIn}`, {
params: {
limit: 9999,
...params,
...(q && { identifier__icontains: q })
}
})
.then(({ data }) => {
const results = (data?.objects || [])
.map((result) => {
const selectOption = {
value: result.identifier,
label: addCountToLabel(result.gn_description || result.gn_description_en, result.count)
};
const category = {
...result,
selectOption
};
setFilterById(filterKey + result.identifier, category);
return category;
});
return results;
});
};

export const getKeywords = ({ q, idIn, ...params }, filterKey = 'keywords') => {
const { endpointV1 = '/api' } = getConfigProp('geoNodeApi') || {};
const queryIn = idIn
? idIn.reduce((query, value, idx) => query + (idx === 0 ? '?' : '&') + 'slug__in=' + value, '')
: '';
return axios.get(`${endpointV1}/keywords${queryIn}`, {
params: {
limit: 9999,
...params,
...(q && { slug__icontains: q })
}
})
.then(({ data }) => {
const results = (data?.objects || [])
.map((result) => {
const selectOption = {
value: result.slug,
label: addCountToLabel(result.slug, result.count)
};
const keyword = {
...result,
selectOption
};
setFilterById(filterKey + result.slug, keyword);
return keyword;
});
return results;
});
};

export const getRegions = ({ q, idIn, ...params }, filterKey = 'regions') => {
const { endpointV1 = '/api' } = getConfigProp('geoNodeApi') || {};
const queryIn = idIn
? idIn.reduce((query, value, idx) => query + (idx === 0 ? '?' : '&') + 'name__in=' + value, '')
: '';
return axios.get(`${endpointV1}/regions${queryIn}`, {
params: {
limit: 9999,
...params,
...(q && { name__icontains: q })
}
})
.then(({ data }) => {
const results = (data?.objects || [])
.map((result) => {
const selectOption = {
value: result.name,
label: addCountToLabel(result.name, result.count)
};
const region = {
...result,
selectOption
};
setFilterById(filterKey + result.name, region);
return region;
});
return results;
});
};

export const getOwners = ({ q, idIn, ...params }, filterKey = 'owners') => {
const { endpointV1 = '/api' } = getConfigProp('geoNodeApi') || {};
const queryIn = idIn
? idIn.reduce((query, value, idx) => query + (idx === 0 ? '?' : '&') + 'username__in=' + value, '')
: '';
return axios.get(`${endpointV1}/owners${queryIn}`, {
params: {
limit: 9999,
...params,
...(q && { username__icontains: q })
}
})
.then(({ data }) => {
const results = (data?.objects || [])
.map((result) => {
const selectOption = {
value: result.username,
label: addCountToLabel(result.username, result.count)
};
const owner = {
...result,
selectOption
};
setFilterById(filterKey + result.username, owner);
return owner;
});
return results;
});
};

export const setLanguage = (languageCode) => {
const csrfMiddlewareToken = cookies.get('csrftoken');
Expand All @@ -189,9 +64,5 @@ export const setLanguage = (languageCode) => {

export default {
getResourceByPk,
getCategories,
getKeywords,
getRegions,
getOwners,
setLanguage
};
128 changes: 126 additions & 2 deletions geonode_mapstore_client/client/js/api/geonode/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ let endpoints = {
'geoapps': '/api/v2/geoapps',
'geostories': '/api/v2/geostories',
'users': '/api/v2/users',
'resource_types': '/api/v2/resources/resource_types'
'resource_types': '/api/v2/resources/resource_types',
'categories': '/api/v2/categories',
'owners': '/api/v2/owners',
'keywords': '/api/v2/keywords',
'regions': '/api/v2/regions'
};

const RESOURCES = 'resources';
Expand All @@ -42,8 +46,17 @@ const GEOAPPS = 'geoapps';
const GEOSTORIES = 'geostories';
const USERS = 'users';
const RESOURCE_TYPES = 'resource_types';
const OWNERS = 'owners';
const REGIONS = 'regions';
const CATEGORIES = 'categories';
const KEYWORDS = 'keywords';

// const GROUPS = 'groups';

function addCountToLabel(name, count) {
return `${name} (${count || 0})`;
}

const requestOptions = (name, requestFunc) => {
const options = getRequestOptions(name);
if (!options) {
Expand Down Expand Up @@ -423,6 +436,113 @@ export const getResourcesTotalCount = () => {
});
};

export const getCategories = ({ q, idIn, ...params }, filterKey = 'categories') => {
return axios.get(parseDevHostname(`${endpoints[CATEGORIES]}`), {
params: {
page_size: 9999,
...params,
...(idIn && {'filter{identifier.in}': idIn}),
...(q && { 'filter{identifier.icontains}': q })
}
})
.then(({ data }) => {
const results = (data?.TopicCategories || [])
.map((result) => {
const selectOption = {
value: result.identifier,
label: addCountToLabel(result.gn_description || result.gn_description_en, result.total)
};
const category = {
...result,
selectOption
};
setFilterById(filterKey + result.identifier, category);
return category;
});
return results;
});
};

export const getRegions = ({ q, idIn, ...params }, filterKey = 'regions') => {
return axios.get(parseDevHostname(`${endpoints[REGIONS]}`), {
params: {
page_size: 9999,
...params,
...(idIn && {'filter{name.in}': idIn}),
...(q && { 'filter{name.icontains}': q })
}
})
.then(({ data }) => {
const results = (data?.Regions || [])
.map((result) => {
const selectOption = {
value: result.name,
label: addCountToLabel(result.name, result.total)
};
const region = {
...result,
selectOption
};
setFilterById(filterKey + result.name, region);
return region;
});
return results;
});
};

export const getOwners = ({ q, idIn, ...params }, filterKey = 'owners') => {
return axios.get(parseDevHostname(`${endpoints[OWNERS]}`), {
params: {
page_size: 9999,
...params,
...(idIn && {'filter{username.in}': idIn}),
...(q && { 'filter{username.icontains}': q })
}
})
.then(({ data }) => {
const results = (data?.users || [])
.map((result) => {
const selectOption = {
value: result.username,
label: addCountToLabel(result.username, result.total)
};
const owner = {
...result,
selectOption
};
setFilterById(filterKey + result.username, owner);
return owner;
});
return results;
});
};

export const getKeywords = ({ q, idIn, ...params }, filterKey = 'keywords') => {
return axios.get(parseDevHostname(`${endpoints[KEYWORDS]}`), {
params: {
page_size: 9999,
...params,
...(idIn && {'filter{slug.in}': idIn}),
...(q && { 'filter{slug.icontains}': q })
}
})
.then(({ data }) => {
const results = (data?.HierarchicalKeywords || [])
.map((result) => {
const selectOption = {
value: result.slug,
label: addCountToLabel(result.slug, result.total)
};
const keyword = {
...result,
selectOption
};
setFilterById(filterKey + result.slug, keyword);
return keyword;
});
return results;
});
};
export default {
getEndpoints,
getResources,
Expand All @@ -440,5 +560,9 @@ export default {
getResourceTypes,
getResourcesTotalCount,
getLayerByPk,
getDocumentByPk
getDocumentByPk,
getCategories,
getRegions,
getOwners,
getKeywords
};
9 changes: 2 additions & 7 deletions geonode_mapstore_client/client/js/routes/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,8 @@ import {
import { withResizeDetector } from 'react-resize-detector';
import Footer from '@js/components/home/Footer';
import { useInView } from 'react-intersection-observer';
import {
getKeywords,
getCategories,
getRegions,
getOwners
} from '@js/api/geonode/v1';
import { getResourceTypes } from '@js/api/geonode/v2';

import { getResourceTypes, getCategories, getRegions, getOwners, getKeywords } from '@js/api/geonode/v2';

const DEFAULT_SUGGESTIONS = [];
const DEFAULT_RESOURCES = [];
Expand Down

0 comments on commit 7bb618e

Please sign in to comment.