-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '143-hide-filter-by-type-setting-if-the-setting-cannot-b…
…e-changed-on-decidim-geo' into 'main' Hide "Filter by type" setting if the setting cannot be changed on Decidim GEO See merge request decidim/decidim-module-geo!131
- Loading branch information
Showing
20 changed files
with
320 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module Geo | ||
class PointsController < ::Decidim::Api::ApplicationController | ||
before_action :set_default_format | ||
|
||
def index | ||
results = query.select( | ||
permitted_fields_params | ||
).where( | ||
query.arel_table[:id].gt(after_params) | ||
).limit( | ||
first_params | ||
).reorder( | ||
id: :asc | ||
) | ||
last_modified = query.maximum(:updated_at) || 1.year.from_now | ||
etag = query.cache_key_with_version + "/params-" + Digest::MD5.hexdigest(permitted_params.to_json) | ||
if stale?(last_modified: last_modified.utc, etag: etag) | ||
last_id = query.maximum(:id) | ||
render json: { | ||
meta: { | ||
fields: permitted_fields_params, | ||
filters: filters_params, | ||
organization: current_organization.id, | ||
end_cursor: results.last && results.last.id, | ||
default_locale: current_organization.default_locale, | ||
first: first_params, | ||
after: after_params, | ||
has_more: results.last && results.last.id != last_id | ||
}, | ||
data: results | ||
} | ||
end | ||
end | ||
|
||
private | ||
|
||
def permitted_params | ||
@permitted_params ||= params.permit(:first, :after, :locale, :is_index, :format, fields: [], filters: []) | ||
end | ||
|
||
def first_params | ||
@first_params ||= (permitted_params[:first] || "50").to_i | ||
end | ||
|
||
def after_params | ||
@after_params ||= (permitted_params[:after] || "0").to_i | ||
end | ||
|
||
def query | ||
::Decidim::Geo::Api::GeoQuery.new( | ||
current_organization, | ||
current_user, | ||
{ | ||
filters: filters_params, | ||
is_index: is_index_params | ||
}, | ||
locale_param | ||
).results | ||
end | ||
|
||
def is_index_params | ||
@is_index_params ||= permitted_params[:is_index] || false | ||
end | ||
|
||
def locale_param | ||
@locale_param ||= permitted_params[:locale] || I18n.default_locale | ||
end | ||
|
||
def filters_params | ||
@filters_params ||= (permitted_params[:filters] || []).map { |f| JSON.parse(f).with_indifferent_access } | ||
end | ||
|
||
def fields_params | ||
@fields_params ||= permitted_params[:fields] || [] | ||
end | ||
|
||
def include_lonlat? | ||
fields_params.include? "lonlat" | ||
end | ||
|
||
def permitted_fields_params | ||
@permitted_fields_params ||= begin | ||
allowed_fields = %w( | ||
resourceUrl | ||
resourceId | ||
resourceStatus | ||
participatorySpaceId | ||
participatorySpaceType | ||
componentId | ||
resourceType | ||
startDate | ||
endDate | ||
title | ||
shortDescription | ||
descriptionHtml | ||
imageUrl | ||
lonlat | ||
geoScopeId | ||
extendedData | ||
lonlat | ||
) | ||
|
||
filtered_fields = fields_params.select { |field| allowed_fields.include?(field.to_s) } | ||
filtered_fields.push("id") | ||
filtered_fields.map(&:underscore) | ||
end | ||
end | ||
|
||
def set_default_format | ||
request.format = :json | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,71 @@ | ||
import { _getGeoDataSource, _getGeoDataSourceIds } from "./queries"; | ||
|
||
const getGeoDataSource = async (params = {}, fetchAll = true) => { | ||
let results = []; | ||
if (!params.variables) { | ||
params.variables = {}; | ||
} | ||
const apiQuery = fetchAll ? _getGeoDataSource : _getGeoDataSourceIds; | ||
|
||
|
||
const getGeoDataSource = async (params = {}, fetchAll = true, callback) => { | ||
const { filters = [], locale, isIndex = false, after = 0, first = 50 } = params; | ||
const fields = fetchAll | ||
? [ | ||
"resourceUrl", | ||
"resourceId", | ||
"resourceType", | ||
"resourceStatus", | ||
"participatorySpaceId", | ||
"participatorySpaceType", | ||
"componentId", | ||
"startDate", | ||
"endDate", | ||
"title", | ||
"shortDescription", | ||
"descriptionHtml", | ||
"imageUrl", | ||
"latitude", | ||
"longitude", | ||
"scopeId", | ||
"lonlat", | ||
"extendedData" | ||
] | ||
: ["resourceType"]; | ||
|
||
const searchParams = new URLSearchParams({ | ||
locale, | ||
is_index: isIndex, | ||
after, | ||
first | ||
}); | ||
filters.forEach((f) => { | ||
searchParams.append("filters[]", JSON.stringify(f)) | ||
}) | ||
fields.forEach((f) => searchParams.append("fields[]", f)); | ||
let page; | ||
try { | ||
page = await apiQuery(params); | ||
const response = await fetch("/api/decidim-geo/points?" + searchParams.toString()); | ||
if (response.ok) page = await response.json(); | ||
else throw new Error(await response.text()); | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
if (!page) return { nodes: [], edges: [] }; | ||
const { hasNextPage = false, endCursor = "" } = page?.pageInfo || {}; | ||
results = results.concat(page.nodes); | ||
let hasMore = hasNextPage; | ||
params.variables.after = endCursor; | ||
while (hasMore) { | ||
const { has_more: hasMoreThanOne, end_cursor: endCursor } = page?.meta || {}; | ||
callback(page.data, hasMoreThanOne) | ||
if (!hasMoreThanOne) return; | ||
|
||
searchParams.set("after", endCursor); | ||
while (true) { | ||
try { | ||
page = await apiQuery(params); | ||
const response = await fetch("/api/decidim-geo/points?" + searchParams.toString()); | ||
if (response.ok) page = await response.json(); | ||
else throw new Error(await response.text()); | ||
} catch (error) { | ||
console.error(error); | ||
return { nodes: results }; | ||
return; | ||
} | ||
const { endCursor = params.variables.after, hasNextPage } = page.pageInfo || {}; | ||
results = results.concat(page.nodes); | ||
hasMore = hasNextPage; | ||
params.variables.after = endCursor; | ||
const { end_cursor: endCursor, has_more: hasMore } = page.meta || {}; | ||
callback(page.data, hasMore) | ||
if (!hasMore) break; | ||
searchParams.set("after", endCursor); | ||
} | ||
|
||
return { nodes: results }; | ||
}; | ||
export default getGeoDataSource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.