Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added /compare page and compare selector to /servers #22

Merged
merged 14 commits into from
Jun 3, 2024
1 change: 1 addition & 0 deletions src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ServerListingComponent } from './pages/server-listing/server-listing.co

export const routes: Routes = [
{ path: '', component: LandingpageComponent },
{ path: 'compare', loadComponent: () => import('./pages/server-compare/server-compare.component').then(m => m.ServerCompareComponent)},
{ path: 'servers', component: ServerListingComponent },
{ path: 'server/:vendor/:id', loadComponent: () => import('./pages/server-details/server-details.component').then(m => m.ServerDetailsComponent)},
{ path: 'article/:id', loadComponent: () => import('./pages/article/article.component').then(m => m.ArticleComponent)},
Expand Down
50 changes: 50 additions & 0 deletions src/app/pages/server-compare/server-compare.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!-- eslint-disable @angular-eslint/template/interactive-supports-focus -->
<div class="w-full bg-primary min-h-screen">
<div class="content pt-16 pb-16">
<app-breadcrumbs [segments]="breadcrumbs"></app-breadcrumbs>

<div class="flex flex-col gap-4 my-8">
<h1 class="text-white font-bold text-3xl">Instance Compare</h1>
<div class="text-white text-xl pr-16">
Compare the configuration of your instances to identify differences.
</div>
</div>

<div class="w-full overflow-x-auto relative" style="padding-left: 1px;">
<div *ngIf="isLoading" class="absolute top-0 left-0 w-full h-full" style="background-color: rgba(0,0,0,0.2);" >
<div class="flex justify-center mt-16 opacity-100" role="status">
<svg aria-hidden="true" class="inline w-12 h-12 text-gray-200 animate-spin fill-emerald-400" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span class="sr-only">Loading...</span>
</div>
</div>
<table class="items_table">
<thead>
<tr>
<th></th>
<th *ngFor="let item of servers">
{{item.display_name}}
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of fields; let i = index" >
<td>
{{item.name}}
</td>
<ng-container *ngFor="let column of servers">
<td>
{{column.server_id}}
</td>

</ng-container>

</tr>
</tbody>
</table>
</div>

</div>
</div>
27 changes: 27 additions & 0 deletions src/app/pages/server-compare/server-compare.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.items_table {
@apply w-full text-white;

thead {
@apply border-b border-gray-200 border-solid bg-secondary;
th {
@apply text-left px-5 py-3 font-normal text-xs;

}
}

tbody {
tr:nth-child(2n+1) {
@apply bg-primary;
}
tr:nth-child(2n) {
@apply bg-secondary;
}

tr {
@apply hover:border hover:border-emerald-400 hover:border-solid cursor-pointer;
td {
@apply px-5 py-3 text-left text-sm;
}
}
}
}
23 changes: 23 additions & 0 deletions src/app/pages/server-compare/server-compare.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TypeScript's type import syntax for imports only used as types.

- import { ComponentFixture, TestBed } from '@angular/core/testing';
+ import type { ComponentFixture } from '@angular/core/testing';
+ import { TestBed } from '@angular/core/testing';

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import { ComponentFixture, TestBed } from '@angular/core/testing';
import type { ComponentFixture } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';


import { ServerCompareComponent } from './server-compare.component';

describe('ServerCompareComponent', () => {
let component: ServerCompareComponent;
let fixture: ComponentFixture<ServerCompareComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ServerCompareComponent]
})
.compileComponents();

fixture = TestBed.createComponent(ServerCompareComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
72 changes: 72 additions & 0 deletions src/app/pages/server-compare/server-compare.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-disable prefer-const */
import { Component, OnInit } from '@angular/core';
import { KeeperAPIService } from '../../services/keeper-api.service';
import { ActivatedRoute } from '@angular/router';
import { BreadcrumbSegment, BreadcrumbsComponent } from '../../components/breadcrumbs/breadcrumbs.component';
import { LucideAngularModule } from 'lucide-angular';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ServerPKsWithPrices } from '../../../../sdk/data-contracts';
import { SeoHandlerService } from '../../services/seo-handler.service';

@Component({
selector: 'app-server-compare',
standalone: true,
imports: [BreadcrumbsComponent, LucideAngularModule, CommonModule, FormsModule],
templateUrl: './server-compare.component.html',
styleUrl: './server-compare.component.scss'
})
export class ServerCompareComponent implements OnInit {


breadcrumbs: BreadcrumbSegment[] = [
{ name: 'Home', url: '/' },
{ name: 'Compare Instances', url: '/compare' }
];

isLoading = true;

servers: ServerPKsWithPrices[] = [];

fields: any[] = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace any type with a more specific type to ensure type safety.

- fields: any[] = [
+ fields: { name: string; key: string; }[] = [
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
fields: any[] = [
fields: { name: string; key: string; }[] = [
Tools
Biome

[error] 31-31: Unexpected any. Specify a different type.

{ name: 'CPU', key: 'cpu' },
{ name: 'RAM', key: 'ram' },
{ name: 'Storage', key: 'storage' },
{ name: 'Network', key: 'network' },
{ name: 'Price', key: 'price' }
];

constructor(
private keeperAPI: KeeperAPIService,
private seoHandler: SeoHandlerService,
private route: ActivatedRoute) { }

ngOnInit() {

const title = 'Compare Servers';
const description = 'Compare servers to find the best one for your needs';
const keywords = 'compare, servers, server, hosting, cloud, vps, dedicated, comparison';

this.seoHandler.updateTitleAndMetaTags(title, description, keywords);

this.route.queryParams.subscribe(params => {
const param = params['instances'];
if(param){
const decodedParams = JSON.parse(atob(param));
let promises: Promise<any>[] = [];
decodedParams?.forEach((instance: any) => {
promises.push(
this.keeperAPI.getServer(instance.vendor, instance.server)
);
});
Comment on lines +61 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace forEach with for...of for better performance and readability.

- decodedParams?.forEach((instance: any) => {
-   promises.push(this.keeperAPI.getServer(instance.vendor, instance.server));
- });
+ for (const instance of decodedParams) {
+   promises.push(this.keeperAPI.getServer(instance.vendor, instance.server));
+ }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
decodedParams?.forEach((instance: any) => {
promises.push(
this.keeperAPI.getServer(instance.vendor, instance.server)
);
});
for (const instance of decodedParams) {
promises.push(
this.keeperAPI.getServer(instance.vendor, instance.server)
);
}
Tools
Biome

[error] 61-65: Prefer for...of instead of forEach.


[error] 61-61: Unexpected any. Specify a different type.

Promise.all(promises).then((data) => {
data?.forEach((server: any) => {
this.servers.push(server.body);
});
this.isLoading = false;
});
}
});
}

}
20 changes: 20 additions & 0 deletions src/app/pages/server-listing/server-listing.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- eslint-disable @angular-eslint/template/interactive-supports-focus -->
<div class="w-full bg-primary">
<div class="content pt-16 pb-16">
<app-breadcrumbs [segments]="breadcrumbs"></app-breadcrumbs>
Expand Down Expand Up @@ -253,6 +254,7 @@ <h1 class="text-white font-bold text-3xl">Cloud Compute Resources listing</h1>
<table class="items_table">
<thead>
<tr>
<th></th>
<th *ngFor="let item of tableColumns"
[ngClass]="{
'cursor-pointer': item.orderField
Expand All @@ -275,6 +277,14 @@ <h1 class="text-white font-bold text-3xl">Cloud Compute Resources listing</h1>
[attr.data-ph-capture-attribute-server-listing-row-server]="item.server.api_reference"
[attr.data-ph-capture-attribute-server-listing-row-index]="i+1"
[attr.data-ph-capture-attribute-server-listing-row-page-size]="limit">
<td>
<div class="flex gap-2 items-center" (click)="$event.stopPropagation()">
<input type="checkbox"
id="server_compare_checkbox_{{item.server.server_id}}"
[(ngModel)]="item.selected"
class="focus:ring-emerald-400 accent-emerald-400 text-emerald-400 outline-none" >
</div>
</td>
<ng-container *ngFor="let column of tableColumns">
<td *ngIf="column.type === 'name'" class="min-w-225">
<div class="text-sm text-white flex gap-1">
Expand Down Expand Up @@ -443,6 +453,16 @@ <h1 class="text-white font-bold text-3xl">Cloud Compute Resources listing</h1>
</div>
</div>

<div *ngIf="compareCount() >= 2" class="fixed bottom-8 left-1/2 z-10">
<button
class="flex justify-center bg-emerald-400 text-white py-2 px-4 rounded-lg font-bold hover:bg-emerald-500"
data-ph-capture-attribute-sc-event="server listing compare"
data-ph-capture-attribute-server-listing-compare-action="open"
(click)="openCompare()">
Compare ({{compareCount()}})
</button>
</div>

<!-- Dropdown menu -->
<div id="allocation_options" class="z-10 hidden bg-secondary rounded-lg shadow w-44 border border-emerald-400 border-solid">
<ul class="py-2 px-1 text-sm text-gray-700" aria-labelledby="dropdownDefaultButton">
Expand Down
29 changes: 28 additions & 1 deletion src/app/pages/server-listing/server-listing.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ export class ServerListingComponent implements OnInit {
}

this.keeperAPI.searchServers(queryObject).then(servers => {
this.servers = servers?.body;
this.servers = servers?.body.map((item: any) => {
return {...item, selected: false};
});

if(updateTotalCount) {
this.totalPages = Math.ceil(parseInt(servers?.headers?.get('x-total-count') || '0') / this.limit);
}
Expand Down Expand Up @@ -730,4 +733,28 @@ export class ServerListingComponent implements OnInit {
this.valueChanged();
}

compareCount() {
return this.servers?.filter((server) => server.selected).length;
}

openCompare() {
const selectedServers = this.servers.filter((server) => server.selected);

if(selectedServers.length < 2) {
alert('Please select at least two servers to compare');
return;
}

const serverIds = selectedServers.map((server) => {
return {vendor: server.vendor_id, server: server.server_id}
});

// encode atob to avoid issues with special characters
const encoded = btoa(JSON.stringify(serverIds));

console.log(encoded);

this.router.navigateByUrl('/compare?instances=' + encoded);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The openCompare method correctly handles the comparison logic but can be improved for clarity.

Consider using early returns to reduce nesting and improve readability.

- if(selectedServers.length < 2) {
-   alert('Please select at least two servers to compare');
-   return;
- }
+ if(selectedServers.length >= 2) {
+   const serverIds = selectedServers.map(server => ({ vendor: server.vendor_id, server: server.server_id }));
+   const encoded = btoa(JSON.stringify(serverIds));
+   console.log(encoded);
+   this.router.navigateByUrl('/compare?instances=' + encoded);
+ } else {
+   alert('Please select at least two servers to compare');
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
compareCount() {
return this.servers?.filter((server) => server.selected).length;
}
openCompare() {
const selectedServers = this.servers.filter((server) => server.selected);
if(selectedServers.length < 2) {
alert('Please select at least two servers to compare');
return;
}
const serverIds = selectedServers.map((server) => {
return {vendor: server.vendor_id, server: server.server_id}
});
// encode atob to avoid issues with special characters
const encoded = btoa(JSON.stringify(serverIds));
console.log(encoded);
this.router.navigateByUrl('/compare?instances=' + encoded);
}
openCompare() {
const selectedServers = this.servers.filter((server) => server.selected);
if(selectedServers.length >= 2) {
const serverIds = selectedServers.map(server => ({ vendor: server.vendor_id, server: server.server_id }));
const encoded = btoa(JSON.stringify(serverIds));
console.log(encoded);
this.router.navigateByUrl('/compare?instances=' + encoded);
} else {
alert('Please select at least two servers to compare');
}
}


}