feat: adapt angular part

This commit is contained in:
Amadou Ada DIENE
2020-04-18 17:25:23 +02:00
parent 73acf88a07
commit 83301e0fe7
7 changed files with 184 additions and 8 deletions

View File

@@ -1,18 +1,20 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MutiplesComponent } from './components/mutiples/mutiples.component';
@NgModule({
declarations: [
AppComponent
],
declarations: [AppComponent, MutiplesComponent],
imports: [
BrowserModule,
AppRoutingModule
AppRoutingModule,
ReactiveFormsModule,
RouterModule.forRoot([{ path: '', component: MutiplesComponent }]),
],
providers: [],
bootstrap: [AppComponent]
bootstrap: [AppComponent],
})
export class AppModule { }
export class AppModule {}

View File

@@ -0,0 +1,26 @@
<div id="multiples">
<div class="container">
<h2>Times table</h2>
<form [formGroup]="timesTableForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<input
type="number"
class="form-control"
formControlName="input"
required
/>
</div>
<button
type="submit"
class="btn btn-success"
[disabled]="!timesTableForm.valid"
>
SUBMIT
</button>
</form>
<h3 *ngFor="let multiple of multiples; let i = index">
{{ timesTableForm.value.input }} * {{ 1 + i }} = {{ multiple }}
</h3>
</div>
</div>

View File

@@ -0,0 +1,23 @@
#multiples {
position: absolute;
left: 0;
top: 210px;
width: 100%;
display: flex;
justify-content: center;
color: #ffffff;
font-weight: 900;
.container {
background-color: #d47800e0;
border-radius: 4px;
padding: 20px;
form {
display: flex;
> * {
margin: 5px;
}
}
}
}

View File

@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MutiplesComponent } from './mutiples.component';
describe('MutiplesComponent', () => {
let component: MutiplesComponent;
let fixture: ComponentFixture<MutiplesComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MutiplesComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MutiplesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,40 @@
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { ElectronIpcService } from 'angular/app/services/electron-ipc.service';
import { WindowApiConst } from 'shared';
@Component({
selector: 'app-mutiples',
templateUrl: './mutiples.component.html',
styleUrls: ['./mutiples.component.scss'],
})
export class MutiplesComponent implements OnInit {
timesTableForm = new FormGroup({
input: new FormControl(Math.round(Math.random() * 100) % 10),
});
multiples = [];
constructor(private electronIpc: ElectronIpcService) {}
ngOnInit(): void {
// Specifying what to do with received data from main process
this.electronIpc.receive(WindowApiConst.MULTIPLES_OUTPUT, (...data) => {
// Update current data
this.multiples = data;
});
// Reset multiples on form changes
this.timesTableForm.valueChanges.subscribe((value) => {
this.multiples = [];
});
// Init time tables with given random value
this.onSubmit();
}
onSubmit() {
const intput = this.timesTableForm.value.input;
this.electronIpc.send(WindowApiConst.MULTIPLES_INPUT, intput);
}
}

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ElectronIpcService } from './electron-ipc.service';
describe('ElectronIpcService', () => {
let service: ElectronIpcService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ElectronIpcService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,44 @@
import { Injectable, NgZone } from '@angular/core';
import { WindowApi } from 'shared';
@Injectable({
providedIn: 'root',
})
export class ElectronIpcService {
private _api: WindowApi;
constructor(private zone: NgZone) {
if (window && (window as any).api) {
try {
this._api = (window as any).api;
} catch (e) {
throw e;
}
console.log('Preloader API has been loaded successfully');
} else {
console.warn('Preloader API is not loaded');
}
}
public receive(channel: string, func: (...data) => void): void {
if (this._api) {
this._api.receive(channel, (...data) => {
console.log(`Received from main process channel [${channel}]`, data);
// Next code might run outside of Angular zone and therefore Angular
// doesn't recognize it needs to run change detection
// Further details on SO : https://stackoverflow.com/a/49136353/11480016
this.zone.run(() => {
func(...data);
});
});
}
}
public send(channel: string, ...data): void {
if (this._api) {
console.log(`Sending to main process channel [${channel}]`, data);
this._api.send(channel, ...data);
}
}
}