Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

client pop-up error management #77

Merged
merged 9 commits into from Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 20 additions & 26 deletions src/app/app.component.html
@@ -1,33 +1,27 @@
<h1>Angular CLI to UI</h1>

<ng-container *ngIf="isAlive">
<ng-container *ngIf="isReadyForWork">
<app-new
*ngIf="!isAngularProject"
(sendCommand)="sendCommand($event)"
(changeAngularProjectStatus)="checkAngularProject($event)"
>
</app-new>

<ng-container *ngIf="isAngularProject">
<app-serve (sendCommand)="sendCommand($event)"></app-serve>
<app-generate (sendCommand)="sendCommand($event)"></app-generate>
<button
(click)="leaveProject()"
[disabled]="!isProjectLeavable">
Leave Project
</button>
<div *ngIf="!isProjectLeavable">Can not leave this project</div>
</ng-container>
</ng-container>
<app-popup-error></app-popup-error>

<ng-container
*ngIf="!isReadyForWork"
class="blocker">
Please wait while the server is loading
<ng-container *ngIf="isReadyForWork">
<app-new
*ngIf="!isAngularProject"
(sendCommand)="sendCommand($event)"
(changeAngularProjectStatus)="checkAngularProject($event)"
>
</app-new>

<ng-container *ngIf="isAngularProject">
<app-serve (sendCommand)="sendCommand($event)"></app-serve>
<app-generate (sendCommand)="sendCommand($event)"></app-generate>
<button
(click)="leaveProject()">
Leave Project
</button>
</ng-container>
</ng-container>

<ng-container *ngIf="!isAlive">
The server is offline, please run the server and restart the client
<ng-container
*ngIf="!isReadyForWork"
class="blocker">
Please wait while the server is loading
</ng-container>
48 changes: 31 additions & 17 deletions src/app/app.component.ts
@@ -1,10 +1,12 @@
import { Component, OnInit } from '@angular/core';

import { interval } from 'rxjs';
import { interval, timer } from 'rxjs';
import { exhaustMap } from 'rxjs/operators';

import { CommandService } from './services/command/command.service';
import { CommandRequest } from './models/angular-command-request';
import { AngularCliProcessStatus } from './models/angular-cli-process-status.enum';
import { ErrorService } from './services/error/error.service';

@Component({
selector: 'app-root',
Expand All @@ -18,11 +20,13 @@ export class AppComponent implements OnInit {
isReadyForWork = false;
runningCommands = {};
timedStatusCheck = interval(1000);
KEEP_ALIVE_INTERVAL = 1000;
subscription = {};
isAlive = true;
isProjectLeavable = true;

constructor(private commandService: CommandService) {}
constructor(
private commandService: CommandService,
private errorService: ErrorService
) {}

ngOnInit() {
this.keepAlive();
Expand Down Expand Up @@ -78,24 +82,35 @@ export class AppComponent implements OnInit {
}

keepAlive(): void {
this.subscription['keepAlive'] = this.timedStatusCheck
.subscribe(() => {
this.commandService.keepAlive()
.subscribe( response => {
this.isAlive = true;
}, error => {
this.isAlive = false;
this.subscription['keepAlive'].unsubscribe();
console.log('keep alive sending');
ZimGil marked this conversation as resolved.
Show resolved Hide resolved
this.subscription['TimedkeepAlive'] = timer(0, this.KEEP_ALIVE_INTERVAL)
.pipe(
exhaustMap(
() => this.commandService.keepAlive()
)
)
.subscribe(
response => {},
error => {
this.errorService.addError({
errorText: 'The server is offline',
errorDescription: 'please run the server and restart the client'
});
});
}
this.subscription['TimedkeepAlive'].unsubscribe();
}
);
}

leaveProject(): void {
this.commandService.leaveProject()
.subscribe(
() => this.checkAngularProject(),
() => this.isProjectLeavable = false
);
() => {
this.errorService.addError({
errorText: 'Unable to leave this project',
errorDescription: 'To run ngWiz on another project or to create a new one, please run it in the apropriate project direcroty'
});
});
}

sendCommand(userCommand: string): void {
Expand All @@ -107,5 +122,4 @@ export class AppComponent implements OnInit {
.subscribe(() => this.startCheckingCommand(commandId));
});
}

}
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Expand Up @@ -8,14 +8,16 @@ import { NewComponent } from './components/new/new.component';
import { ServeComponent } from './components/serve/serve.component';
import { GenerateComponent } from './components/generate/generate.component';
import { CommandService } from './services/command/command.service';
import { PopupErrorComponent } from './components/popup-error/popup-error.component';


@NgModule({
declarations: [
AppComponent,
ServeComponent,
NewComponent,
GenerateComponent
GenerateComponent,
PopupErrorComponent
],
imports: [
FormsModule,
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions src/app/components/popup-error/popup-error.component.html
@@ -0,0 +1,12 @@
<div
*ngFor="let error of getErrors()"
class="error">
{{error.errorText}}
<ng-container
*ngIf="error.handleFunction"
class="userSelection">
<button (click)="handleError(error)">Yes</button>
<button (click)="clearError(error)">No</button>
</ng-container>
<button *ngIf="!error.handleFunction" (click)="clearError(error)">Ok</button>
</div>
25 changes: 25 additions & 0 deletions src/app/components/popup-error/popup-error.component.spec.ts
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { PopupErrorComponent } from './popup-error.component';

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

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ PopupErrorComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(PopupErrorComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
29 changes: 29 additions & 0 deletions src/app/components/popup-error/popup-error.component.ts
@@ -0,0 +1,29 @@
import { Component } from '@angular/core';
import { PopUpError } from './../../models/pop-up-error.interface';
import { ErrorService } from './../../services/error/error.service';

@Component({
selector: 'app-popup-error',
templateUrl: './popup-error.component.html',
styleUrls: ['./popup-error.component.css']
})
export class PopupErrorComponent {

constructor(private errorService: ErrorService) {}

isErrors(): boolean {
return this.errorService.isErrors();
}

getErrors(): PopUpError[] {
return this.errorService.getErrors();
}

handleError(error: PopUpError) {
this.errorService.handleError(error);
}

clearError(error: PopUpError) {
this.errorService.clearError(error);
}
}
6 changes: 6 additions & 0 deletions src/app/models/pop-up-error.interface.ts
@@ -0,0 +1,6 @@
export interface PopUpError {
errorText: string;
errorDescription?: string;
handleFunction?: Function;
callingScop?: Object;
ZimGil marked this conversation as resolved.
Show resolved Hide resolved
}
15 changes: 15 additions & 0 deletions src/app/services/error/error.service.spec.ts
@@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';

import { ErrorService } from './error.service';

describe('ErrorService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ErrorService]
});
});

it('should be created', inject([ErrorService], (service: ErrorService) => {
expect(service).toBeTruthy();
}));
});
33 changes: 33 additions & 0 deletions src/app/services/error/error.service.ts
@@ -0,0 +1,33 @@
import { Injectable } from '@angular/core';
import { PopUpError } from './../../models/pop-up-error.interface';

@Injectable({
providedIn: 'root'
})
export class ErrorService {

private errors: PopUpError[] = [];

addError(error: PopUpError): void {
this.errors.push(error);
}

getErrors(): PopUpError[] {
return this.errors;
}

clearError(error: PopUpError): void {
this.errors.splice(this.errors.indexOf(error), 1);
}

isErrors(): boolean {
return !!this.errors.length;
}

handleError(error: PopUpError): void {
if (error.handleFunction && typeof error.handleFunction === 'function') {
error.handleFunction.call(error.callingScop);
}
this.clearError(error);
}
}