54 lines
2.6 KiB
JavaScript
54 lines
2.6 KiB
JavaScript
function createServiceRequestsAction({ messageTypes, requestConsumerTokenServiceOnce, sendRuntimeMessage,
|
|
appendBackgroundRequestLog, openServiceRequestsModal, environment }) {
|
|
const { document } = environment;
|
|
const pending = new Map();
|
|
|
|
async function load(partyNumber) {
|
|
const authorizations = await requestConsumerTokenServiceOnce();
|
|
const response = await sendRuntimeMessage({
|
|
type: messageTypes.requestCustomerServiceRequest, partyNumber,
|
|
authorizations: { accountSummary: authorizations.accountSummary, customerServiceRequests: authorizations.customerServiceRequests }
|
|
});
|
|
appendBackgroundRequestLog(response?.debugRequests);
|
|
if (!response?.ok) throw new Error(response?.error || "Unable to load service requests.");
|
|
return response.items;
|
|
}
|
|
|
|
function createServiceRequestsButton(opportunity) {
|
|
const button = document.createElement("button");
|
|
button.type = "button";
|
|
button.className = "opportunities-extension-action-button";
|
|
button.title = "View customer service requests";
|
|
button.setAttribute("aria-label", `View service requests for ${opportunity.CustomerAccount?.PartyUniqueName || "customer"}`);
|
|
const icon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
icon.setAttribute("viewBox", "0 0 24 24");
|
|
icon.setAttribute("aria-hidden", "true");
|
|
icon.setAttribute("focusable", "false");
|
|
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
path.setAttribute("d", "M4 14v-3a8 8 0 0 1 16 0v3 M4 11H3v7h4v-7H4 M20 11h1v7h-4v-7h3 M20 18v1a2 2 0 0 1-2 2h-6");
|
|
path.setAttribute("fill", "none");
|
|
path.setAttribute("stroke", "currentColor");
|
|
path.setAttribute("stroke-width", "1.7");
|
|
path.setAttribute("stroke-linecap", "round");
|
|
path.setAttribute("stroke-linejoin", "round");
|
|
icon.append(path);
|
|
button.append(icon);
|
|
button.addEventListener("click", async () => {
|
|
const view = openServiceRequestsModal(opportunity, button);
|
|
const partyNumber = String(opportunity.CustomerAccount?.PartyNumber || "");
|
|
try {
|
|
if (!partyNumber) throw new Error("Customer Party Number is unavailable.");
|
|
if (!pending.has(partyNumber)) {
|
|
const request = load(partyNumber).finally(() => { if (pending.get(partyNumber) === request) pending.delete(partyNumber); });
|
|
pending.set(partyNumber, request);
|
|
}
|
|
view.render(await pending.get(partyNumber));
|
|
} catch (error) {
|
|
view.render([], error.message || "Unable to load service requests.");
|
|
}
|
|
});
|
|
return button;
|
|
}
|
|
return { createServiceRequestsButton, resetRequests: () => pending.clear() };
|
|
}
|