Skip to content

Commit 3f08e90

Browse files
Chahat JainChahat Jain
authored andcommitted
feat: added ht-status-display component
1 parent 0eeceb0 commit 3f08e90

File tree

6 files changed

+183
-0
lines changed

6 files changed

+183
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { IconType } from '@hypertrace/assets-library';
3+
4+
@Pipe({
5+
name: 'htStatusDisplayIcon'
6+
})
7+
export class StatusDisplayIconPipe implements PipeTransform {
8+
public transform(statusCode: string | number, status?: string): IconType | undefined {
9+
if (status === 'FAIL') {
10+
return IconType.AlertFill;
11+
}
12+
if (status === 'SUCCESS') {
13+
return IconType.CheckCircleFill;
14+
}
15+
/**
16+
* GRPC Success -> 0
17+
* HTTP Success -> 100-300
18+
*/
19+
if (+statusCode === 0 || (+statusCode >= 100 && +statusCode <= 399)) {
20+
return IconType.CheckCircleFill;
21+
}
22+
23+
return IconType.AlertFill;
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { displayString } from '@hypertrace/common';
3+
import { isNil } from 'lodash-es';
4+
5+
@Pipe({
6+
name: 'htStatusDisplayText'
7+
})
8+
export class StatusDisplayTextPipe implements PipeTransform {
9+
/**
10+
* Appends the `statusMessage` with a `-` if it's available.
11+
* If not just show the `statusCode`
12+
*
13+
* @param statusCode - status code
14+
* @param statusMessage - status message
15+
*/
16+
public transform(statusCode: string | number, statusMessage?: string): string {
17+
let statusDisplayText = displayString(statusCode);
18+
if (!isNil(statusMessage) && statusMessage !== 'null') {
19+
statusDisplayText = `${statusDisplayText} - ${statusMessage}`;
20+
}
21+
22+
return statusDisplayText;
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@import 'mixins';
2+
3+
.status {
4+
display: flex;
5+
align-items: center;
6+
gap: 4px;
7+
padding: 0 4px;
8+
background-color: $gray-1;
9+
border-radius: 4px;
10+
font-size: 12px;
11+
color: $gray-5;
12+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { IconType } from '@hypertrace/assets-library';
2+
import { IconComponent } from '@hypertrace/components';
3+
import { createHostFactory } from '@ngneat/spectator/jest';
4+
import { MockComponent } from 'ng-mocks';
5+
import { StatusDisplayIconPipe } from './status-display-icon.pipe';
6+
import { StatusDisplayTextPipe } from './status-display-text.pipe';
7+
import { StatusDisplayComponent } from './status-display.component';
8+
9+
describe('Status Display Component', () => {
10+
const createHost = createHostFactory({
11+
component: StatusDisplayComponent,
12+
shallow: true,
13+
declarations: [MockComponent(IconComponent), StatusDisplayTextPipe, StatusDisplayIconPipe]
14+
});
15+
16+
test('should display the status code properly with check icon', () => {
17+
const spectator = createHost(`<ht-status-display [statusCode]="statusCode"></ht-status-display>`, {
18+
hostProps: {
19+
statusCode: 200
20+
}
21+
});
22+
23+
expect(spectator.query(IconComponent)?.icon).toBe(IconType.CheckCircleFill);
24+
expect(spectator.query('.text')?.textContent).toBe('200');
25+
});
26+
27+
test('should display the fail icon', () => {
28+
const spectator = createHost(
29+
`<ht-status-display [statusCode]="statusCode" [status]="status"></ht-status-display>`,
30+
{
31+
hostProps: {
32+
status: 'FAIL',
33+
statusCode: 400
34+
}
35+
}
36+
);
37+
38+
expect(spectator.query(IconComponent)?.icon).toBe(IconType.AlertFill);
39+
});
40+
41+
test('should display the fail icon', () => {
42+
const spectator = createHost(
43+
`<ht-status-display [statusCode]="statusCode" [status]="status"></ht-status-display>`,
44+
{
45+
hostProps: {
46+
statusCode: 400
47+
}
48+
}
49+
);
50+
51+
expect(spectator.query(IconComponent)?.icon).toBe(IconType.AlertFill);
52+
});
53+
54+
test('should display the check icon if unknown status is send', () => {
55+
const spectator = createHost(
56+
`<ht-status-display [statusCode]="statusCode" [status]="status"></ht-status-display>`,
57+
{
58+
hostProps: {
59+
status: 'SOMETHING',
60+
statusCode: 200
61+
}
62+
}
63+
);
64+
65+
expect(spectator.query(IconComponent)?.icon).toBe(IconType.CheckCircleFill);
66+
});
67+
68+
test('should display the status code with status message', () => {
69+
const spectator = createHost(
70+
`<ht-status-display [statusCode]="statusCode" [statusMessage]="statusMessage"></ht-status-display>`,
71+
{
72+
hostProps: {
73+
statusCode: 200,
74+
statusMessage: 'OK'
75+
}
76+
}
77+
);
78+
79+
expect(spectator.query('.text')?.textContent).toBe('200 - OK');
80+
});
81+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
2+
import { IconSize } from '@hypertrace/components';
3+
4+
@Component({
5+
selector: 'ht-status-display',
6+
template: ` <div class="status" *ngIf="this.statusCode">
7+
<ng-container *ngIf="this.statusCode | htStatusDisplayIcon: this.status as icon">
8+
<ht-icon [icon]="icon" size="${IconSize.ExtraSmall}"></ht-icon>
9+
</ng-container>
10+
<span class="text">{{ this.statusCode | htStatusDisplayText: this.statusMessage }}</span>
11+
</div>`,
12+
styleUrls: ['./status-display.component.scss'],
13+
changeDetection: ChangeDetectionStrategy.OnPush
14+
})
15+
export class StatusDisplayComponent {
16+
@Input()
17+
public statusCode?: number | string;
18+
19+
@Input()
20+
public status?: string;
21+
22+
@Input()
23+
public statusMessage?: string;
24+
25+
// TODO: Add display for different styles if required
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NgModule } from '@angular/core';
2+
3+
import { CommonModule } from '@angular/common';
4+
import { FormattingModule, MemoizeModule } from '@hypertrace/common';
5+
import { IconModule } from '@hypertrace/components';
6+
import { StatusDisplayIconPipe } from './status-display-icon.pipe';
7+
import { StatusDisplayTextPipe } from './status-display-text.pipe';
8+
import { StatusDisplayComponent } from './status-display.component';
9+
10+
@NgModule({
11+
imports: [IconModule, MemoizeModule, CommonModule, FormattingModule],
12+
exports: [StatusDisplayComponent],
13+
declarations: [StatusDisplayComponent, StatusDisplayTextPipe, StatusDisplayIconPipe]
14+
})
15+
export class StatusDisplayModule {}

0 commit comments

Comments
 (0)