public/javascripts/index.js in pact_broker-2.83.0 vs public/javascripts/index.js in pact_broker-2.84.0

- old
+ new

@@ -1,26 +1,15 @@ $(document).ready(function() { - $(".integration-settings") - .materialMenu("init", { + $(".integration-settings").click(function() { + const clickedElementData = $(this).closest("tr").data(); + $(this).materialMenu("init", { position: "overlay", animationSpeed: 1, - items: [ - { - type: "normal", - text: "Delete pacts ...", - click: handleDeletePactsSelected - }, - { - type: "normal", - text: "Delete integration...", - click: handleDeleteIntegrationsSelected - } - ] - }) - .click(function() { - $(this).materialMenu("open"); + items: buildMaterialMenuItems(clickedElementData) }); + $(this).materialMenu("open"); + }); }); function createPactDeletionConfirmationText(rowData) { return `This will delete all versions of the pact between ${ rowData.consumerName @@ -33,10 +22,20 @@ return `This will delete ${rowData.consumerName} and ${ rowData.providerName }, and all associated data (pacts, verifications, application versions, tags and webhooks) that are not associated with other integrations. Do you wish to continue?`; } +function createPactTagDeletionConfirmationText({ + providerName, + consumerName, + pactTagName +}) { + return `This will delete the pacts for provider ${providerName} and all versions of ${ + consumerName + } with tag ${pactTagName}. Do you wish to continue?`; +} + function handleDeletePactsSelected(clickedElement) { const tr = $(clickedElement).closest("tr"); const confirmationText = createPactDeletionConfirmationText(tr.data()); handleDeleteResourcesSelected( tr, @@ -49,18 +48,53 @@ const tr = $(clickedElement).closest("tr"); const confirmationText = createIntegrationDeletionConfirmationText(tr.data()); handleDeleteResourcesSelected(tr, tr.data().integrationUrl, confirmationText); } -function findRowsToBeDeleted(table, consumerName, providerName) { - return table - .children("tbody") - .find( - `[data-consumer-name="${consumerName}"][data-provider-name="${providerName}"]` +function handleDeleteTagSelected({ + providerName, + consumerName, + pactTagName, + deletionUrl +}) { + return function(clickedElement) { + const tr = $(clickedElement).closest("tr"); + const confirmationText = createPactTagDeletionConfirmationText({ + providerName, + consumerName, + pactTagName + }); + handleDeleteResourcesSelected( + tr, + deletionUrl, + confirmationText, + pactTagName ); + }; } +function findRowsToBeDeleted(table, consumerName, providerName, tagName) { + if (!tagName) { + return table + .children("tbody") + .find( + `[data-consumer-name="${consumerName}"][data-provider-name="${providerName}"]` + ); + } + + return table + .children("tbody") + .find("tr") + .find("td") + .filter(function() { + return $(this) + .text() + .includes(`tag: ${tagName}`); + }) + .closest("tr"); +} + function highlightRowsToBeDeleted(rows) { rows.children("td").addClass("to-be-deleted"); } function unHighlightRows(rows) { @@ -85,32 +119,42 @@ cancel: cancelCallback } }); } -function handleDeleteResourcesSelected(row, deletionUrl, confirmationText) { +function handleDeleteResourcesSelected( + row, + deletionUrl, + confirmationText, + tagName +) { const rowData = row.data(); const rows = findRowsToBeDeleted( row.closest("table"), rowData.consumerName, - rowData.providerName + rowData.providerName, + tagName ); + const isRefreshingThePage = !!tagName; const cancelled = function() { unHighlightRows(rows); }; const confirmed = function() { deleteResources( deletionUrl, function() { - handleDeletionSuccess(rows); + handleDeletionSuccess(rows, isRefreshingThePage); }, function(response) { handleDeletionFailure(rows, response); } ); }; - highlightRowsToBeDeleted(rows); + + if (!isRefreshingThePage) { + highlightRowsToBeDeleted(rows); + } confirmDeleteResources(confirmationText, confirmed, cancelled); } function hideDeletedRows(rows) { rows @@ -123,23 +167,38 @@ .closest("tr") .remove(); }); } -function handleDeletionSuccess(rows) { +function refreshPage() { + const url = new URL(window.location); + url.searchParams.delete("search"); + window.location = url.toString(); +} + +function handleDeletionSuccess(rows, isRefreshingThePage) { + if (isRefreshingThePage) { + return refreshPage(); + } + hideDeletedRows(rows); } function createErrorMessage(responseBody) { - if (responseBody && responseBody.error && responseBody.error.message && responseBody.error.reference) { - return `<p>Could not delete resources due to error: ${ - responseBody.error.message - }</p><p>Error reference: + if ( + responseBody && + responseBody.error && + responseBody.error.message && + responseBody.error.reference + ) { + return `<p>Could not delete resources due to error: ${responseBody.error.message}</p><p>Error reference: ${responseBody.error.reference} </p>`; } else if (responseBody) { - return `Could not delete resources due to error: ${JSON.stringify(responseBody)}`; + return `Could not delete resources due to error: ${JSON.stringify( + responseBody + )}`; } return "Could not delete resources."; } @@ -157,13 +216,49 @@ dataType: "json", type: "delete", accepts: { text: "application/hal+json" }, - success: function(data, textStatus, jQxhr) { + success: function() { successCallback(); }, - error: function(jqXhr, textStatus, errorThrown) { + error: function(jqXhr) { errorCallback(jqXhr.responseJSON); } }); +} + +function buildMaterialMenuItems(clickedElementData) { + const baseOptions = [ + { + type: "normal", + text: "Delete pacts ...", + click: handleDeletePactsSelected + }, + { + type: "normal", + text: "Delete integration...", + click: handleDeleteIntegrationsSelected + } + ]; + + const taggedPacts = clickedElementData.taggedPacts || []; + const providerName = clickedElementData.providerName; + const consumerName = clickedElementData.consumerName; + const taggedPactsOptions = taggedPacts.map(taggedPact => { + const taggedPactObject = JSON.parse(taggedPact); + const pactTagName = taggedPactObject.tag; + const taggedPactUrl = taggedPactObject.deletionUrl; + return { + type: "normal", + text: `Delete pacts for ${pactTagName}...`, + click: handleDeleteTagSelected({ + providerName, + consumerName, + pactTagName, + deletionUrl: taggedPactUrl + }) + }; + }); + + return [...baseOptions, ...taggedPactsOptions]; }