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

Fix block crash when Apple Maps credentials are missing. #233

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
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
Loading