Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

refactor(cloudcp): delegate Pulse Account shell actions

rcourtman69d516a7a40f
diff --git a/internal/cloudcp/portal/assets/portal.js b/internal/cloudcp/portal/assets/portal.js
index 7dea8266b8..42ddc7a7d9 100644
--- a/internal/cloudcp/portal/assets/portal.js
+++ b/internal/cloudcp/portal/assets/portal.js
@@ -56,7 +56,7 @@ function renderWorkspaceCard(account, workspace) {
var manageAction = '';
if (account.can_manage && (state === 'active' || state === 'suspended' || state === 'failed')) {
- manageAction = '<button class="btn-danger" onclick="suspendOrDelete(event,\'' + escapeAttr(account.id) + '\',\'' + escapeAttr(workspace.id) + '\',\'' + escapeAttr(state) + '\',\'' + escapeAttr(workspace.display_name) + '\')">⋯</button>';
+ manageAction = '<button type="button" class="btn-danger" data-action="workspace-manage" data-account-id="' + escapeAttr(account.id) + '" data-workspace-id="' + escapeAttr(workspace.id) + '" data-workspace-state="' + escapeAttr(state) + '" data-workspace-name="' + escapeAttr(workspace.display_name) + '">⋯</button>';
}
var createdMeta = createdLabel ? '<span class="ws-created">Created ' + escapeHTML(createdLabel) + '</span>' : '';
@@ -96,12 +96,12 @@ function renderAccountSection(account) {
if (account.can_manage) {
actions = '<div class="account-actions">' +
(account.kind === 'msp'
- ? '<button class="btn-secondary" id="add-ws-btn-' + escapeAttr(account.id) + '" onclick="toggleAddWorkspace(\'' + escapeAttr(account.id) + '\')">+ Add workspace</button>'
+ ? '<button type="button" class="btn-secondary" id="add-ws-btn-' + escapeAttr(account.id) + '" data-action="toggle-add-workspace" data-account-id="' + escapeAttr(account.id) + '">+ Add workspace</button>'
: '') +
(account.has_billing
- ? '<button class="btn-secondary" onclick="openBilling(\'' + escapeAttr(account.id) + '\')">Manage billing</button>'
+ ? '<button type="button" class="btn-secondary" data-action="open-billing" data-account-id="' + escapeAttr(account.id) + '">Manage billing</button>'
: '') +
- '<button class="btn-secondary" id="team-btn-' + escapeAttr(account.id) + '" onclick="toggleTeam(\'' + escapeAttr(account.id) + '\',\'' + escapeAttr(account.role) + '\')">Manage team</button>' +
+ '<button type="button" class="btn-secondary" id="team-btn-' + escapeAttr(account.id) + '" data-action="toggle-team" data-account-id="' + escapeAttr(account.id) + '">Manage team</button>' +
'</div>';
teamSection = '' +
@@ -116,7 +116,7 @@ function renderAccountSection(account) {
'<div class="team-invite">' +
'<div><label for="invite-email-' + escapeAttr(account.id) + '">Email</label><input type="email" id="invite-email-' + escapeAttr(account.id) + '" placeholder="[email protected]" autocomplete="off"></div>' +
'<div><label for="invite-role-' + escapeAttr(account.id) + '">Role</label><select id="invite-role-' + escapeAttr(account.id) + '"><option value="admin">Admin</option><option value="tech">Tech</option><option value="read_only">Read-only</option></select></div>' +
- '<button class="btn-primary" style="padding:8px 14px;font-size:13px" onclick="inviteMember(\'' + escapeAttr(account.id) + '\')">Invite</button>' +
+ '<button type="button" class="btn-primary" style="padding:8px 14px;font-size:13px" data-action="invite-member" data-account-id="' + escapeAttr(account.id) + '">Invite</button>' +
'</div>' +
'</div>';
@@ -126,8 +126,8 @@ function renderAccountSection(account) {
'<label for="ws-name-' + escapeAttr(account.id) + '">Workspace name (e.g. client name)</label>' +
'<input type="text" id="ws-name-' + escapeAttr(account.id) + '" placeholder="Acme Corp" maxlength="80" autocomplete="off">' +
'<div class="form-actions">' +
- '<button class="btn-primary" onclick="createWorkspace(\'' + escapeAttr(account.id) + '\')">Create workspace</button>' +
- '<button class="btn-secondary" onclick="toggleAddWorkspace(\'' + escapeAttr(account.id) + '\')">Cancel</button>' +
+ '<button type="button" class="btn-primary" data-action="create-workspace" data-account-id="' + escapeAttr(account.id) + '">Create workspace</button>' +
+ '<button type="button" class="btn-secondary" data-action="toggle-add-workspace" data-account-id="' + escapeAttr(account.id) + '">Cancel</button>' +
'<div class="spinner" id="ws-spinner-' + escapeAttr(account.id) + '"></div>' +
'</div>' +
'</div>';
@@ -208,6 +208,66 @@ document.getElementById('logout-btn').onclick = async function() {
applyBootstrap(portalBootstrap);
refreshBootstrap();
+document.addEventListener('click', function(event) {
+ var actionEl = event.target.closest('[data-action]');
+ if (!actionEl) return;
+ var action = actionEl.getAttribute('data-action') || '';
+ var accountID = actionEl.getAttribute('data-account-id') || '';
+
+ switch (action) {
+ case 'toggle-add-workspace':
+ event.preventDefault();
+ toggleAddWorkspace(accountID);
+ return;
+ case 'open-billing':
+ event.preventDefault();
+ openBilling(accountID);
+ return;
+ case 'toggle-team':
+ event.preventDefault();
+ toggleTeam(accountID);
+ return;
+ case 'invite-member':
+ event.preventDefault();
+ inviteMember(accountID);
+ return;
+ case 'create-workspace':
+ event.preventDefault();
+ createWorkspace(accountID);
+ return;
+ case 'workspace-manage':
+ event.preventDefault();
+ suspendOrDelete(
+ event,
+ accountID,
+ actionEl.getAttribute('data-workspace-id') || '',
+ actionEl.getAttribute('data-workspace-state') || '',
+ actionEl.getAttribute('data-workspace-name') || ''
+ );
+ return;
+ case 'remove-member':
+ event.preventDefault();
+ removeMember(
+ accountID,
+ actionEl.getAttribute('data-user-id') || '',
+ actionEl.getAttribute('data-member-email') || ''
+ );
+ return;
+ default:
+ return;
+ }
+});
+
+document.addEventListener('change', function(event) {
+ var target = event.target;
+ if (!target || target.getAttribute('data-action') !== 'change-role') return;
+ changeRole(
+ target.getAttribute('data-account-id') || '',
+ target.getAttribute('data-user-id') || '',
+ target.value
+ );
+});
+
function toggleServicePanel(panelID) {
var panels = ['manage-service-panel', 'retrieve-service-panel', 'refund-service-panel', 'data-service-panel'];
for (var i = 0; i < panels.length; i++) {
@@ -684,16 +744,22 @@ async function loadTeam(accountID) {
if (m.role === roles[j]) opt.selected = true;
sel.appendChild(opt);
}
- sel.addEventListener('change', function() { changeRole(accountID, m.user_id, this.value); });
+ sel.setAttribute('data-action', 'change-role');
+ sel.setAttribute('data-account-id', accountID);
+ sel.setAttribute('data-user-id', m.user_id);
tdRole.appendChild(sel);
}
tr.appendChild(tdRole);
var tdAction = document.createElement('td');
if (!(m.role === 'owner' && !isOwner)) {
var btn = document.createElement('button');
+ btn.type = 'button';
btn.className = 'btn-remove';
btn.textContent = 'Remove';
- btn.addEventListener('click', function() { removeMember(accountID, m.user_id, m.email); });
+ btn.setAttribute('data-action', 'remove-member');
+ btn.setAttribute('data-account-id', accountID);
+ btn.setAttribute('data-user-id', m.user_id);
+ btn.setAttribute('data-member-email', m.email);
tdAction.appendChild(btn);
}
tr.appendChild(tdAction);
diff --git a/internal/cloudcp/portal/handlers_test.go b/internal/cloudcp/portal/handlers_test.go
index 137f1c7981..a637ef850e 100644
--- a/internal/cloudcp/portal/handlers_test.go
+++ b/internal/cloudcp/portal/handlers_test.go
@@ -692,8 +692,9 @@ func TestPortalPageTemplate_TeamManagementRendered(t *testing.T) {
`id="team-list-a_managed"`, // member list tbody
`id="invite-email-a_managed"`, // invite email input
`id="invite-role-a_managed"`, // invite role select
- `toggleTeam('a_managed'`, // button onclick references account ID
- `inviteMember('a_managed'`,
+ `data-action="toggle-team"`,
+ `data-action="invite-member"`,
+ `data-account-id="a_managed"`,
}
for _, needle := range mustContain {
if !strings.Contains(html, needle) {
@@ -811,6 +812,11 @@ func TestPortalPageTemplate_AccountServicesRendered(t *testing.T) {
`fetch(LICENSE_API_BASE + '/v1/gdpr/confirm-delete'`,
`if (!await refreshBootstrap())`,
`refreshAccountTeamSection(accountID)`,
+ `document.addEventListener('click'`,
+ `document.addEventListener('change'`,
+ `data-action="open-billing"`,
+ `data-action="create-workspace"`,
+ `data-action="workspace-manage"`,
`href="https://pulserelay.pro/refund.html?email=owner%40example.com"`,
"commercial account actions now live here",
}
@@ -828,6 +834,9 @@ func TestPortalPageTemplate_AccountServicesRendered(t *testing.T) {
if strings.Contains(html, `showToast('Member invited!');`+"\n"+` loadTeam(accountID);`) {
t.Errorf("expected membership mutations to refresh the account bootstrap instead of only repainting the team table")
}
+ if strings.Contains(html, `onclick="`) {
+ t.Errorf("expected portal shell interactions to be delegated through data-action attributes instead of inline onclick handlers")
+ }
if strings.Contains(html, `await fetch('/auth/logout'`) {
t.Errorf("expected portal paths to be renderer-owned, not hardcoded in the asset")
}
diff --git a/internal/cloudcp/portal/templates/portal.html b/internal/cloudcp/portal/templates/portal.html
index 94dd375145..1c5462c20c 100644
--- a/internal/cloudcp/portal/templates/portal.html
+++ b/internal/cloudcp/portal/templates/portal.html
@@ -65,7 +65,7 @@
<span style="font-size:13px;color:#94a3b8">{{$ws.State}}</span>
{{end}}
{{if and $account.CanManage (or (eq $ws.State "active") (eq $ws.State "suspended") (eq $ws.State "failed"))}}
- <button class="btn-danger" onclick="suspendOrDelete(event,'{{$account.ID}}','{{$ws.ID}}','{{$ws.State}}','{{$ws.DisplayName}}')">⋯</button>
+ <button type="button" class="btn-danger" data-action="workspace-manage" data-account-id="{{$account.ID}}" data-workspace-id="{{$ws.ID}}" data-workspace-state="{{$ws.State}}" data-workspace-name="{{$ws.DisplayName}}">⋯</button>
{{end}}
</div>
</div>
@@ -76,12 +76,12 @@
{{if $account.CanManage}}
<div class="account-actions">
{{if eq $account.Kind "msp"}}
- <button class="btn-secondary" id="add-ws-btn-{{$account.ID}}" onclick="toggleAddWorkspace('{{$account.ID}}')">+ Add workspace</button>
+ <button type="button" class="btn-secondary" id="add-ws-btn-{{$account.ID}}" data-action="toggle-add-workspace" data-account-id="{{$account.ID}}">+ Add workspace</button>
{{end}}
{{if $account.HasBilling}}
- <button class="btn-secondary" onclick="openBilling('{{$account.ID}}')">Manage billing</button>
+ <button type="button" class="btn-secondary" data-action="open-billing" data-account-id="{{$account.ID}}">Manage billing</button>
{{end}}
- <button class="btn-secondary" id="team-btn-{{$account.ID}}" onclick="toggleTeam('{{$account.ID}}','{{$account.Role}}')">Manage team</button>
+ <button type="button" class="btn-secondary" id="team-btn-{{$account.ID}}" data-action="toggle-team" data-account-id="{{$account.ID}}">Manage team</button>
</div>
<div class="team-section" id="team-section-{{$account.ID}}" data-actor-role="{{$account.Role}}">
@@ -95,7 +95,7 @@
<div class="team-invite">
<div><label for="invite-email-{{$account.ID}}">Email</label><input type="email" id="invite-email-{{$account.ID}}" placeholder="[email protected]" autocomplete="off"></div>
<div><label for="invite-role-{{$account.ID}}">Role</label><select id="invite-role-{{$account.ID}}"><option value="admin">Admin</option><option value="tech">Tech</option><option value="read_only">Read-only</option></select></div>
- <button class="btn-primary" style="padding:8px 14px;font-size:13px" onclick="inviteMember('{{$account.ID}}')">Invite</button>
+ <button type="button" class="btn-primary" style="padding:8px 14px;font-size:13px" data-action="invite-member" data-account-id="{{$account.ID}}">Invite</button>
</div>
</div>
@@ -104,8 +104,8 @@
<label for="ws-name-{{$account.ID}}">Workspace name (e.g. client name)</label>
<input type="text" id="ws-name-{{$account.ID}}" placeholder="Acme Corp" maxlength="80" autocomplete="off">
<div class="form-actions">
- <button class="btn-primary" onclick="createWorkspace('{{$account.ID}}')">Create workspace</button>
- <button class="btn-secondary" onclick="toggleAddWorkspace('{{$account.ID}}')">Cancel</button>
+ <button type="button" class="btn-primary" data-action="create-workspace" data-account-id="{{$account.ID}}">Create workspace</button>
+ <button type="button" class="btn-secondary" data-action="toggle-add-workspace" data-account-id="{{$account.ID}}">Cancel</button>
<div class="spinner" id="ws-spinner-{{$account.ID}}"></div>
</div>
</div>