-
Notifications
You must be signed in to change notification settings - Fork 43
[1078] refactor organization-contact to reactive-form #1081
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
760de24
424a6be
959a6fa
ecef9ac
e6a3df9
2100d30
f826c08
1159e84
854a432
951c2cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,72 @@ | ||
| import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges, ViewChild } from '@angular/core'; | ||
| import { NgForm } from '@angular/forms'; | ||
| import { Component, OnInit, OnChanges, SimpleChanges, EventEmitter, Output, Input } from '@angular/core'; | ||
| import { FormBuilder, FormGroup, Validators } from '@angular/forms'; | ||
|
|
||
| import { Observable } from 'rxjs/Observable'; | ||
| import { filter, take } from 'rxjs/operators'; | ||
|
|
||
| import { OrganizationContactPayload } from '../organization.model'; | ||
| import { ContactStatus } from '../organization.interfaces'; | ||
| import {AuthService} from '../../auth.service'; | ||
| import {Observable} from 'rxjs/Observable'; | ||
| import {User} from '../../user'; | ||
| import {UserService} from '../../user.service'; | ||
|
|
||
| import { AuthService } from '../../auth.service'; | ||
| import { User } from '../../user'; | ||
| import { UserService } from '../../user.service'; | ||
|
|
||
| @Component({ | ||
| selector: 'volontulo-organization-contact', | ||
| templateUrl: './organization-contact.component.html', | ||
| styleUrls: ['./organization-contact.component.scss'] | ||
| }) | ||
| export class OrganizationContactComponent implements OnChanges { | ||
| @ViewChild('contactForm') contactForm: NgForm; | ||
| @Output() contact = new EventEmitter<OrganizationContactPayload>(); | ||
|
|
||
| export class OrganizationContactComponent implements OnInit, OnChanges { | ||
| @Input() contactStatus: ContactStatus; | ||
| @Output() contact = new EventEmitter<OrganizationContactPayload>(); | ||
| submitDisabled = false; | ||
| alertSuccessClosed = true; | ||
| alertErrorClosed = true; | ||
| user$: Observable<User> = this.authService.user$; | ||
| getFullName = this.userService.getFullName; | ||
|
|
||
| constructor(private authService: AuthService, private userService: UserService) { | ||
| public fg: FormGroup = this.fb.group({ | ||
| name: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(30)]], | ||
| email: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(30), Validators.email]], | ||
| phone_no: ['', [Validators.required, Validators.minLength(9), Validators.maxLength(9), Validators.pattern(/^[0-9]{9}$/)]], | ||
| message: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(2000)]], | ||
| honeyBunny: [''] | ||
| }); | ||
| public success: null | boolean = null; | ||
|
|
||
|
|
||
| constructor( | ||
| private fb: FormBuilder, | ||
| private authService: AuthService, | ||
| private userService: UserService | ||
| ) { } | ||
|
|
||
| ngOnInit() { | ||
| this.user$ | ||
| .pipe( | ||
| filter(user => user !== null), | ||
| take(1), | ||
| ) | ||
| .subscribe(user => { | ||
| this.fg.controls.name.setValue(this.getFullName(user)); | ||
| this.fg.controls.email.setValue(user.email); | ||
| this.fg.controls.phone_no.setValue(user.phoneNo); | ||
| }); | ||
| } | ||
|
|
||
| ngOnChanges(changes: SimpleChanges) { | ||
| if (changes.contactStatus.currentValue && changes.contactStatus.currentValue.status === 'success') { | ||
| this.alertSuccessClosed = false; | ||
| this.contactForm.reset(); | ||
| this.success = true; | ||
| this.fg.controls.message.reset(); | ||
| } else if (changes.contactStatus.currentValue && changes.contactStatus.currentValue.status === 'error') { | ||
| this.alertErrorClosed = false; | ||
| this.success = false; | ||
| } | ||
| this.submitDisabled = false; | ||
| } | ||
|
|
||
| onSubmit() { | ||
| if (!this.contactForm.value.honeyBunny) { | ||
| this.contact.emit(this.contactForm.value as OrganizationContactPayload); | ||
| if (this.fg.valid && !this.fg.value.honeyBunny) { | ||
| this.submitDisabled = true; | ||
| delete this.fg.value.honeyBunny; | ||
| this.contact.emit(this.fg.value as OrganizationContactPayload); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't change component structure, and how it handles sending value from contact form. If I understood it correctly, form is send not from child component directly, but from parent component: organization.component.html: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry - I didn't notice it was moved not added 👍 |
||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex will fail for the given example. Please refactor checking phone number to a service or just accept anything here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it fail? I made simple live example of this regex validation pattern there: https://stackblitz.com/edit/angular-xhr4qe and it seems to work fine. According to imask docs https://www.npmjs.com/package/angular-imask, unmasked value should be exactly nine digits, like in this regex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about phone number which includes the plus sign, no-exactly 9 digits, spaces and so on?
try:
+48 111-222-333