From 2bd996fe10ac210b2a0d9501c48f7162b5ec37ac Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Mon, 9 Apr 2018 17:13:47 +0200 Subject: [PATCH] Added slave edit component --- .../src/app/operations/domains.operations.ts | 22 +++++++++++++ .../app/pages/domains/domains.component.html | 4 ++- .../edit-slave/edit-slave.component.html | 20 +++++++++-- .../pages/edit-slave/edit-slave.component.ts | 33 ++++++++++++++++++- .../partials/pagesize/pagesize.component.html | 22 ++++++------- .../app/partials/select/select.component.ts | 2 -- frontend/src/app/services/http.service.ts | 4 +-- 7 files changed, 86 insertions(+), 21 deletions(-) diff --git a/frontend/src/app/operations/domains.operations.ts b/frontend/src/app/operations/domains.operations.ts index c6b3bbf..c003cfb 100644 --- a/frontend/src/app/operations/domains.operations.ts +++ b/frontend/src/app/operations/domains.operations.ts @@ -35,4 +35,26 @@ export class DomainsOperation { return false; } } + + public async getSingle(domainId: number): Promise { + try { + return new DomainApitype(await this.http.get(['/domains', domainId.toString()])); + } catch (e) { + console.error(e); + return new DomainApitype({}); + } + } + + public async updateMaster(domainId: number, master: string): Promise { + try { + await this.http.put(['/domains', domainId.toString()], { + master: master + }); + + return true; + } catch (e) { + console.error(e); + return false; + } + } } diff --git a/frontend/src/app/pages/domains/domains.component.html b/frontend/src/app/pages/domains/domains.component.html index 5a64bd1..aad2428 100644 --- a/frontend/src/app/pages/domains/domains.component.html +++ b/frontend/src/app/pages/domains/domains.component.html @@ -1,4 +1,6 @@ - +
+ +
diff --git a/frontend/src/app/pages/edit-slave/edit-slave.component.html b/frontend/src/app/pages/edit-slave/edit-slave.component.html index b2db57e..f363243 100644 --- a/frontend/src/app/pages/edit-slave/edit-slave.component.html +++ b/frontend/src/app/pages/edit-slave/edit-slave.component.html @@ -1,3 +1,17 @@ -

- edit-slave works! -

\ No newline at end of file +
+
+

Master for {{ domainName }}

+ +
+
+ + +
+ Master can not be empty. +
+
+ + +
+
+
\ No newline at end of file diff --git a/frontend/src/app/pages/edit-slave/edit-slave.component.ts b/frontend/src/app/pages/edit-slave/edit-slave.component.ts index 01ae251..39380d9 100644 --- a/frontend/src/app/pages/edit-slave/edit-slave.component.ts +++ b/frontend/src/app/pages/edit-slave/edit-slave.component.ts @@ -1,3 +1,6 @@ +import { DomainsOperation } from './../../operations/domains.operations'; +import { ActivatedRoute, ParamMap } from '@angular/router'; +import { FormGroup, Validators, FormBuilder } from '@angular/forms'; import { Component, OnInit } from '@angular/core'; @Component({ @@ -7,9 +10,37 @@ import { Component, OnInit } from '@angular/core'; }) export class EditSlaveComponent implements OnInit { - constructor() { } + public slaveForm: FormGroup; + + constructor(private fb: FormBuilder, private route: ActivatedRoute, private domains: DomainsOperation) { } + + public domainName = ''; + public domainId = 0; ngOnInit() { + this.createForm(); + + this.route.paramMap.subscribe((params) => this.initControl(params)); } + private async initControl(params: ParamMap) { + const domain = await this.domains.getSingle(+params.get('domainId')); + + this.domainName = domain.name; + this.domainId = domain.id; + + this.slaveForm.reset({ master: domain.master }); + } + + private createForm() { + this.slaveForm = this.fb.group({ + master: ['', Validators.required] + }); + } + + public async onSubmit() { + await this.domains.updateMaster(this.domainId, this.slaveForm.value.master); + + this.slaveForm.markAsPristine(); + } } diff --git a/frontend/src/app/partials/pagesize/pagesize.component.html b/frontend/src/app/partials/pagesize/pagesize.component.html index 88747ed..a5a4d14 100644 --- a/frontend/src/app/partials/pagesize/pagesize.component.html +++ b/frontend/src/app/partials/pagesize/pagesize.component.html @@ -1,12 +1,10 @@ -
- -
\ No newline at end of file + \ No newline at end of file diff --git a/frontend/src/app/partials/select/select.component.ts b/frontend/src/app/partials/select/select.component.ts index 4c0c987..e34edb5 100644 --- a/frontend/src/app/partials/select/select.component.ts +++ b/frontend/src/app/partials/select/select.component.ts @@ -78,8 +78,6 @@ export class SelectComponent implements OnInit, ControlValueAccessor { } public writeValue(obj: any): void { - console.log('input obj ' + JSON.stringify(obj)); - console.log(obj); if (obj === null) { this.selections = []; } else if (obj instanceof Array) { diff --git a/frontend/src/app/services/http.service.ts b/frontend/src/app/services/http.service.ts index 401672c..3321fc1 100644 --- a/frontend/src/app/services/http.service.ts +++ b/frontend/src/app/services/http.service.ts @@ -15,7 +15,7 @@ export class HttpService { }); } - public async get(url: string, params: Object = {}): Promise { + public async get(url: string | Array, params: Object = {}): Promise { const parts = []; for (const [k, v] of Object.entries(params)) { if (v === undefined || v === null) { @@ -34,7 +34,7 @@ export class HttpService { const queryStr = parts.join('&'); - const reqUrl = queryStr.length > 0 ? this.makeUrl(url) + '?' + queryStr : url; + const reqUrl = queryStr.length > 0 ? this.makeUrl(url) + '?' + queryStr : this.makeUrl(url); return (await this.http({ url: reqUrl,