Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/components/dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h3>{{ ws.name }}</h3>
<small>Created: {{ ws.createdDate | date: 'mediumDate' }}</small>
<div class="card-actions">
<a [routerLink]="['/workspaces', ws.id, 'decisions']" class="action-btn open-btn">Open</a>
<a [routerLink]="['/workspaces', ws.id, 'edit']" class="action-btn edit-btn">Edit</a>
<a *ngIf="canEditWorkspace(ws)" [routerLink]="['/workspaces', ws.id, 'edit']" class="action-btn edit-btn">Edit</a>
<button type="button" class="action-btn delete-btn" (click)="deleteWorkspace(ws)">Delete</button>
</div>
</article>
Expand Down
21 changes: 20 additions & 1 deletion src/app/components/dashboard/dashboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('Dashboard', () => {
};

mockAuthService = {
logout: jasmine.createSpy('logout')
logout: jasmine.createSpy('logout'),
getCurrentUserId: jasmine.createSpy('getCurrentUserId').and.returnValue('u1')
};

mockSignalService = {
Expand Down Expand Up @@ -91,6 +92,24 @@ describe('Dashboard', () => {
expect(compiled.querySelector('.workspace-card h3')?.textContent).toContain('Test Workspace');
});

it('should hide edit action for workspaces not owned by the current user', () => {
mockWorkspaceService.getWorkspaces.and.returnValue(of([
{ id: '1', name: 'Owned Workspace', description: 'Owned Desc', createdDate: new Date(), ownerId: 'u1' },
{ id: '2', name: 'Shared Workspace', description: 'Shared Desc', createdDate: new Date(), ownerId: 'u2' }
]));

fixture = TestBed.createComponent(Dashboard);
component = fixture.componentInstance;
fixture.detectChanges();

const cards = Array.from(fixture.nativeElement.querySelectorAll('.workspace-card')) as HTMLElement[];
const ownedCard = cards.find(card => card.textContent?.includes('Owned Workspace'));
const sharedCard = cards.find(card => card.textContent?.includes('Shared Workspace'));

expect(ownedCard?.textContent).toContain('Edit');
expect(sharedCard?.textContent).not.toContain('Edit');
});

it('should call logout on button click', () => {
const button = fixture.nativeElement.querySelector('.logout-btn');
button.click();
Expand Down
9 changes: 9 additions & 0 deletions src/app/components/dashboard/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ export class Dashboard implements OnInit {
filters: SignalFilters = { source: 'all', status: 'all' };
githubBanner = '';
slackBanner = '';
currentUserId: string | null = null;

ngOnInit() {
this.currentUserId = this.authService.getCurrentUserId();

this.workspaceService.getWorkspaces().subscribe({
next: ws => {
this.workspaces = ws;
Expand All @@ -57,6 +60,10 @@ export class Dashboard implements OnInit {
this.loadSignals();
}

canEditWorkspace(workspace: Workspace): boolean {
return this.currentUserId !== null && workspace.ownerId === this.currentUserId;
}

deleteWorkspace(workspace: Workspace) {
const confirmed = window.confirm(`Delete workspace "${workspace.name}"?`);
if (!confirmed) {
Expand All @@ -68,6 +75,7 @@ export class Dashboard implements OnInit {
return;
}
this.workspaces = this.workspaces.filter(ws => ws.id !== workspace.id);
this.cdr.detectChanges();
});
}

Expand Down Expand Up @@ -97,6 +105,7 @@ export class Dashboard implements OnInit {
private loadSignals(): void {
this.signalService.getSignals(this.filters).subscribe(signals => {
this.signals = signals;
this.cdr.detectChanges();
});
}
}
2 changes: 1 addition & 1 deletion src/app/components/workspace/edit-workspace.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2>Edit Workspace</h2>
<span>Loading workspace...</span>
</div>

<form *ngIf="!isLoading && !error" (ngSubmit)="onSubmit()" #workspaceForm="ngForm" novalidate>
<form *ngIf="!isLoading && hasWorkspace" (ngSubmit)="onSubmit()" #workspaceForm="ngForm" novalidate>
<div class="form-group">
<label for="workspace-name">Workspace Name</label>
<input
Expand Down
38 changes: 29 additions & 9 deletions src/app/components/workspace/edit-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit, inject } from '@angular/core';
import { ChangeDetectorRef, Component, OnInit, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
Expand All @@ -18,10 +18,12 @@ export class EditWorkspaceComponent implements OnInit {
error = '';
isSubmitting = false;
isLoading = true;
hasWorkspace = false;

private workspaceService = inject(WorkspaceService);
private router = inject(Router);
private route = inject(ActivatedRoute);
private cdr = inject(ChangeDetectorRef);

ngOnInit(): void {
this.route.paramMap.subscribe(params => {
Expand All @@ -35,13 +37,24 @@ export class EditWorkspaceComponent implements OnInit {
}

loadWorkspace(id: string): void {
this.workspaceService.getWorkspace(id).subscribe(workspace => {
this.isLoading = false;
if (workspace) {
this.name = workspace.name;
this.description = workspace.description || '';
} else {
this.error = 'Workspace not found';
this.workspaceService.getWorkspace(id).subscribe({
next: workspace => {
this.isLoading = false;
if (workspace) {
this.hasWorkspace = true;
this.name = workspace.name;
this.description = workspace.description || '';
} else {
this.hasWorkspace = false;
this.error = 'Workspace not found';
}
this.cdr.detectChanges();
},
error: () => {
this.isLoading = false;
this.hasWorkspace = false;
this.error = 'Unable to load workspace. Please try again.';
this.cdr.detectChanges();
}
});
}
Expand All @@ -57,7 +70,14 @@ export class EditWorkspaceComponent implements OnInit {
this.error = '';

this.workspaceService.updateWorkspace(this.workspaceId, trimmedName, this.description.trim()).subscribe({
next: () => this.router.navigate(['/dashboard']),
next: (workspace) => {
if (workspace) {
this.router.navigate(['/dashboard']);
} else {
this.error = 'Workspace not found.';
this.isSubmitting = false;
}
},
error: () => {
this.error = 'Unable to update workspace. Please try again.';
this.isSubmitting = false;
Expand Down
20 changes: 20 additions & 0 deletions src/app/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ export class AuthService {
return !!this.getToken();
}

getCurrentUserId(): string | null {
const token = this.getToken();
if (!token) {
return null;
}

const payload = token.split('.')[1];
if (!payload) {
return null;
}

try {
const normalizedPayload = payload.replace(/-/g, '+').replace(/_/g, '/');
const decodedPayload = JSON.parse(atob(normalizedPayload)) as { user_id?: number | string };
return decodedPayload.user_id === undefined ? null : String(decodedPayload.user_id);
} catch {
return null;
}
}

getToken(): string | null {
return localStorage.getItem(this.tokenKey);
}
Expand Down
Loading