[openstreetmap/openstreetmap-website] Image download with user feedback (PR #6192)
mmd
notifications at github.com
Mon Jul 21 19:10:01 UTC 2025
@mmd-osm commented on this pull request.
> + async function handleExportSuccess(fetchResponse) {
+ try {
+ const blob = await fetchResponse.response.blob();
+ const filename = OSM.i18n.t("javascripts.share.filename");
+ downloadBlob(blob, filename);
+ } catch (err) {
+ // eslint-disable-next-line no-alert
+ alert(OSM.i18n.t("javascripts.share.export_failed", { reason: "(blob error)" }));
+ }
+ }
+
+ async function handleExportError(event) {
+ let detailMessage;
+ try {
+ detailMessage = event?.detail?.error?.message;
+ if (!detailMessage) {
+ const responseText = await event.detail.fetchResponse.responseText;
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(responseText, "text/html");
+ detailMessage = doc.body ? doc.body.textContent.trim() : "(unknown)";
+ }
+ } catch (err) {
+ detailMessage = "(unknown)";
+ }
+ // eslint-disable-next-line no-alert
+ alert(OSM.i18n.t("javascripts.share.export_failed", { reason: detailMessage }));
+ }
+
+ $("#export-image").on("turbo:submit-end", async function (event) {
+ if (event.detail.success) {
+ await handleExportSuccess(event.detail.fetchResponse);
+ } else {
+ await handleExportError(event);
+ }
+ });
I have to say that I find async/await much more readable than Promise pipelines. Also, your example is mixing up error path and success path in one event handler, which I tried to avoid by splitting it up in different functions.
So while your code is a bit shorter, I'd prefer to stick to the current version in this case.
--
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/6192#discussion_r2220026915
You are receiving this because you are subscribed to this thread.
Message ID: <openstreetmap/openstreetmap-website/pull/6192/review/3039360440 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20250721/e9c805f4/attachment.htm>
More information about the rails-dev
mailing list