Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to new endpoint for fetching specifically task markers #2356

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from '../../../services/MapBounds/MapBounds'
import { fetchTaskClusters, clearTaskClusters }
from '../../../services/Task/TaskClusters'
import { fetchBoundedTasks, clearBoundedTasks }
import { fetchBoundedTaskMarkers, fetchBoundedTasks, clearBoundedTasks }
from '../../../services/Task/BoundedTask'
import { MAX_ZOOM, UNCLUSTER_THRESHOLD } from '../../TaskClusterMap/TaskClusterMap'

Expand Down Expand Up @@ -109,12 +109,13 @@
searchCriteria.page = 0

// Fetch up to threshold+1 individual tasks (eg. 1001 tasks)
this.props.fetchBoundedTasks(searchCriteria, UNCLUSTER_THRESHOLD + 1, !storeTasks, ignoreLocked, true).then(results => {
this.props.fetchBoundedTaskMarkers(searchCriteria, UNCLUSTER_THRESHOLD + 1, !storeTasks, ignoreLocked).then(results => {

Check warning on line 112 in src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx#L112

Added line #L112 was not covered by tests
if (currentFetchId >= this.state.fetchId) {
const totalCount = results.length

Check warning on line 114 in src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx#L114

Added line #L114 was not covered by tests
// If we retrieved 1001 tasks then there might be more tasks and
// they should be clustered. So fetch as clusters
// (unless we are zoomed all the way in already)
if (results.totalCount > UNCLUSTER_THRESHOLD &&
if (totalCount > UNCLUSTER_THRESHOLD &&

Check warning on line 118 in src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx#L118

Added line #L118 was not covered by tests
_get(this.props, 'criteria.zoom', 0) < MAX_ZOOM) {
this.props.fetchTaskClusters(challengeId, searchCriteria, 25, overrideDisable
).then(results => {
Expand All @@ -127,8 +128,8 @@
})
}
else {
this.setState({clusters: results.tasks, loading: false,
taskCount: results.totalCount})
this.setState({clusters: results, loading: false,
taskCount: totalCount})

Check warning on line 132 in src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/HOCs/WithChallengeTaskClusters/WithChallengeTaskClusters.jsx#L131-L132

Added lines #L131 - L132 were not covered by tests
}
}
}).catch(error => {
Expand Down Expand Up @@ -256,7 +257,7 @@

export const mapDispatchToProps = dispatch => Object.assign(
{},
bindActionCreators({ fetchTaskClusters, fetchBoundedTasks }, dispatch),
bindActionCreators({ fetchTaskClusters, fetchBoundedTaskMarkers, fetchBoundedTasks }, dispatch),
{
clearTasksAndClusters: () => {
dispatch(clearBoundedTasks())
Expand Down
1 change: 1 addition & 0 deletions src/services/Server/APIRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const apiRoutes = (factory) => {
tasks: {
random: factory.get("/tasks/random", { noCache: true }),
withinBounds: factory.put("/tasks/box/:left/:bottom/:right/:top"),
markersWithinBounds: factory.put("/markers/box/:left/:bottom/:right/:top"),
bulkUpdate: factory.put("/tasks"),
bulkStatusChange: factory.put("/tasks/changeStatus"),
review: factory.get("/tasks/review"),
Expand Down
104 changes: 99 additions & 5 deletions src/services/Task/BoundedTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import AppErrors from '../Error/AppErrors'
import _get from 'lodash/get'
import _values from 'lodash/values'
import _isArray from 'lodash/isArray'
import _isUndefined from 'lodash/isUndefined'
import _map from 'lodash/map'
import { generateSearchParametersString } from '../Search/Search'
Expand Down Expand Up @@ -43,14 +42,109 @@
}
}

// async action creators
/**
* Retrieve all task markers (up to the given limit) matching the given search
* criteria, which should at least include a boundingBox field, and may
* optionally include a filters field with additional constraints
*/
export function fetchBoundedTaskMarkers(criteria, limit = 50, skipDispatch = false, ignoreLocked = true) {
return function(dispatch) {
if (!skipDispatch) {
// The map is either showing task clusters or bounded tasks so we shouldn't
// have both in redux.
// (ChallengeLocation needs to know which challenge tasks pass the location)
dispatch(clearTaskClusters())
}

const normalizedBounds = toLatLngBounds(criteria.boundingBox)
if (!normalizedBounds) {
return null
}

const filters = criteria.filters ?? {};
const searchParameters = generateSearchParametersString(
filters,
null,
criteria.savedChallengesOnly,
null,
null,
criteria.invertFields
)

if (!filters.challengeId) {
const onlyEnabled = criteria.onlyEnabled ?? true;
const challengeStatus = criteria.challengeStatus
if (challengeStatus) {
searchParameters.cStatus = challengeStatus.join(',')
}

// ce: limit to enabled challenges
// pe: limit to enabled projects
searchParameters.ce = onlyEnabled ? 'true' : 'false'
searchParameters.pe = onlyEnabled ? 'true' : 'false'

// if we are restricting to onlyEnabled challenges then let's
// not show 'local' challenges either.
searchParameters.cLocal = onlyEnabled ? CHALLENGE_EXCLUDE_LOCAL :
CHALLENGE_INCLUDE_LOCAL
}

// If we are searching within map bounds we need to ensure the parent
// challenge is also within those bounds
if (filters.location === CHALLENGE_LOCATION_WITHIN_MAPBOUNDS) {
if (Array.isArray(criteria.boundingBox)) {
searchParameters.bb = criteria.boundingBox.join(',')
} else {
searchParameters.bb = criteria.boundingBox
}
}

const fetchId = uuidv1()
!skipDispatch && dispatch(receiveBoundedTasks(null, RequestStatus.inProgress, fetchId))

return new Endpoint(
api.tasks.markersWithinBounds, {
schema: {
tasks: [taskSchema()]
},
variables: {
left: normalizedBounds.getWest(),
bottom: normalizedBounds.getSouth(),
right: normalizedBounds.getEast(),
top: normalizedBounds.getNorth(),
},
params: {
limit,
excludeLocked: ignoreLocked,
...searchParameters,
},
json: filters.taskPropertySearch ? {
taskPropertySearch: filters.taskPropertySearch
} : null,
}
).execute().then(({ result }) => {
let tasks = result ? Object.values(result) : [];
tasks = tasks.map(task => Object.assign(task, task.pointReview))

if (!skipDispatch) {
dispatch(receiveBoundedTasks(tasks, RequestStatus.success, fetchId, tasks.length))
}

return tasks
}).catch(error => {
dispatch(receiveBoundedTasks([], RequestStatus.error, fetchId))
dispatch(addError(AppErrors.boundedTask.fetchFailure))
console.log(error.response || error)
})
}
}

Check warning on line 140 in src/services/Task/BoundedTask.js

View check run for this annotation

Codecov / codecov/patch

src/services/Task/BoundedTask.js#L51-L140

Added lines #L51 - L140 were not covered by tests

/**
* Retrieve all tasks (up to the given limit) matching the given search
* criteria, which should at least include a boundingBox field, and may
* optionally include a filters field with additional constraints
*/
export const fetchBoundedTasks = function(criteria, limit=50, skipDispatch=false, ignoreLocked=true, withGeometries) {
export function fetchBoundedTasks(criteria, limit=50, skipDispatch=false, ignoreLocked=true, withGeometries) {
return function(dispatch) {
if (!skipDispatch) {
// The map is either showing task clusters or bounded tasks so we shouldn't
Expand Down Expand Up @@ -101,7 +195,7 @@
// If we are searching within map bounds we need to ensure the parent
// challenge is also within those bounds
if (filters.location === CHALLENGE_LOCATION_WITHIN_MAPBOUNDS) {
if (_isArray(criteria.boundingBox)) {
if (Array.isArray(criteria.boundingBox)) {

Check warning on line 198 in src/services/Task/BoundedTask.js

View check run for this annotation

Codecov / codecov/patch

src/services/Task/BoundedTask.js#L198

Added line #L198 was not covered by tests
searchParameters.bb = criteria.boundingBox.join(',')
}
else {
Expand Down Expand Up @@ -177,7 +271,7 @@
updatedTasks.loading = true
}
else {
updatedTasks.tasks = _isArray(action.tasks) ? action.tasks : []
updatedTasks.tasks = Array.isArray(action.tasks) ? action.tasks : []

Check warning on line 274 in src/services/Task/BoundedTask.js

View check run for this annotation

Codecov / codecov/patch

src/services/Task/BoundedTask.js#L274

Added line #L274 was not covered by tests
updatedTasks.loading = false
updatedTasks.totalCount = action.totalCount
}
Expand Down
Loading