Skip to content

Commit

Permalink
Show reset button if user can't able to create site
Browse files Browse the repository at this point in the history
  • Loading branch information
bafi committed Aug 5, 2024
1 parent a3761ee commit 22dcc2f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
10 changes: 1 addition & 9 deletions admin/templates/partials/error-message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@
<div class="flex items-center gap-2.5">
<img src="<?php echo esc_url(PCC_PLUGIN_DIR_URL . 'assets/images/diamond-exclamation.png') ?>"
alt="Diamond exclamation icon">
<p id="pcc-error-text" class="text-sm text-black"></p>
<div id="pcc-error-text"></div>
</div>
<div class="flex items-center gap-4">
<!-- <div>-->
<!-- <a class="secondary-button" href="">--><?php
// esc_html_e(
// 'Action',
// PCC_HANDLE
// )
// ?><!--</a>-->
<!-- </div>-->
<button id="pcc-error-close-button">
<img src="<?php echo esc_url(PCC_PLUGIN_DIR_URL . 'assets/images/close-icon.png') ?>"
alt="Close icon">
Expand Down
21 changes: 7 additions & 14 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {deleteConfigDetails, redirectToMainPage} from "./helper";
import {deleteConfigDetails, PccDisconnect, redirectToMainPage} from "./helper";
import createSite from "./createSite";
import {hideErrorMessage, hideSpinner, showErrorMessage, showSpinner, updateSpinnerText} from "./helper";
import updatePostType from "./updatePostType";
Expand All @@ -24,7 +24,10 @@ if (document.getElementById('pcc-create-site') != undefined) {
await createSite();
redirectToMainPage();
} catch (error) {
showErrorMessage(`Error while creating site: ${error.message}`)
showErrorMessage([
`Error while creating site: ${error.response.data}`,
'Your management token might be restricted or you might have to tried to authenticate with a gmail.com account'
], true)
hideSpinner();
}
});
Expand All @@ -36,24 +39,14 @@ if (document.getElementById('pcc-update-collection') != undefined) {
await updatePostType();
redirectToMainPage();
} catch (error) {
showErrorMessage(`Error while creating site: ${error.message}`)
showErrorMessage(`Error while creating site: ${error.response.data}`)
} finally {
}
});
}

if (document.getElementById('pcc-disconnect') != undefined) {
document.getElementById('pcc-disconnect').addEventListener('click', async function () {
try {
showSpinner();
updateSpinnerText('Disconnecting your collection...')
await deleteConfigDetails();
redirectToMainPage();
} catch (error) {
showErrorMessage(`Error while disconnecting: ${error.message}`)
hideSpinner();
}
});
document.getElementById('pcc-disconnect').addEventListener('click', PccDisconnect);
}

if (document.getElementById('pcc-error-close-button') != undefined) {
Expand Down
37 changes: 35 additions & 2 deletions assets/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,32 @@ export function getAccessToken() {
* Show error message
* @param message
*/
export function showErrorMessage(message) {
export function showErrorMessage(messages, showResetLink = false) {
const errorMessageContainer = document.getElementById('pcc-error-message');
const errorText = document.getElementById('pcc-error-text');
if(typeof messages === 'string') {
messages = [messages];
}
if (errorMessageContainer && errorText) {
errorText.textContent = message || 'Error:please try again later';
errorText.innerHTML = '';
for (let index in messages) {
if (index !== "0") {
errorText.appendChild(document.createElement("br"));
}
let pTag = document.createElement("p");
pTag.className = "text-sm text-black";
pTag.textContent = messages[index];
errorText.appendChild(pTag);
}
if (showResetLink) {
let resetLink = document.createElement("a");
resetLink.className = "text-red-600 font-semibold";
resetLink.textContent = 'Click here to reset Google Workspace authentication.';
resetLink.id = 'pcc-disconnect';
resetLink.href = '#';
resetLink.onclick = PccDisconnect;
errorText.appendChild(resetLink);
}
errorMessageContainer.classList.remove('hidden');
}
}
Expand Down Expand Up @@ -87,3 +108,15 @@ export const deleteConfigDetails = async () => {

return resp
};

export const PccDisconnect = async () => {
try {
showSpinner();
updateSpinnerText('Disconnecting your collection...')
await deleteConfigDetails();
redirectToMainPage();
} catch (error) {
showErrorMessage(`Error while disconnecting: ${error.response.data}`)
hideSpinner();
}
}

0 comments on commit 22dcc2f

Please sign in to comment.