Skip to content

Commit 5b72e0d

Browse files
authored
Merge pull request #64 from OS2iot/feature/1247_edit-api-key
Edit API key
2 parents fc418c7 + aa8a3e9 commit 5b72e0d

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

src/app/admin/api-key/api-key-edit/api-key-edit.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
class="form-control"
4343
name="permissions"
4444
[compareWith]="compare"
45-
[(ngModel)]="apiKeyRequest.permissions"
45+
[(ngModel)]="apiKeyRequest.permissionIds"
4646
[multiple]="true"
4747
>
4848
<mat-option

src/app/admin/api-key/api-key-edit/api-key-edit.component.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class ApiKeyEditComponent implements OnInit {
3030
public formFailedSubmit = false;
3131
public permissions: PermissionResponse[] = [];
3232
private organizationId: number;
33+
private id: number;
3334

3435
constructor(
3536
private translate: TranslateService,
@@ -39,9 +40,7 @@ export class ApiKeyEditComponent implements OnInit {
3940
private permissionService: PermissionService,
4041
private errorMessageService: ErrorMessageService,
4142
private sharedVariableService: SharedVariableService
42-
) {
43-
translate.use('da');
44-
}
43+
) {}
4544

4645
ngOnInit(): void {
4746
this.getPermissions();
@@ -54,6 +53,11 @@ export class ApiKeyEditComponent implements OnInit {
5453
this.submitButton = translations['API-KEY.EDIT.SAVE'];
5554
});
5655

56+
this.id = +this.route.snapshot.paramMap.get('api-key-id');
57+
58+
if (this.id > 0) {
59+
this.getApiKey(this.id);
60+
}
5761
this.organizationId = this.sharedVariableService.getSelectedOrganisationId();
5862
}
5963

@@ -68,8 +72,8 @@ export class ApiKeyEditComponent implements OnInit {
6872
this.organizationId
6973
)
7074
.subscribe(
71-
(permissions) => {
72-
this.permissions = permissions.data.filter(
75+
(permissionsResponse) => {
76+
this.permissions = permissionsResponse.data.filter(
7377
(x) => x.organization?.id === this.organizationId
7478
);
7579
},
@@ -79,8 +83,16 @@ export class ApiKeyEditComponent implements OnInit {
7983
);
8084
}
8185

86+
private getApiKey(id: number) {
87+
this.apiKeyService.get(id).subscribe((key) => {
88+
this.apiKeyRequest.id = key.id;
89+
this.apiKeyRequest.name = key.name;
90+
this.apiKeyRequest.permissionIds = key.permissions.map((pm) => pm.id);
91+
});
92+
}
93+
8294
onSubmit(): void {
83-
this.create();
95+
this.id ? this.update() : this.create();
8496
}
8597

8698
private create(): void {
@@ -90,11 +102,21 @@ export class ApiKeyEditComponent implements OnInit {
90102
);
91103
}
92104

93-
public compare(o1: any, o2: any): boolean {
94-
return o1 === o2;
105+
private update(): void {
106+
this.apiKeyService.update(this.apiKeyRequest, this.id).subscribe(
107+
() => this.routeBack(),
108+
(err) => this.showError(err)
109+
);
110+
}
111+
112+
public compare(
113+
matOptionValue: number,
114+
ngModelObject: number
115+
): boolean {
116+
return matOptionValue === ngModelObject;
95117
}
96118

97-
private showError(err: HttpErrorResponse) {
119+
showError(err: HttpErrorResponse) {
98120
const result = this.errorMessageService.handleErrorMessageWithFields(err);
99121
this.errorFields = result.errorFields;
100122
this.errorMessages = result.errorMessages;

src/app/admin/api-key/api-key-list/api-key-table/api-key-table.component.html

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,34 @@
5050
<ng-container matColumnDef="menu">
5151
<th mat-header-cell *matHeaderCellDef></th>
5252
<td mat-cell *matCellDef="let element">
53-
<a
54-
*ngIf="canAccess(element)"
55-
(click)="deleteApiKey(element.id)"
56-
[routerLink]=""
57-
>{{ 'API-KEY.TABLE-ROW.DELETE' | translate }}
58-
</a>
53+
<div *ngIf="canAccess(element)" class="dropdown">
54+
<a
55+
href="#"
56+
role="button"
57+
id="tableRowDropdown-{{ element.id }}"
58+
class="applicationRow__edit dropdown-toggle"
59+
data-toggle="dropdown"
60+
aria-expanded="false"
61+
[attr.aria-label]="'APPLICATION-TABLE-ROW.SHOW-OPTIONS' | translate"
62+
></a>
63+
<ul
64+
class="dropdown-menu dropdown-menu--table"
65+
attr.aria-labelledby="tableRowDropdown-{{ element.id }}"
66+
>
67+
<li class="dropdown-item">
68+
<a
69+
[routerLink]="[element.id, 'edit-api-key']"
70+
routerLinkActive="active"
71+
>{{ 'ORGANISATION-TABLE-ROW.EDIT' | translate }}
72+
</a>
73+
</li>
74+
<li class="dropdown-item">
75+
<a (click)="deleteApiKey(element.id)" [routerLink]=""
76+
>{{ 'API-KEY.TABLE-ROW.DELETE' | translate }}
77+
</a>
78+
</li>
79+
</ul>
80+
</div>
5981
</td>
6082
</ng-container>
6183

src/app/admin/api-key/api-key.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PermissionResponse } from '../permission/permission.model';
33
export class ApiKeyRequest {
44
id: number;
55
name: string;
6-
permissions?: PermissionResponse[];
6+
permissionIds?: number[];
77
}
88

99
export interface ApiKeyResponse {

src/app/admin/api-key/api-key.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export class ApiKeyService {
2525
});
2626
}
2727

28+
update(body: ApiKeyRequest, id: number): Observable<ApiKeyResponse> {
29+
return this.restService.put(this.endpoint, body, id, {
30+
observe: 'response',
31+
});
32+
}
33+
2834
get(id: number): Observable<ApiKeyResponse> {
2935
return this.restService.get(this.endpoint, {}, id).pipe(
3036
map((response: ApiKeyResponse) => {

0 commit comments

Comments
 (0)