Skip to content

Commit

Permalink
Merge pull request #2908 from HSLdevcom/DT-3156
Browse files Browse the repository at this point in the history
DT-3156: Enable general communication banner (=yleisviestipalvelu) for itinerary view to support targeting for destination area
  • Loading branch information
optionsome authored Aug 29, 2019
2 parents 354477a + 54b3565 commit 72dea83
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 27 deletions.
16 changes: 16 additions & 0 deletions app/component/SummaryPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import ComponentUsageExample from './ComponentUsageExample';
import exampleData from './data/SummaryPage.ExampleData';
import { isBrowser } from '../util/browser';
import { itineraryHasCancelation } from '../util/alertUtils';
import triggerMessage from '../util/messageUtils';
import MessageStore from '../store/MessageStore';

export const ITINERARYFILTERING_DEFAULT = 1.5;

Expand Down Expand Up @@ -88,6 +90,7 @@ class SummaryPage extends React.Component {
config: PropTypes.object,
executeAction: PropTypes.func.isRequired,
headers: PropTypes.object.isRequired,
getStore: PropTypes.func,
};

static propTypes = {
Expand Down Expand Up @@ -182,6 +185,19 @@ class SummaryPage extends React.Component {
} = this.context;
const itineraries = (plan && plan.itineraries) || [];
const activeIndex = getActiveIndex(location, itineraries);
triggerMessage(
from.lat,
from.lon,
this.context,
this.context.getStore(MessageStore).getMessages(),
);

triggerMessage(
to.lat,
to.lon,
this.context,
this.context.getStore(MessageStore).getMessages(),
);

const leafletObjs = sortBy(
itineraries.map((itinerary, i) => (
Expand Down
53 changes: 26 additions & 27 deletions app/component/map/MapWithTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
stopRealTimeClient,
changeRealTimeClientTopics,
} from '../../action/realTimeClientAction';
import { findFeatures } from '../../util/geo-utils';
import { updateMessage } from '../../action/MessageActions';
import triggerMessage from '../../util/messageUtils';

const DEFAULT_ZOOM = 12;
const FOCUS_ZOOM = 16;
Expand Down Expand Up @@ -138,7 +137,7 @@ class MapWithTrackingStateHandler extends React.Component {
const lon = this.state.focusOnDestination
? this.state.destination.lon
: this.state.origin.lon;
await this.triggerMessage(lat, lon);
await triggerMessage(lat, lon, this.context, this.props.messages);
}
}

Expand All @@ -152,7 +151,12 @@ class MapWithTrackingStateHandler extends React.Component {
!this.state.origin.gps) // current position selected
) {
this.usePosition(newProps.origin);
this.triggerMessage(newProps.origin.lat, newProps.origin.lon);
triggerMessage(
newProps.origin.lat,
newProps.origin.lon,
this.context,
this.props.messages,
);
} else if (
// "current position selected"
newProps.destination.lat !== null &&
Expand All @@ -163,7 +167,12 @@ class MapWithTrackingStateHandler extends React.Component {
!this.state.destination.gps) // current position selected
) {
this.usePosition(newProps.destination);
this.triggerMessage(newProps.destination.lat, newProps.destination.lon);
triggerMessage(
newProps.destination.lat,
newProps.destination.lon,
this.context,
this.props.messages,
);
} else if (
// "poi selected"
!newProps.origin.gps &&
Expand All @@ -173,7 +182,12 @@ class MapWithTrackingStateHandler extends React.Component {
newProps.origin.lon != null
) {
this.useOrigin(newProps.origin);
this.triggerMessage(newProps.origin.lat, newProps.origin.lon);
triggerMessage(
newProps.origin.lat,
newProps.origin.lon,
this.context,
this.props.messages,
);
} else if (
// destination selected without poi
!newProps.destination.gps &&
Expand All @@ -183,7 +197,12 @@ class MapWithTrackingStateHandler extends React.Component {
newProps.destination.lon != null
) {
this.useDestination(newProps.destination);
this.triggerMessage(newProps.destination.lat, newProps.destination.lon);
triggerMessage(
newProps.destination.lat,
newProps.destination.lon,
this.context,
this.props.messages,
);
}
}

Expand Down Expand Up @@ -257,26 +276,6 @@ class MapWithTrackingStateHandler extends React.Component {
return geoHashes;
};

triggerMessage = (lat, lon) => {
const messages = this.props.messages.filter(
msg => !msg.shouldTrigger && msg.content && msg.geoJson,
);
messages.forEach(msg => {
return new Promise(resolve => {
resolve(this.props.getGeoJsonData(msg.geoJson));
}).then(value => {
const data = findFeatures(
{ lat, lon },
(value && value.data && value.data.features) || [],
);
if (data.length > 0) {
msg.shouldTrigger = true; // eslint-disable-line no-param-reassign
this.context.executeAction(updateMessage, msg);
}
});
});
};

startClient() {
const { realTime, defaultEndpoint } = this.props.config;
const agency = this.props.config.feedIds[0];
Expand Down
1 change: 1 addition & 0 deletions app/store/MessageStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class MessageStore extends Store {
} else {
message.shouldTrigger = true;
}

this.messages.set(message.id, message);
this.emitChange();
};
Expand Down
32 changes: 32 additions & 0 deletions app/util/messageUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { findFeatures } from './geo-utils';
import { updateMessage } from '../action/MessageActions';
import GeoJsonStore from '../store/GeoJsonStore';

/**
* Checks if the user is inside an area polygon featured in a message
*
* @param {*} lat The latitude of the position
* @param {*} lon The longitude of the position
* @param {*} context the context of the component
* @param {*} messagesToCheck the messages to be checked
*/
export default function triggerMessage(lat, lon, context, messagesToCheck) {
const { getGeoJsonData } = context.getStore(GeoJsonStore);
const messages = messagesToCheck.filter(
msg => !msg.shouldTrigger && msg.content && msg.geoJson,
);
messages.forEach(msg => {
return new Promise(resolve => {
resolve(getGeoJsonData(msg.geoJson));
}).then(value => {
const data = findFeatures(
{ lat, lon },
(value && value.data && value.data.features) || [],
);
if (data.length > 0) {
msg.shouldTrigger = true; // eslint-disable-line no-param-reassign
context.executeAction(updateMessage, msg);
}
});
});
}
72 changes: 72 additions & 0 deletions test/unit/util/messageUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { mockContext } from '../helpers/mock-context';
import triggerMessage from '../../../app/util/messageUtils';
import MessageStore from '../../../app/store/MessageStore';

describe('triggerMessage', () => {
it("should call the update message action if position is found from the message's GeoJSON", async () => {
const store = new MessageStore();

const lon = 24.933973;
const lat = 60.199017;

const config = {
staticMessages: [
{
id: '1',
content: {
en: [
{
type: 'text',
content: 'bar',
},
],
},
shouldTrigger: false,
priority: -1,
},
],
};

await store.addConfigMessages(config);

const msgs = [
{
dataURI: '',
geoJson:
'data:application/json;base64,ewogICJ0eXBlIjogIkZlYXR1cmVDb2xsZWN0aW9uIiwKICAiZmVhdHVyZXMiOiBbCiAgICB7CiAgICAgICJ0eXBlIjogIkZlYXR1cmUiLAogICAgICAiZ2VvbWV0cnkiOiB7CiAgICAgICAgInR5cGUiOiAiUG9seWdvbiIsCiAgICAgICAgImNvb3JkaW5hdGVzIjogWwogICAgICAgICAgWwogICAgICAgICAgICBbCiAgICAgICAgICAgICAgMjQuOTMxNDAzNjQ4NTg2NTI0LAogICAgICAgICAgICAgIDYwLjIwMDc2NjQ1MDk1NDE4CiAgICAgICAgICAgIF0sCiAgICAgICAgICAgIFsKICAgICAgICAgICAgICAyNC45Mjg1MDY4NjI4NTA0NCwKICAgICAgICAgICAgICA2MC4xOTk4NjAwMjc3NDA5NTQKICAgICAgICAgICAgXSwKICAgICAgICAgICAgWwogICAgICAgICAgICAgIDI0LjkyOTQ1MTAwMDQyMzY4MywKICAgICAgICAgICAgICA2MC4xOTcwOTc5NDg1MDMyCiAgICAgICAgICAgIF0sCiAgICAgICAgICAgIFsKICAgICAgICAgICAgICAyNC45MzQ1NzkzODQwNjAxNTcsCiAgICAgICAgICAgICAgNjAuMTk1MTc4MjE5ODIxNzEKICAgICAgICAgICAgXSwKICAgICAgICAgICAgWwogICAgICAgICAgICAgIDI0Ljk0MzE2MjQ1MjkwNzgxNCwKICAgICAgICAgICAgICA2MC4xOTYyNTU0MTQ3NDIzMgogICAgICAgICAgICBdLAogICAgICAgICAgICBbCiAgICAgICAgICAgICAgMjQuOTQ0NDA2OTk3ODkwNzI0LAogICAgICAgICAgICAgIDYwLjIwMDIzMzI2Mzg2MTEzCiAgICAgICAgICAgIF0sCiAgICAgICAgICAgIFsKICAgICAgICAgICAgICAyNC45Mzg2OTkyNTcxMDcwMzIsCiAgICAgICAgICAgICAgNjAuMjAxNTg3NTQyMTMyNzMKICAgICAgICAgICAgXSwKICAgICAgICAgICAgWwogICAgICAgICAgICAgIDI0LjkzMTQwMzY0ODU4NjUyNCwKICAgICAgICAgICAgICA2MC4yMDA3NjY0NTA5NTQxOAogICAgICAgICAgICBdCiAgICAgICAgICBdCiAgICAgICAgXQogICAgICB9LAogICAgICAicHJvcGVydGllcyI6IHt9CiAgICB9CiAgXQp9',
content: {
fi: [
{
content: 'Geojson testi: olet Pasilassa',
type: 'heading',
},
],
en: [
{
content: 'Geojson test: you are in Pasila',
type: 'heading',
},
],
sv: [
{
content: 'Geojson test: Pasila',
type: 'heading',
},
],
},
backgroundColor: '#ffffff',
textColor: '#449599',
type: 'info',
id: '16082019_120612_45',
persistence: 'repeat',
priority: '2',
shouldTrigger: true,
},
];

await triggerMessage(lon, lat, mockContext, msgs);
expect(store.getMessages()[0].shouldTrigger).to.equal(true);
});
});

0 comments on commit 72dea83

Please sign in to comment.