-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject-components-screen.component.ts
More file actions
218 lines (195 loc) · 7.47 KB
/
Copy pathproject-components-screen.component.ts
File metadata and controls
218 lines (195 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { AppShellIconComponent, AppShellLink, AppShellNotification, AppShellPageHeaderComponent, AppShellToastService } from '@opendevstack/ngx-appshell';
import { ProjectService } from '../../services/project.service';
import { Subject, map, switchMap, takeUntil } from 'rxjs';
import { AppProject } from '../../models/project';
import { ActivatedRoute, Router } from '@angular/router';
import { ProjectComponent } from '../../models/project-component';
import { ComponentCardComponent } from '../../components/component-card/component-card.component';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { RequestDeletionDialogComponent } from '../../components/request-deletion-dialog/request-deletion-dialog.component';
import { RequestDeletionDialogResult } from '../../models/request-deletion-dialog-data';
import { ProvisionerService } from '../../services/provisioner.service';
import { AzureService } from '../../services/azure.service';
import { AppUser } from '../../models/app-user';
import { CreateIncidentParameter } from '../../openapi/component-provisioner';
import { ComponentStatus } from '../../models/component-status';
@Component({
selector: 'app-project-components-screen',
imports: [AppShellPageHeaderComponent, AppShellIconComponent, ComponentCardComponent, MatDialogModule],
templateUrl: './project-components-screen.component.html',
styleUrl: './project-components-screen.component.scss',
encapsulation: ViewEncapsulation.None
})
export class ProjectComponentsScreenComponent implements OnInit, OnDestroy {
selectedProject: AppProject | null = null;
breadcrumbLinks: AppShellLink[] = [];
pageTitle = 'My Components';
projectComponents: ProjectComponent[] = [];
isLoading = false;
connectionErrorHtmlMessage: string | undefined;
connectionErrorIcon: string | undefined;
loggedUser: AppUser | null = null;
private readonly _destroying$ = new Subject<void>();
constructor(
private readonly projectService: ProjectService,
private readonly route: ActivatedRoute,
private readonly router: Router,
private readonly provisionerService: ProvisionerService,
private readonly azureService: AzureService,
private readonly toastService: AppShellToastService,
public dialog: MatDialog
) {}
ngOnInit() {
this.projectService.project$
.pipe(takeUntil(this._destroying$))
.subscribe((project: AppProject | null) => {
if (this.selectedProject?.projectKey !== project?.projectKey) {
this.projectComponents = [];
this.router.navigateByUrl(`/${project?.projectKey}/components`, { replaceUrl: true });
}
this.selectedProject = project;
if (project != null) {
this.updateBreadcrumbs(project);
this.isLoading = true;
this.projectService.getProjectComponents(project.projectKey).subscribe({
next: (components) => {
this.projectComponents = components;
this.isLoading = false;
this.unsetConnectionErrorState();
},
error: () => {
this.setConnectionErrorState();
this.isLoading = false;
}
});
}
});
this.route.params
.pipe(
takeUntil(this._destroying$),
map(params => params['projectKey'] || ''),
switchMap((projectKey: string) =>
this.projectService.ensureUserProjectsLoaded().pipe(
map((projects) => ({ projectKey, projects }))
)
)
)
.subscribe(({ projectKey, projects }) => {
if (!projectKey) {
this.router.navigateByUrl('/page-not-found', { replaceUrl: true });
return;
}
if (!projects.includes(projectKey)) {
this.router.navigateByUrl('/page-not-found', { replaceUrl: true });
return;
}
const currentProject = this.projectService.getCurrentProject();
if (currentProject?.projectKey !== projectKey) {
this.projectService.setCurrentProject(projectKey);
}
});
this.azureService.loggedUser$.pipe(takeUntil(this._destroying$)).subscribe(user => {
this.loggedUser = user;
});
}
updateBreadcrumbs(project: AppProject): void {
this.breadcrumbLinks = [
{
anchor: '',
label: `Project ${project.projectKey}`,
},
{
anchor: '',
label: 'My Components',
}
];
}
onRequestDeletionClicked(component: ProjectComponent): void {
if (!this.selectedProject) {
return;
}
const dialogRef = this.dialog.open(RequestDeletionDialogComponent, {
autoFocus: false,
data: {
componentName: component.name,
projectKey: this.selectedProject.projectKey,
}
});
dialogRef.afterClosed().subscribe((result: RequestDeletionDialogResult | undefined) => {
if (result) {
this.submitDeletionRequest(result);
}
});
}
private submitDeletionRequest(result: RequestDeletionDialogResult): void {
// Apply optimistic UI and set the current component to deleting status
const componentIndex = this.projectComponents.findIndex(c => c.name === result.componentName);
let originalStatus: ComponentStatus | undefined = undefined;
if (componentIndex !== -1) {
originalStatus = this.projectComponents[componentIndex].status;
this.projectComponents[componentIndex].status = 'DELETING';
}
/* eslint-disable @typescript-eslint/no-wrapper-object-types */
const incidentParams: CreateIncidentParameter[] = [
{
name: 'is_deployed',
type: 'boolean',
value: result.deploymentStatus as Boolean // NOSONAR
},
{
name: 'change_number',
type: 'string',
value: result.changeNumber as String // NOSONAR
},
{
name: 'reason',
type: 'string',
value: result.reason as String // NOSONAR
}
];
/* eslint-enable @typescript-eslint/no-wrapper-object-types */
this.provisionerService.requestComponentDeletion(
result.projectKey,
result.componentName,
incidentParams
).subscribe({
next: () => this.onDeletionRequestSuccess(),
error: (error) => {
this.onDeletionRequestError(error)
if (originalStatus && componentIndex !== -1) {
this.projectComponents[componentIndex].status = originalStatus;
}
}
});
}
private onDeletionRequestSuccess(): void {
this.toastService.showToast({
id: '',
read: false,
subject: 'only_toast',
title: 'The request has successfully been sent. Support will receive a Service Now ticket and manage the component deletion.'
} as AppShellNotification, 8000);
}
private onDeletionRequestError(error: unknown): void {
console.error('Error executing action:', error);
this.toastService.showToast({
id: '',
read: false,
subject: 'only_toast',
title: 'Something went wrong. Please try again later.'
} as AppShellNotification, 8000);
}
private setConnectionErrorState() {
this.connectionErrorHtmlMessage = 'Sorry, we are having trouble loading the page.<br/>Please check back in a few minutes.';
this.connectionErrorIcon = 'smiley_sad';
}
private unsetConnectionErrorState() {
this.connectionErrorHtmlMessage = undefined;
this.connectionErrorIcon = undefined;
}
ngOnDestroy(): void {
this._destroying$.next(undefined);
this._destroying$.complete();
}
}