Skip to content

Commit

Permalink
Add extra icons for tram disruptions affecting stops (#570)
Browse files Browse the repository at this point in the history
* create icons for each tram stop in disruption

* hover all icons linking to same tram disruption
  • Loading branch information
gldgrnt authored Aug 26, 2021
1 parent 78664a8 commit 34f55bd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ const useCreateIconLayer = (view) => {
return '';
})();

if (mode === 'tram' && stopsAffected?.length > 0) {
return stopsAffected.map(
(stop) =>
new Graphic({
attributes: {
id,
title,
mode,
disruptionSeverity,
mapIcon: `${mode}-${disruptionSeverity}`,
servicesAffected: affectedIds,
startDate,
endDate,
},
geometry: {
type: 'point',
// If no lat/long then default to Birmingham city centre
longitude: stop.lon || -1.8960335,
latitude: stop.lat || 52.481755,
spatialreference: {
wkid: 4326,
},
},
})
);
}

return new Graphic({
attributes: {
id,
Expand All @@ -98,7 +125,7 @@ const useCreateIconLayer = (view) => {

const iconLayer = new FeatureLayer({
id: 'disruptions',
source: disruptionGraphics,
source: disruptionGraphics.flat(Infinity),
objectIdField: 'oid', // This must be defined when creating a layer from `Graphic` objects
fields: [
{
Expand Down
37 changes: 20 additions & 17 deletions src/components/ViewToShow/MapView/Map/customHooks/useHoverIcon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-console */
import { useState, useCallback, useContext, useEffect } from 'react';
import { AutoCompleteContext } from 'globalState';
Expand Down Expand Up @@ -58,33 +59,35 @@ const useHoverIcon = (view, isIconLayerCreated) => {

if (!features.length) return;

const featureToHover = features.filter(
(feature) => feature.attributes.id === disruptionId
)[0];
const featuresToHover = features.filter((feature) => feature.attributes.id === disruptionId);

const featureToUnhover = features.filter((feature) =>
const featuresToUnhover = features.filter((feature) =>
feature.attributes.mapIcon.includes('hover')
)[0];
);
const hoveredFeatureIds = featuresToUnhover.map((hovered) => hovered?.attributes?.id);

const isAlreadyHovered = featureToHover?.attributes?.id === featureToUnhover?.attributes?.id;
if (isAlreadyHovered) return;
const isIdAlreadyHovered = featuresToHover.some((feature) => {
return hoveredFeatureIds.includes(feature?.attributes?.id);
});
if (isIdAlreadyHovered) return;

const updateFeatures = [];
if (featureToUnhover) {
featureToUnhover.attributes.mapIcon = featureToUnhover.attributes.mapIcon.replace(
'-hover',
''
);
updateFeatures.push(featureToUnhover);
if (featuresToUnhover) {
featuresToUnhover.forEach((feature) => {
feature.attributes.mapIcon = feature.attributes.mapIcon.replace('-hover', '');
});
updateFeatures.push(featuresToUnhover);
}

if (featureToHover) {
featureToHover.attributes.mapIcon = `${featureToHover.attributes.mapIcon}-hover`;
updateFeatures.push(featureToHover);
if (featuresToHover.length) {
featuresToHover.forEach((feature) => {
feature.attributes.mapIcon = `${feature.attributes.mapIcon}-hover`;
});
updateFeatures.push(featuresToHover);
}

try {
await disruptionsFeatureLayer.applyEdits({ updateFeatures });
await disruptionsFeatureLayer.applyEdits({ updateFeatures: updateFeatures.flat() });
} catch (error) {
console.log('Error applying disruption feature layer edits:', error);
}
Expand Down

0 comments on commit 34f55bd

Please sign in to comment.