From e105c19c8ebe56b6828ba82948895ad0ca71d8c2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 21 Aug 2016 10:41:21 +0200 Subject: Client: support the new make friends method --- .../friends/friend-add/friend-add.component.ts | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 client/src/app/admin/friends/friend-add/friend-add.component.ts (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts new file mode 100644 index 000000000..30dbf4d36 --- /dev/null +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -0,0 +1,99 @@ +import { Component } from '@angular/core'; +import { Router } from '@angular/router'; + +import { FriendService } from '../shared'; + +@Component({ + selector: 'my-friend-add', + template: require('./friend-add.component.html'), + styles: [ require('./friend-add.component.scss') ] +}) +export class FriendAddComponent { + urls = [ '' ]; + error: string = null; + + constructor(private router: Router, private friendService: FriendService) {} + + addField() { + this.urls.push(''); + } + + customTrackBy(index: number, obj: any): any { + return index; + } + + displayAddField(index: number) { + return index === (this.urls.length - 1); + } + + displayRemoveField(index: number) { + return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1); + } + + removeField(index: number) { + this.urls.splice(index, 1); + } + + makeFriends() { + this.error = ''; + + const notEmptyUrls = this.getNotEmptyUrls(); + if (notEmptyUrls.length === 0) { + this.error = 'You need to specify at less 1 url.'; + return; + } + + if (!this.isUrlsRegexValid(notEmptyUrls)) { + this.error = 'Some url(s) are not valid.'; + return; + } + + if (!this.isUrlsUnique(notEmptyUrls)) { + this.error = 'Urls need to be unique.'; + return; + } + + const confirmMessage = 'Are you sure to make friends with:\n - ' + this.urls.join('\n - '); + if (!confirm(confirmMessage)) return; + + this.friendService.makeFriends(notEmptyUrls).subscribe( + status => { + if (status === 409) { + alert('Already made friends!'); + } else { + alert('Made friends!'); + } + }, + error => alert(error) + ); + } + + private getNotEmptyUrls() { + const notEmptyUrls = []; + + this.urls.forEach((url) => { + if (url !== '') notEmptyUrls.push(url); + }); + + return notEmptyUrls; + } + + // Temporary + // Use HTML validators + private isUrlsRegexValid(urls: string[]) { + let res = true; + + const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$'); + urls.forEach((url) => { + if (urlRegex.test(url) === false) { + res = false; + } + }); + + return res; + } + + private isUrlsUnique(urls: string[]) { + return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); + } +} -- cgit v1.2.3 From 0f6da32b148c0f4146b2ae9ad1add9a9f00cc339 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Aug 2016 14:37:49 +0200 Subject: Client: update to new form api --- client/src/app/admin/friends/friend-add/friend-add.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 30dbf4d36..07888a781 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -53,7 +53,7 @@ export class FriendAddComponent { return; } - const confirmMessage = 'Are you sure to make friends with:\n - ' + this.urls.join('\n - '); + const confirmMessage = 'Are you sure to make friends with:\n - ' + notEmptyUrls.join('\n - '); if (!confirm(confirmMessage)) return; this.friendService.makeFriends(notEmptyUrls).subscribe( -- cgit v1.2.3 From 9ab1071c8d90d112f1031cf006ed621011553e84 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Aug 2016 14:48:59 +0200 Subject: Do not wait the make friends process ends to send a response to the request --- client/src/app/admin/friends/friend-add/friend-add.component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 07888a781..ffc499b92 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -61,7 +61,8 @@ export class FriendAddComponent { if (status === 409) { alert('Already made friends!'); } else { - alert('Made friends!'); + alert('Make friends request sent!'); + this.router.navigate([ '/admin/friends/list' ]); } }, error => alert(error) -- cgit v1.2.3 From 9e8aa10d94e4642ec1d20a522c41b06e9df7758b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Aug 2016 15:49:16 +0200 Subject: Client: change url validation for friend add --- .../friends/friend-add/friend-add.component.ts | 53 ++++++++++++---------- 1 file changed, 29 insertions(+), 24 deletions(-) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index ffc499b92..16cfd8a3a 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -1,20 +1,30 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; +import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; import { Router } from '@angular/router'; +import { validateUrl } from '../../../shared'; import { FriendService } from '../shared'; @Component({ selector: 'my-friend-add', template: require('./friend-add.component.html'), - styles: [ require('./friend-add.component.scss') ] + styles: [ require('./friend-add.component.scss') ], + directives: [ REACTIVE_FORM_DIRECTIVES ] }) -export class FriendAddComponent { - urls = [ '' ]; +export class FriendAddComponent implements OnInit { + friendAddForm: FormGroup; + urls = [ ]; error: string = null; constructor(private router: Router, private friendService: FriendService) {} + ngOnInit() { + this.friendAddForm = new FormGroup({}); + this.addField(); + } + addField() { + this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); this.urls.push(''); } @@ -30,6 +40,21 @@ export class FriendAddComponent { return (index !== 0 || this.urls.length > 1) && index !== (this.urls.length - 1); } + isFormValid() { + // Do not check the last input + for (let i = 0; i < this.urls.length - 1; i++) { + if (!this.friendAddForm.controls[`url-${i}`].valid) return false; + } + + const lastIndex = this.urls.length - 1; + // If the last input (which is not the first) is empty, it's ok + if (this.urls[lastIndex] === '' && lastIndex !== 0) { + return true; + } else { + return this.friendAddForm.controls[`url-${lastIndex}`].valid; + } + } + removeField(index: number) { this.urls.splice(index, 1); } @@ -43,11 +68,6 @@ export class FriendAddComponent { return; } - if (!this.isUrlsRegexValid(notEmptyUrls)) { - this.error = 'Some url(s) are not valid.'; - return; - } - if (!this.isUrlsUnique(notEmptyUrls)) { this.error = 'Urls need to be unique.'; return; @@ -79,21 +99,6 @@ export class FriendAddComponent { return notEmptyUrls; } - // Temporary - // Use HTML validators - private isUrlsRegexValid(urls: string[]) { - let res = true; - - const urlRegex = new RegExp('^https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$'); - urls.forEach((url) => { - if (urlRegex.test(url) === false) { - res = false; - } - }); - - return res; - } - private isUrlsUnique(urls: string[]) { return urls.every(url => urls.indexOf(url) === urls.lastIndexOf(url)); } -- cgit v1.2.3 From f84a89f0e7e9595d2b6f6dd59181c01f562a4239 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Aug 2016 18:09:21 +0200 Subject: Client: fix friend add input control when removing an input --- client/src/app/admin/friends/friend-add/friend-add.component.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 16cfd8a3a..2b2aceb8a 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -56,6 +56,8 @@ export class FriendAddComponent implements OnInit { } removeField(index: number) { + // Remove the last control + this.friendAddForm.removeControl(`url-${this.urls.length - 1}`); this.urls.splice(index, 1); } -- cgit v1.2.3 From ab32b0fc805b92c5a1d7ac5901cb1a38e94622ca Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Sep 2016 22:40:57 +0200 Subject: Dirty update to Angular RC6 --- .../app/admin/friends/friend-add/friend-add.component.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 2b2aceb8a..55aed9156 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { FormControl, FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; +import { FormControl, FormGroup } from '@angular/forms'; import { Router } from '@angular/router'; import { validateUrl } from '../../../shared'; @@ -8,8 +8,7 @@ import { FriendService } from '../shared'; @Component({ selector: 'my-friend-add', template: require('./friend-add.component.html'), - styles: [ require('./friend-add.component.scss') ], - directives: [ REACTIVE_FORM_DIRECTIVES ] + styles: [ require('./friend-add.component.scss') ] }) export class FriendAddComponent implements OnInit { friendAddForm: FormGroup; @@ -80,12 +79,13 @@ export class FriendAddComponent implements OnInit { this.friendService.makeFriends(notEmptyUrls).subscribe( status => { - if (status === 409) { - alert('Already made friends!'); - } else { + // TODO: extractdatastatus + // if (status === 409) { + // alert('Already made friends!'); + // } else { alert('Make friends request sent!'); this.router.navigate([ '/admin/friends/list' ]); - } + // } }, error => alert(error) ); -- cgit v1.2.3 From 4b2f33f3c6d109365090b08244d7f99ad4e69025 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Sep 2016 22:16:51 +0200 Subject: Client: reactive forms --- .../app/admin/friends/friend-add/friend-add.component.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 55aed9156..68363b482 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -11,19 +11,19 @@ import { FriendService } from '../shared'; styles: [ require('./friend-add.component.scss') ] }) export class FriendAddComponent implements OnInit { - friendAddForm: FormGroup; + form: FormGroup; urls = [ ]; error: string = null; constructor(private router: Router, private friendService: FriendService) {} ngOnInit() { - this.friendAddForm = new FormGroup({}); + this.form = new FormGroup({}); this.addField(); } addField() { - this.friendAddForm.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); + this.form.addControl(`url-${this.urls.length}`, new FormControl('', [ validateUrl ])); this.urls.push(''); } @@ -42,7 +42,7 @@ export class FriendAddComponent implements OnInit { isFormValid() { // Do not check the last input for (let i = 0; i < this.urls.length - 1; i++) { - if (!this.friendAddForm.controls[`url-${i}`].valid) return false; + if (!this.form.controls[`url-${i}`].valid) return false; } const lastIndex = this.urls.length - 1; @@ -50,13 +50,13 @@ export class FriendAddComponent implements OnInit { if (this.urls[lastIndex] === '' && lastIndex !== 0) { return true; } else { - return this.friendAddForm.controls[`url-${lastIndex}`].valid; + return this.form.controls[`url-${lastIndex}`].valid; } } removeField(index: number) { // Remove the last control - this.friendAddForm.removeControl(`url-${this.urls.length - 1}`); + this.form.removeControl(`url-${this.urls.length - 1}`); this.urls.splice(index, 1); } @@ -94,7 +94,8 @@ export class FriendAddComponent implements OnInit { private getNotEmptyUrls() { const notEmptyUrls = []; - this.urls.forEach((url) => { + Object.keys(this.form.value).forEach((urlKey) => { + const url = this.form.value[urlKey]; if (url !== '') notEmptyUrls.push(url); }); -- cgit v1.2.3 From ec8d8440a893ba64075da2e57ea04c7976e0b303 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 19 Sep 2016 22:49:31 +0200 Subject: Client: use templateUrl/styleUrls instead of require --- client/src/app/admin/friends/friend-add/friend-add.component.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 68363b482..7aab82b97 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -7,8 +7,8 @@ import { FriendService } from '../shared'; @Component({ selector: 'my-friend-add', - template: require('./friend-add.component.html'), - styles: [ require('./friend-add.component.scss') ] + templateUrl: './friend-add.component.html', + styleUrls: [ './friend-add.component.scss' ] }) export class FriendAddComponent implements OnInit { form: FormGroup; -- cgit v1.2.3 From da4971c11f16b541804b5071d543166cd3954a98 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 26 Sep 2016 22:54:34 +0200 Subject: Client: fix error alert --- client/src/app/admin/friends/friend-add/friend-add.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'client/src/app/admin/friends/friend-add/friend-add.component.ts') diff --git a/client/src/app/admin/friends/friend-add/friend-add.component.ts b/client/src/app/admin/friends/friend-add/friend-add.component.ts index 7aab82b97..64165a9a5 100644 --- a/client/src/app/admin/friends/friend-add/friend-add.component.ts +++ b/client/src/app/admin/friends/friend-add/friend-add.component.ts @@ -87,7 +87,7 @@ export class FriendAddComponent implements OnInit { this.router.navigate([ '/admin/friends/list' ]); // } }, - error => alert(error) + error => alert(error.text) ); } -- cgit v1.2.3