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

Make GPX name attribute string translatable #5985

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions frontend/src/components/editor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import React, { useEffect, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { useIntl } from 'react-intl';
import { gpx } from '@tmcw/togeojson';
import * as iD from '@hotosm/id';
import '@hotosm/id/dist/iD.css';

import { OSM_CLIENT_ID, OSM_CLIENT_SECRET, OSM_REDIRECT_URI, OSM_SERVER_URL } from '../config';
import messages from './messages';

export default function Editor({ setDisable, comment, presets, imagery, gpxUrl }) {
const dispatch = useDispatch();
const intl = useIntl();
const session = useSelector((state) => state.auth.session);
const iDContext = useSelector((state) => state.editor.context);
const locale = useSelector((state) => state.preferences.locale);
Expand Down Expand Up @@ -82,7 +86,20 @@ export default function Editor({ setDisable, comment, presets, imagery, gpxUrl }
iDContext.init();
}
if (gpxUrl) {
iDContext.layers().layer('data').url(gpxUrl, '.gpx');
fetch(gpxUrl)
.then((response) => response.text())
.then((data) => {
let gpxData = new DOMParser().parseFromString(data, 'text/xml');
let nameNode = gpxData.getElementsByTagName('trk')[0].childNodes[0];
let projectId = nameNode.textContent.match(/\d+/g);
nameNode.textContent = intl.formatMessage(messages.gpxNameAttribute, {
projectId: projectId[0],
});
iDContext.layers().layer('data').geojson(gpx(gpxData));
})
.catch((error) => {
console.error('Error loading GPX data');
});
}

let osm = iDContext.connection();
Expand All @@ -106,7 +123,7 @@ export default function Editor({ setDisable, comment, presets, imagery, gpxUrl }
}
});
}
}, [session, iDContext, setDisable, presets, locale, gpxUrl]);
}, [session, iDContext, setDisable, presets, locale, gpxUrl, intl]);

return <div className="w-100 vh-minus-69-ns" id="id-container"></div>;
}
4 changes: 4 additions & 0 deletions frontend/src/components/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ export default defineMessages({
id: 'common.loading',
defaultMessage: 'Loading...',
},
gpxNameAttribute: {
id: 'editor.layer.gpx.name',
defaultMessage: 'Task for project {projectId}. Do not edit outside of this area!',
},
});
21 changes: 19 additions & 2 deletions frontend/src/components/rapidEditor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React, { useEffect, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { useIntl } from 'react-intl';
import { gpx } from '@tmcw/togeojson';
import * as RapiD from 'RapiD/dist/iD.legacy';
import 'RapiD/dist/RapiD.css';

import { OSM_CLIENT_ID, OSM_CLIENT_SECRET, OSM_REDIRECT_URI, OSM_SERVER_URL } from '../config';
import messages from './messages';

export default function RapidEditor({
setDisable,
Expand All @@ -15,6 +18,7 @@ export default function RapidEditor({
}) {
const dispatch = useDispatch();
const session = useSelector((state) => state.auth.session);
const intl = useIntl();
const RapiDContext = useSelector((state) => state.editor.rapidContext);
const locale = useSelector((state) => state.preferences.locale);
const [customImageryIsSet, setCustomImageryIsSet] = useState(false);
Expand Down Expand Up @@ -88,7 +92,20 @@ export default function RapidEditor({
RapiDContext.init();
}
if (gpxUrl) {
RapiDContext.layers().layer('data').url(gpxUrl, '.gpx');
fetch(gpxUrl)
.then((response) => response.text())
.then((data) => {
let gpxData = new DOMParser().parseFromString(data, 'text/xml');
let nameNode = gpxData.getElementsByTagName('trk')[0].childNodes[0];
let projectId = nameNode.textContent.match(/\d+/g);
nameNode.textContent = intl.formatMessage(messages.gpxNameAttribute, {
projectId: projectId[0],
});
RapiDContext.layers().layer('data').geojson(gpx(gpxData));
})
.catch((error) => {
console.error('Error loading GPX data');
});
}

RapiDContext.rapidContext().showPowerUser = powerUser;
Expand All @@ -114,7 +131,7 @@ export default function RapidEditor({
}
});
}
}, [session, RapiDContext, setDisable, presets, locale, gpxUrl, powerUser]);
}, [session, RapiDContext, setDisable, presets, locale, gpxUrl, powerUser, intl]);

return <div className="w-100 vh-minus-69-ns" id="rapid-container"></div>;
}
Loading