Skip to content

Commit

Permalink
Catch Mapkit API request failures & show error (#29356)
Browse files Browse the repository at this point in the history
* 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 e426f62
Merge: 0f2a0f4 0712aed
Author: Tim Broddin <[email protected]>
Date:   Wed Mar 8 12:37:40 2023 +0100

    Merge branch 'trunk' into add/map-block-mapkit

commit 0712aed
Author: thingalon <[email protected]>
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 <[email protected]>

commit 12fbcf8
Author: Nauris Pūķis <[email protected]>
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 0dcad49
Author: Francesco Bigiarini <[email protected]>
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 cf08052
Author: thingalon <[email protected]>
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 <[email protected]>

commit 318799a
Author: Nate Weller <[email protected]>
Date:   Tue Mar 7 22:44:08 2023 -0700

    Protect: Display "All threats" instead of "All 1 threats" (#29327)

commit 39f711e
Author: Samiff <[email protected]>
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 e480211
Author: Matthew Reishus <[email protected]>
Date:   Tue Mar 7 14:35:16 2023 -0600

    media summary: write to memo when no images found (#29326)

commit 177950e
Author: Jason Johnston <[email protected]>
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 <[email protected]>

    * Move hook to a directory

    Add readme

    * Update VideoPress version

    * Fix package path

    ---------

    Co-authored-by: jhnstn <[email protected]>
    Co-authored-by: Damián Suárez <[email protected]>

commit dff2d71
Author: Nate Weller <[email protected]>
Date:   Tue Mar 7 11:33:25 2023 -0700

    Protect: More Descriptive Error Messages (#29185)

commit fc9b6b5
Author: Nate Weller <[email protected]>
Date:   Tue Mar 7 11:14:12 2023 -0700

    WAF: Minor Error Handling Improvements (#29108)

commit 128facd
Author: Damián Suárez <[email protected]>
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 34dc9c3
Author: Grant Kinney <[email protected]>
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 76990eb
Author: Kuba Birecki <[email protected]>
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 <[email protected]>
  • Loading branch information
TimBroddin and mashikag authored Mar 9, 2023
1 parent 096a0cb commit 6a6ff41
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Show error message when fetching Mapkit API key fails
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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' );
Expand All @@ -27,6 +27,12 @@ const MapkitComponent = forwardRef( ( props, mapRef ) => {
}
} );

useEffect( () => {
if ( error ) {
onError( 'mapkit_error', error );
}
}, [ error, onError ] );

return (
<MapkitProvider
value={ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,22 @@ function fetchMapkitKey( mapkitObj, blogId, currentWindow ) {
currentWindow.mapkitIsInitialized = false;
mapkitObj.init( {
authorizationCallback: async done => {
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();
},
} );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 );
}
);
} );
}
}
Expand Down

0 comments on commit 6a6ff41

Please sign in to comment.