Skip to content

Commit 5cb3243

Browse files
move types into the grid component to streamline the api integration
1 parent 6383f63 commit 5cb3243

5 files changed

Lines changed: 47 additions & 53 deletions

File tree

libs/vault/src/components/add-item-dialog/add-item-dialog.component.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
[canCreateFolder]="data.canCreateFolder"
88
[canCreateCollection]="data.canCreateCollection"
99
[canCreateSshKey]="data.canCreateSshKey"
10-
(cipherSelected)="onCipherSelected($event)"
11-
(folderSelected)="onFolderSelected()"
12-
(collectionSelected)="onCollectionSelected()"
10+
(itemSelected)="onItemSelected($event)"
1311
/>
1412
</div>
1513
</bit-dialog>

libs/vault/src/components/add-item-dialog/add-item-dialog.component.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ describe("AddItemDialogComponent", () => {
5959
canCreateSshKey: true,
6060
});
6161

62-
getGrid().cipherSelected.emit(CipherType.Login);
62+
getGrid().itemSelected.emit({
63+
result: AddItemDialogResult.Cipher,
64+
cipherType: CipherType.Login,
65+
});
6366

6467
expect(close).toHaveBeenCalledWith({
6568
result: AddItemDialogResult.Cipher,
@@ -74,7 +77,7 @@ describe("AddItemDialogComponent", () => {
7477
canCreateSshKey: false,
7578
});
7679

77-
getGrid().folderSelected.emit();
80+
getGrid().itemSelected.emit({ result: AddItemDialogResult.Folder });
7881

7982
expect(close).toHaveBeenCalledWith({ result: AddItemDialogResult.Folder });
8083
});
@@ -86,7 +89,7 @@ describe("AddItemDialogComponent", () => {
8689
canCreateSshKey: false,
8790
});
8891

89-
getGrid().collectionSelected.emit();
92+
getGrid().itemSelected.emit({ result: AddItemDialogResult.Collection });
9093

9194
expect(close).toHaveBeenCalledWith({ result: AddItemDialogResult.Collection });
9295
});

libs/vault/src/components/add-item-dialog/add-item-dialog.component.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
import { ChangeDetectionStrategy, Component, inject } from "@angular/core";
22

3-
import { CipherType } from "@bitwarden/common/vault/enums";
4-
import { UnionOfValues } from "@bitwarden/common/vault/types/union-of-values";
53
import { DIALOG_DATA, DialogModule, DialogRef, DialogService } from "@bitwarden/components";
64
import { I18nPipe } from "@bitwarden/ui-common";
75

8-
import { AddItemGridComponent } from "../add-item-grid/add-item-grid.component";
6+
import { AddItemGridComponent, AddItemGridResult } from "../add-item-grid/add-item-grid.component";
97

10-
export const AddItemDialogResult = Object.freeze({
11-
Cipher: "cipher",
12-
Folder: "folder",
13-
Collection: "collection",
14-
} as const);
15-
16-
export type AddItemDialogResult = UnionOfValues<typeof AddItemDialogResult>;
17-
18-
export type AddItemDialogCloseResult =
19-
| { result: typeof AddItemDialogResult.Cipher; cipherType: CipherType }
20-
| { result: typeof AddItemDialogResult.Folder }
21-
| { result: typeof AddItemDialogResult.Collection };
8+
export { AddItemGridResult as AddItemDialogResult } from "../add-item-grid/add-item-grid.component";
9+
export type AddItemDialogCloseResult = AddItemGridResult;
2210

2311
export type AddItemDialogData = {
2412
canCreateFolder: boolean;
@@ -36,16 +24,8 @@ export class AddItemDialogComponent {
3624
protected readonly dialogRef = inject<DialogRef<AddItemDialogCloseResult>>(DialogRef);
3725
protected readonly data = inject<AddItemDialogData>(DIALOG_DATA);
3826

39-
protected onCipherSelected(cipherType: CipherType): void {
40-
this.dialogRef.close({ result: AddItemDialogResult.Cipher, cipherType });
41-
}
42-
43-
protected onFolderSelected(): void {
44-
this.dialogRef.close({ result: AddItemDialogResult.Folder });
45-
}
46-
47-
protected onCollectionSelected(): void {
48-
this.dialogRef.close({ result: AddItemDialogResult.Collection });
27+
protected onItemSelected(closeResult: AddItemDialogCloseResult): void {
28+
this.dialogRef.close(closeResult);
4929
}
5030

5131
static open(

libs/vault/src/components/add-item-grid/add-item-grid.component.spec.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.servic
66
import { CipherType } from "@bitwarden/common/vault/enums";
77
import { RestrictedItemTypesService } from "@bitwarden/common/vault/services/restricted-item-types.service";
88

9-
import { AddItemGridComponent } from "./add-item-grid.component";
9+
import { AddItemGridComponent, AddItemGridResult } from "./add-item-grid.component";
1010

1111
describe("AddItemGridComponent", () => {
1212
let component: AddItemGridComponent;
@@ -125,51 +125,54 @@ describe("AddItemGridComponent", () => {
125125
expect(items.map((i) => i.labelKey)).not.toContain("typeLogin");
126126
});
127127

128-
it("emits cipherSelected when a cipher type is selected", () => {
128+
it("emits itemSelected with cipher result when a cipher type is selected", () => {
129129
createComponent({
130130
canCreateFolder: false,
131131
canCreateCollection: false,
132132
canCreateSshKey: true,
133133
});
134134

135-
const cipherSelected = jest.fn();
136-
component.cipherSelected.subscribe(cipherSelected);
135+
const itemSelected = jest.fn();
136+
component.itemSelected.subscribe(itemSelected);
137137

138138
const loginItem = component["items"]().find((i) => i.labelKey === "typeLogin");
139139
loginItem!.action();
140140

141-
expect(cipherSelected).toHaveBeenCalledWith(CipherType.Login);
141+
expect(itemSelected).toHaveBeenCalledWith({
142+
result: AddItemGridResult.Cipher,
143+
cipherType: CipherType.Login,
144+
});
142145
});
143146

144-
it("emits folderSelected when folder is selected", () => {
147+
it("emits itemSelected with folder result when folder is selected", () => {
145148
createComponent({
146149
canCreateFolder: true,
147150
canCreateCollection: false,
148151
canCreateSshKey: false,
149152
});
150153

151-
const folderSelected = jest.fn();
152-
component.folderSelected.subscribe(folderSelected);
154+
const itemSelected = jest.fn();
155+
component.itemSelected.subscribe(itemSelected);
153156

154157
const folderItem = component["items"]().find((i) => i.labelKey === "folder");
155158
folderItem!.action();
156159

157-
expect(folderSelected).toHaveBeenCalled();
160+
expect(itemSelected).toHaveBeenCalledWith({ result: AddItemGridResult.Folder });
158161
});
159162

160-
it("emits collectionSelected when collection is selected", () => {
163+
it("emits itemSelected with collection result when collection is selected", () => {
161164
createComponent({
162165
canCreateFolder: false,
163166
canCreateCollection: true,
164167
canCreateSshKey: false,
165168
});
166169

167-
const collectionSelected = jest.fn();
168-
component.collectionSelected.subscribe(collectionSelected);
170+
const itemSelected = jest.fn();
171+
component.itemSelected.subscribe(itemSelected);
169172

170173
const collectionItem = component["items"]().find((i) => i.labelKey === "collection");
171174
collectionItem!.action();
172175

173-
expect(collectionSelected).toHaveBeenCalled();
176+
expect(itemSelected).toHaveBeenCalledWith({ result: AddItemGridResult.Collection });
174177
});
175178
});

libs/vault/src/components/add-item-grid/add-item-grid.component.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ import {
1717
} from "@bitwarden/components";
1818
import { I18nPipe } from "@bitwarden/ui-common";
1919

20-
type DialogItem = {
20+
export const AddItemGridResult = Object.freeze({
21+
Cipher: "cipher",
22+
Folder: "folder",
23+
Collection: "collection",
24+
} as const);
25+
26+
export type AddItemGridResult =
27+
| { result: typeof AddItemGridResult.Cipher; cipherType: CipherType }
28+
| { result: typeof AddItemGridResult.Folder }
29+
| { result: typeof AddItemGridResult.Collection };
30+
31+
type GridItem = {
2132
icon: BitwardenIcon;
2233
labelKey: string;
2334
subtitleKey: string;
@@ -35,17 +46,15 @@ export class AddItemGridComponent {
3546
readonly canCreateCollection = input(false);
3647
readonly canCreateSshKey = input(false);
3748

38-
readonly cipherSelected = output<CipherType>();
39-
readonly folderSelected = output();
40-
readonly collectionSelected = output();
49+
readonly itemSelected = output<AddItemGridResult>();
4150

4251
private readonly restrictedTypes = toSignal(this.restrictedItemTypesService.restricted$, {
4352
initialValue: [] as RestrictedCipherType[],
4453
});
4554

46-
protected readonly items = computed<DialogItem[]>(() => {
55+
protected readonly items = computed<GridItem[]>(() => {
4756
const restrictedTypes = this.restrictedTypes();
48-
const items: DialogItem[] = DIALOG_CIPHER_MENU_ITEMS.filter((item) => {
57+
const items: GridItem[] = DIALOG_CIPHER_MENU_ITEMS.filter((item) => {
4958
if (!this.canCreateSshKey() && item.type === CipherType.SshKey) {
5059
return false;
5160
}
@@ -54,15 +63,16 @@ export class AddItemGridComponent {
5463
icon: item.icon as BitwardenIcon,
5564
labelKey: item.labelKey,
5665
subtitleKey: item.subtitleKey,
57-
action: () => this.cipherSelected.emit(item.type),
66+
action: () =>
67+
this.itemSelected.emit({ result: AddItemGridResult.Cipher, cipherType: item.type }),
5868
}));
5969

6070
if (this.canCreateFolder()) {
6171
items.push({
6272
icon: "bwi-folder",
6373
labelKey: "folder",
6474
subtitleKey: "folderSubtitle",
65-
action: () => this.folderSelected.emit(),
75+
action: () => this.itemSelected.emit({ result: AddItemGridResult.Folder }),
6676
});
6777
}
6878

@@ -71,7 +81,7 @@ export class AddItemGridComponent {
7181
icon: "bwi-collection-shared",
7282
labelKey: "collection",
7383
subtitleKey: "collectionSubtitle",
74-
action: () => this.collectionSelected.emit(),
84+
action: () => this.itemSelected.emit({ result: AddItemGridResult.Collection }),
7585
});
7686
}
7787

0 commit comments

Comments
 (0)