Skip to content

Commit

Permalink
Merge branch 'v3' into waltti
Browse files Browse the repository at this point in the history
  • Loading branch information
vesameskanen committed Jan 7, 2025
2 parents 412fcfe + 00905f7 commit 4d29394
Show file tree
Hide file tree
Showing 22 changed files with 585 additions and 759 deletions.
104 changes: 0 additions & 104 deletions app/action/MockGeolocationApi.js

This file was deleted.

116 changes: 39 additions & 77 deletions app/action/PositionActions.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import debounce from 'lodash/debounce';
import d from 'debug';
import { getJson } from '../util/xhrPromise';
import geolocationMessages from '../util/geolocationMessages';
import { addAnalyticsEvent } from '../util/analyticsUtils';

const debug = d('PositionActions.js');

let geoWatchId;

function reverseGeocodeAddress(actionContext, location) {
function reverseGeocodeAddress(actionContext, coords) {
const language = actionContext.getStore('PreferencesStore').getLanguage();

const searchParams = {
'point.lat': location.lat,
'point.lon': location.lon,
'point.lat': coords.latitude,
'point.lon': coords.longitude,
lang: language,
size: 1,
layers: 'address',
Expand All @@ -23,6 +20,7 @@ function reverseGeocodeAddress(actionContext, location) {
searchParams['boundary.country'] =
actionContext.config.searchParams['boundary.country'];
}
actionContext.dispatch('StartReverseGeocoding');

return getJson(
actionContext.config.URL.PELIAS_REVERSE_GEOCODER,
Expand All @@ -43,41 +41,17 @@ function reverseGeocodeAddress(actionContext, location) {
});
}

const runReverseGeocodingAction = (actionContext, lat, lon) =>
actionContext.executeAction(reverseGeocodeAddress, {
lat,
lon,
});

const debouncedRunReverseGeocodingAction = debounce(
runReverseGeocodingAction,
10000,
{
leading: true,
},
);
const debouncedReverseGeocoding = debounce(reverseGeocodeAddress, 10000, {
leading: true,
});

function geoCallback(actionContext, { pos, disableDebounce }) {
actionContext.dispatch('StartReverseGeocoding');
function geoCallback(actionContext, pos) {
actionContext.dispatch('GeolocationFound', {
lat: pos.coords.latitude,
lon: pos.coords.longitude,
heading: pos.coords.heading,
disableFiltering: disableDebounce,
});
if (disableDebounce) {
runReverseGeocodingAction(
actionContext,
pos.coords.latitude,
pos.coords.longitude,
);
} else {
debouncedRunReverseGeocodingAction(
actionContext,
pos.coords.latitude,
pos.coords.longitude,
);
}
debouncedReverseGeocoding(actionContext, pos.coords);
}

function updateGeolocationMessage(actionContext, newId) {
Expand Down Expand Up @@ -115,16 +89,15 @@ function dispatchGeolocationError(actionContext, error) {

// set watcher for geolocation
function watchPosition(actionContext) {
debug('watchPosition');
const quietTimeoutSeconds = 20;

let timeout = setTimeout(() => {
actionContext.dispatch('GeolocationWatchTimeout');
updateGeolocationMessage(actionContext, 'timeout');
}, quietTimeoutSeconds * 1000);
try {
geoWatchId = navigator.geoapi.watchPosition(
(position, disableDebounce) => {
geoWatchId = navigator.geolocation.watchPosition(
position => {
updateGeolocationMessage(actionContext);
if (timeout !== null) {
clearTimeout(timeout);
Expand All @@ -138,7 +111,7 @@ function watchPosition(actionContext) {
lon !== undefined &&
!Number.isNaN(lon)
) {
geoCallback(actionContext, { pos: position, disableDebounce });
geoCallback(actionContext, position);
}
},
error => {
Expand All @@ -152,6 +125,7 @@ function watchPosition(actionContext) {
},
{ enableHighAccuracy: true, timeout: 60000, maximumAge: 60000 },
);
actionContext.dispatch('storeWatchId', geoWatchId);
} catch (error) {
if (timeout !== null) {
clearTimeout(timeout);
Expand All @@ -168,15 +142,9 @@ function watchPosition(actionContext) {
/**
* Small wrapper around permission api.
* Returns a promise of checking positioning permission.
* resolving to null means there's no permission api.
*/
export function checkPositioningPermission() {
const p = new Promise(resolve => {
if (typeof window !== 'undefined' && window.mock !== undefined) {
debug('mock permission');
resolve({ state: window.mock.permission });
return;
}
if (!navigator.permissions) {
resolve({ state: 'error' });
} else {
Expand All @@ -201,44 +169,38 @@ export function checkPositioningPermission() {
});
}
});

return p;
}

function startPositioning(actionContext) {
checkPositioningPermission().then(status => {
debug('Examining permission', status);
switch (status.state) {
case 'granted':
actionContext.dispatch('GeolocationSearch');
updateGeolocationMessage(actionContext);
watchPosition(actionContext);
break;
case 'denied':
actionContext.dispatch('GeolocationDenied');
updateGeolocationMessage(actionContext, 'denied');
break;
case 'prompt':
updateGeolocationMessage(actionContext, 'prompt');
actionContext.dispatch('GeolocationSearch');
watchPosition(actionContext);
break;
default:
// browsers not supporting permission api
actionContext.dispatch('GeolocationSearch');
watchPosition(actionContext);
break;
}
});
}
let watchPending;

/* starts location watch */
export function startLocationWatch(actionContext) {
if (typeof geoWatchId === 'undefined') {
debug('starting...');
startPositioning(actionContext); // from geolocation.js
} else {
debug('already started...');
if (typeof geoWatchId === 'undefined' && !watchPending) {
watchPending = true;
checkPositioningPermission().then(status => {
switch (status.state) {
case 'granted':
actionContext.dispatch('GeolocationSearch');
updateGeolocationMessage(actionContext);
watchPosition(actionContext);
break;
case 'denied':
actionContext.dispatch('GeolocationDenied');
updateGeolocationMessage(actionContext, 'denied');
break;
case 'prompt':
updateGeolocationMessage(actionContext, 'prompt');
actionContext.dispatch('GeolocationSearch');
watchPosition(actionContext);
break;
default:
// browsers not supporting permission api
actionContext.dispatch('GeolocationSearch');
watchPosition(actionContext);
break;
}
});
}
}

Expand Down
20 changes: 0 additions & 20 deletions app/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { ReactRelayContext } from 'react-relay';
import { setRelayEnvironment } from '@digitransit-search-util/digitransit-search-util-query-utils';
import { configShape } from './util/shapes';
import { historyMiddlewares, render } from './routes';
import Raven from './util/Raven';
import configureMoment from './util/configure-moment';
import StoreListeningIntlProvider from './util/StoreListeningIntlProvider';
import appCreator from './app';
Expand All @@ -48,29 +47,11 @@ import {
fetchFavouritesComplete,
} from './action/FavouriteActions';

const plugContext = f => () => ({
plugComponentContext: f,
plugActionContext: f,
plugStoreContext: f,
});

window.debug = debug; // Allow _debug.enable('*') in browser console

// TODO: this is an ugly hack, but required due to cyclical processing in app
const { config } = window.state.context.plugins['extra-context-plugin'];
const app = appCreator(config);
const raven = Raven(config.SENTRY_DSN);
const addRaven = c => {
c.raven = raven; // eslint-disable-line no-param-reassign
};

const ravenPlugin = {
name: 'RavenPlugin',
plugContext: plugContext(addRaven),
};

// Add plugins
app.plug(ravenPlugin);

const getParams = query => {
if (!query) {
Expand Down Expand Up @@ -252,7 +233,6 @@ async function init() {

const ContextProvider = provideContext(StoreListeningIntlProvider, {
/* eslint-disable-next-line */
raven: PropTypes.object,
config: configShape,
headers: PropTypes.objectOf(PropTypes.string),
});
Expand Down
16 changes: 1 addition & 15 deletions app/component/ErrorBoundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@ import isRelayNetworkError from '../util/relayUtils';
export default class ErrorBoundary extends React.Component {
static propTypes = { children: PropTypes.node.isRequired };

static contextTypes = {
raven: PropTypes.shape({
captureException: PropTypes.func.isRequired,
}),
};

state = { error: null, hasRetried: false };

resetState = () => this.setState({ error: null, hasRetried: true });

componentDidCatch(error, errorInfo) {
componentDidCatch(error) {
if (this.state.hasRetried) {
// Did retry, didn't help
window.location.reload();
return;
}
this.setState({ error });
if (this.context.raven && !isRelayNetworkError(error)) {
this.context.raven.captureException(error, { extra: errorInfo });
}
}

render() {
Expand All @@ -49,11 +40,6 @@ export default class ErrorBoundary extends React.Component {
<button type="button" onClick={this.resetState}>
<FormattedMessage id="try-again" defaultMessage="Try again ›" />
</button>
{/*
<button onClick(() => this.context.raven.lastEventId() && this.context.raven.showReportDialog())>
<FormattedMessage id="tell-us-what-happened" defaultMessage="Tell us what happened" />
</button>
*/}
</p>
</div>
);
Expand Down
Loading

0 comments on commit 4d29394

Please sign in to comment.