Skip to content
Merged
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
30 changes: 28 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,28 @@ export default function MapsBlockAppleEdit(props) {
const { updateAuthenticationStatus } = useDispatch(mapsBlockAppleStore);
const { toggleSelection } = useDispatch(blockEditorStore);

/**
* Check if the credentials exist.
*/
const checkCredentials = async () => {
try {
const response = await apiFetch({
path: 'MapsBlockApple/v1/GetJWT',
});
if (response) {
return true;
}

setIsLoading(false);
updateAuthenticationStatus(false);
return false;
} 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 +167,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