Skip to content

Commit c368adc

Browse files
authored
Merge pull request #233 from 10up/fix/232-Block_crashes_without_apple_maps_credentials
Fix block crash when Apple Maps credentials are missing.
2 parents 23ff657 + 448fdb3 commit c368adc

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/edit.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
store as blockEditorStore,
1414
} from '@wordpress/block-editor';
1515
import { useSelect, useDispatch } from '@wordpress/data';
16+
import apiFetch from '@wordpress/api-fetch';
1617

1718
import { AppleMapEdit } from './components/AppleMap';
1819
import EditAuthForm from './components/EditAuthForm';
@@ -75,6 +76,28 @@ export default function MapsBlockAppleEdit(props) {
7576
const { updateAuthenticationStatus } = useDispatch(mapsBlockAppleStore);
7677
const { toggleSelection } = useDispatch(blockEditorStore);
7778

79+
/**
80+
* Check if the credentials exist.
81+
*/
82+
const checkCredentials = async () => {
83+
try {
84+
const response = await apiFetch({
85+
path: 'MapsBlockApple/v1/GetJWT',
86+
});
87+
if (response) {
88+
return true;
89+
}
90+
91+
setIsLoading(false);
92+
updateAuthenticationStatus(false);
93+
return false;
94+
} catch (error) {
95+
setIsLoading(false);
96+
updateAuthenticationStatus(false);
97+
return false;
98+
}
99+
};
100+
78101
/**
79102
* setup the initial authentication of mapkit and setup all the event listeners
80103
*
@@ -144,8 +167,11 @@ export default function MapsBlockAppleEdit(props) {
144167
/**
145168
* handleMapkitReInitialization
146169
*/
147-
const InitializeMapkit = () => {
148-
AppleMapEdit.authenticateMap(localMapkit);
170+
const InitializeMapkit = async () => {
171+
const hasValidCredentials = await checkCredentials();
172+
if (hasValidCredentials) {
173+
AppleMapEdit.authenticateMap(localMapkit);
174+
}
149175
};
150176
localMapkit.addEventListener('reinitialize', InitializeMapkit);
151177

0 commit comments

Comments
 (0)