Skip to content

Commit

Permalink
Fix block crash when Apple Maps credentials are missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
elvismdev committed Jan 27, 2025
1 parent 64daec9 commit 000fc8c
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import apiFetch from '@wordpress/api-fetch';

import { AppleMapEdit } from './components/AppleMap';
import EditAuthForm from './components/EditAuthForm';
Expand Down Expand Up @@ -75,6 +76,33 @@ export default function MapsBlockAppleEdit(props) {
const { updateAuthenticationStatus } = useDispatch(mapsBlockAppleStore);
const { toggleSelection } = useDispatch(blockEditorStore);

/**
* Check if the credentials exist.
*/
const checkCredentials = async () => {
try {
const settings = await apiFetch({ path: 'wp/v2/settings' });
const hasCredentials =
settings.maps_block_apple &&
settings.maps_block_apple.private_key &&
settings.maps_block_apple.key_id &&
settings.maps_block_apple.team_id;

if (!hasCredentials) {
setIsLoading(false);
updateAuthenticationStatus(false);
return;
}

// Credentials exist, proceed with normal initialization
return true;
} catch (error) {
setIsLoading(false);
updateAuthenticationStatus(false);
return false;
}
};

/**
* setup the initial authentication of mapkit and setup all the event listeners
*
Expand Down Expand Up @@ -144,8 +172,11 @@ export default function MapsBlockAppleEdit(props) {
/**
* handleMapkitReInitialization
*/
const InitializeMapkit = () => {
AppleMapEdit.authenticateMap(localMapkit);
const InitializeMapkit = async () => {
const hasValidCredentials = await checkCredentials();
if (hasValidCredentials) {
AppleMapEdit.authenticateMap(localMapkit);
}
};
localMapkit.addEventListener('reinitialize', InitializeMapkit);

Expand Down

0 comments on commit 000fc8c

Please sign in to comment.