From 6a6ff4128dd68b2858811db2f4a89a98b535a365 Mon Sep 17 00:00:00 2001 From: Tim Broddin Date: Thu, 9 Mar 2023 21:36:22 +0100 Subject: [PATCH] Catch Mapkit API request failures & show error (#29356) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Cherry-pick hook * Cherry-pick * WIP * Markers & info window * WIP useEffect dependency cleanup * - MapRef for resizing - Better zooming for multiple markers * Some cleanup * - Disable zoom control on map when we have more than 1 point * Sane defaults when adding a map * Remove TODO * Add changelog * Merge with location search * Make check for mapRef more compact * Rename createCalloutElement to createCalloutElementCallback * Update style code styling * Remove todo and add linebreak between import & function call * Code style fix * InfoWindow renaming * Remove commented out console.log * Mapkit doesn't have id's for places returned, so create one * Fix resizing doing bad things * Load Mapkit only once * Load Mapkit only once (frontend) * Make cookie override frontend too * Changelog * Show error when Mapkit API request fails * Changelog * Friendlier error message * Squashed commit of the following: commit e426f627e8e46687b6984a3b4799fb0e0d23d83c Merge: 0f2a0f4c0f 0712aed11d Author: Tim Broddin Date: Wed Mar 8 12:37:40 2023 +0100 Merge branch 'trunk' into add/map-block-mapkit commit 0712aed11dec56d0eb3edec395724adbed246497 Author: thingalon Date: Wed Mar 8 20:00:50 2023 +1100 [Boost] Clean up linting issues in the minify feature (#29352) * Fix linting issues in minify module * changelog --------- Co-authored-by: Mark George commit 12fbcf886edb37715296d65a3359f0e8d4d9e6ce Author: Nauris Pūķis Date: Wed Mar 8 09:45:29 2023 +0200 Jetpack Boost: Add livereload (#29342) * Add livereload to the run dev script * Move livereaload functionality to "devlive" script * Add a bit of documentation 🤏 * Add a changelog commit 0dcad4905f686d3f6039e144ba106037a560014c Author: Francesco Bigiarini Date: Wed Mar 8 08:32:37 2023 +0100 Add import package media endpoints (#29080) * [not verified] Add /jetpack/v4/import/media/* endpoints * [not verified] changelog * [not verified] Fix: remove categories and tags from pages. * [not verified] Add attachment PUT method * [not verified] Fix: wrong variable name * [not verified] Add missing body action * [not verified] Update version commit cf0805218897551ae19fdfff39defa036e46c67d Author: thingalon Date: Wed Mar 8 17:54:09 2023 +1100 Add a changelog for fixing Safari lazy loading for the next boost release (#29266) Co-authored-by: Mark George commit 318799a6bcd1707bafe23ab13de46c8608f25316 Author: Nate Weller Date: Tue Mar 7 22:44:08 2023 -0700 Protect: Display "All threats" instead of "All 1 threats" (#29327) commit 39f711eb3a2dee0b82759792a7ecdc6d5d0f70b1 Author: Samiff Date: Tue Mar 7 15:10:52 2023 -0700 Update/jetpack backport 11.9 (#29345) * [not verified] Jetpack: 11.9 changelog and readme * Update stable tag to 11.9 commit e4802118bce9b018a94722af018d172448a901d9 Author: Matthew Reishus Date: Tue Mar 7 14:35:16 2023 -0600 media summary: write to memo when no images found (#29326) commit 177950e3a8217608ada9e853dead742041108d3b Author: Jason Johnston Date: Tue Mar 7 15:25:17 2023 -0500 VideoPress: Add/use preview hook (#29164) * [not verified] Add usePreview hook * [not verified] Use UsePreview hook * [not verified] Add changelog * Update projects/packages/videopress/src/client/block-editor/hooks/use-preview.ts Co-authored-by: Damián Suárez * Move hook to a directory Add readme * Update VideoPress version * Fix package path --------- Co-authored-by: jhnstn Co-authored-by: Damián Suárez commit dff2d716871a777c82912815e84de69f0a7b609c Author: Nate Weller Date: Tue Mar 7 11:33:25 2023 -0700 Protect: More Descriptive Error Messages (#29185) commit fc9b6b52d0b74829227ee5a41c960766c9f69ff2 Author: Nate Weller Date: Tue Mar 7 11:14:12 2023 -0700 WAF: Minor Error Handling Improvements (#29108) commit 128facd7c534971fe90ef4b172e4759f699b2a5b Author: Damián Suárez Date: Tue Mar 7 15:00:53 2023 -0300 VideoPress: pick and convert core/video VideoPress instances also from inner blocks (#29339) * pick v6 also from inner blocks * changelog * minor code changes * fix changelog * minor doc enhancement * ensure to include the selected v5 instance commit 34dc9c31d064411ab457e807b5dd74676f9af2ab Author: Grant Kinney Date: Tue Mar 7 09:42:49 2023 -0600 Writing prompt block: a few cleanup tasks (#29324) - Uses scss variables from @automattic/jetpack-base-styles/gutenberg-base-styles where appropriate - Ensures "View all responses" is an external link that opens in a new tab/window - Ensures "prompt" is used in css classes and the like, since a block represents a single prompt commit 76990ebc5303b8ffd813768ffb92ab2ddf8dfccd Author: Kuba Birecki Date: Tue Mar 7 15:56:37 2023 +0100 Implement RWD navigation for feedback dashboard (#29315) * [not verified] Fix RWD styles for the table and response * [not verified] Add navigation for switching between inbox and response views on mobile * Update layout component to accept custom class names * changelog * Reject when mapkitIsInitializing is undefined * Delete changelogs --------- Co-authored-by: Maciej Grabowski --- .../fix-map-block-mapkit-catch-faillure | 4 +++ .../extensions/blocks/map/component/mapkit.js | 12 +++++-- .../blocks/map/mapkit-utils/index.js | 26 ++++++++------- .../blocks/map/mapkit/hooks/index.js | 32 ++++++++++++------- 4 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/fix-map-block-mapkit-catch-faillure diff --git a/projects/plugins/jetpack/changelog/fix-map-block-mapkit-catch-faillure b/projects/plugins/jetpack/changelog/fix-map-block-mapkit-catch-faillure new file mode 100644 index 0000000000000..d024385e4240e --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-map-block-mapkit-catch-faillure @@ -0,0 +1,4 @@ +Significance: minor +Type: other + +Show error message when fetching Mapkit API key fails diff --git a/projects/plugins/jetpack/extensions/blocks/map/component/mapkit.js b/projects/plugins/jetpack/extensions/blocks/map/component/mapkit.js index d73363c93441c..22c3976b0f346 100644 --- a/projects/plugins/jetpack/extensions/blocks/map/component/mapkit.js +++ b/projects/plugins/jetpack/extensions/blocks/map/component/mapkit.js @@ -1,4 +1,4 @@ -import { Children, forwardRef, memo, useCallback, useRef } from '@wordpress/element'; +import { Children, forwardRef, memo, useCallback, useEffect, useRef } from '@wordpress/element'; import { get } from 'lodash'; import { MapkitProvider } from '../mapkit/context'; import { @@ -17,8 +17,8 @@ import { createCalloutElementCallback } from '../mapkit-utils'; import InfoWindow from './info-window'; const MapkitComponent = forwardRef( ( props, mapRef ) => { - const { admin, points, onSetPoints } = props; - const { loaded, mapkit, currentDoc, currentWindow } = useMapkitSetup( mapRef ); + const { admin, points, onError, onSetPoints } = props; + const { loaded, error, mapkit, currentDoc, currentWindow } = useMapkitSetup( mapRef ); const { map } = useMapkitInit( mapkit, loaded, mapRef ); const addPoint = Children.map( props.children, child => { const tagName = get( child, 'props.tagName' ); @@ -27,6 +27,12 @@ const MapkitComponent = forwardRef( ( props, mapRef ) => { } } ); + useEffect( () => { + if ( error ) { + onError( 'mapkit_error', error ); + } + }, [ error, onError ] ); + return ( { - const response = await fetch( - `https://public-api.wordpress.com/wpcom/v2/sites/${ blogId }/mapkit` - ); - if ( response.status === 200 ) { - const data = await response.json(); - done( data.wpcom_mapkit_access_token ); - } else { - reject( 'Mapkit API error' ); + try { + const response = await fetch( + `https://public-api.wordpress.com/wpcom/v2/sites/${ blogId }/mapkit` + ); + if ( response.status === 200 ) { + const data = await response.json(); + done( data.wpcom_mapkit_access_token ); + } else { + reject(); + } + currentWindow.mapkitIsInitializing = false; + currentWindow.mapkitIsInitialized = true; + resolve(); + } catch ( error ) { + reject(); } - currentWindow.mapkitIsInitializing = false; - currentWindow.mapkitIsInitialized = true; - resolve(); }, } ); } diff --git a/projects/plugins/jetpack/extensions/blocks/map/mapkit/hooks/index.js b/projects/plugins/jetpack/extensions/blocks/map/mapkit/hooks/index.js index 8954770d13d6d..a87e5c8f25d9d 100644 --- a/projects/plugins/jetpack/extensions/blocks/map/mapkit/hooks/index.js +++ b/projects/plugins/jetpack/extensions/blocks/map/mapkit/hooks/index.js @@ -1,6 +1,7 @@ import { CONNECTION_STORE_ID } from '@automattic/jetpack-connection'; import { select } from '@wordpress/data'; import { useContext, useEffect, useRef, useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; import { debounce } from 'lodash'; import { getLoadContext } from '../../../../shared/block-editor-asset-loader'; import { @@ -38,29 +39,36 @@ const useMapkitSetup = mapRef => { setCurrentWindow( currentWindow ); setCurrentDoc( currentDoc ); + const fetchMapkitKeyErrorMessage = __( + 'Failed to retrieve a Mapkit API token. Please try refreshing.', + 'jetpack' + ); + // If mapkit is already loaded, reuse it. if ( currentWindow.mapkit ) { setMapkit( currentWindow.mapkit ); // Fetch API key in the off chance that mapkit is available but not initialized for some reason // It will just resolve in case it is already initialized. - fetchMapkitKey( currentWindow.mapkit, blog_id, currentWindow ) - .then( () => { + fetchMapkitKey( currentWindow.mapkit, blog_id, currentWindow ).then( + () => { setLoaded( true ); - } ) - .catch( e => { - setError( e ); - } ); + }, + () => { + setError( fetchMapkitKeyErrorMessage ); + } + ); } else { loadMapkitLibrary( currentDoc, currentWindow ).then( mapkitObj => { setMapkit( mapkitObj ); - fetchMapkitKey( mapkitObj, blog_id, currentWindow ) - .then( () => { + fetchMapkitKey( mapkitObj, blog_id, currentWindow ).then( + () => { setLoaded( true ); - } ) - .catch( e => { - setError( e ); - } ); + }, + () => { + setError( fetchMapkitKeyErrorMessage ); + } + ); } ); } }