-
Notifications
You must be signed in to change notification settings - Fork 37
Reactive Forms exercise #45
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: reactive-forms
Are you sure you want to change the base?
Changes from all commits
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,10 +1,18 @@ | ||
|
|
||
| <input type="text" #taskDescription class="add-todo"> | ||
|
|
||
| <button | ||
| class="btn btn-primary" | ||
| (click)="save(taskDescription)"> | ||
| Create | ||
| </button> | ||
| <form [formGroup]="taskGroup" (ngSubmit)="save()"> | ||
| <input type="hidden" formControlName="id"> | ||
| <input type="text" formControlName="description" class="add-todo"> | ||
| <div> | ||
| <span style="color:red" *ngIf="taskGroup.controls.description.invalid && taskGroup.controls.description.touched">* No ha ingresado descripcion</span> | ||
| </div> | ||
| <input type="text" formControlName="url" class="add-todo"> | ||
| <div> | ||
| <span style="color:red" *ngIf="taskGroup.controls.url.errors?.pattern && taskGroup.controls.url.touched">* No ha ingresado una URL válida</span> | ||
| </div> | ||
| <button type="submit" [disabled]="taskGroup.invalid" class="btn btn-primary">Save</button> | ||
| </form> | ||
|
|
||
| {{taskGroup.controls.description.value}} | ||
| <br> | ||
| {{taskGroup.controls.url.value}} | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,71 @@ | ||
| import { Component, OnInit, Output, EventEmitter} from '@angular/core'; | ||
| import { Component, OnInit, Output, EventEmitter, Input} from '@angular/core'; | ||
| import { TodoItem } from '../model/todo-item'; | ||
| import { FormGroup, FormControl, Validators } from '@angular/forms'; | ||
| import { TodoService } from '../todo.service'; | ||
| const urlRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/; | ||
|
|
||
| @Component({ | ||
| selector: 'app-todo-form', | ||
| templateUrl: './todo-form.component.html', | ||
| styleUrls: ['./todo-form.component.scss'] | ||
| }) | ||
| export class TodoFormComponent { | ||
|
|
||
| @Input() selectedTask:TodoItem; | ||
| @Output() add = new EventEmitter(); | ||
| taskGroup: FormGroup; | ||
|
|
||
| constructor(private service:TodoService) { | ||
| this.taskGroup = new FormGroup({ | ||
| description: new FormControl('',[Validators.required]), | ||
| url: new FormControl('',[Validators.required,Validators.pattern(urlRegex)]), | ||
| id: new FormControl('') | ||
| }) | ||
|
|
||
| save(description){ | ||
| if(!description.value || description.value === '') { | ||
| } | ||
| ngOnChanges(){ | ||
| if (!this.selectedTask) { | ||
| this.taskGroup.patchValue({ | ||
| description: '', | ||
| url: '', | ||
| id: '' | ||
| }) | ||
| } | ||
| else{ | ||
| this.taskGroup.patchValue({ | ||
| description: this.selectedTask.description, | ||
| url: this.selectedTask.url, | ||
| id: this.selectedTask.id | ||
| }) | ||
|
|
||
| } | ||
| } | ||
| save(){ | ||
| if(!this.taskGroup.controls.description.value || this.taskGroup.controls.description.value === '') { | ||
| return; | ||
| } | ||
| let task = new TodoItem(); | ||
| task.description = description.value; | ||
| task.isCompleted = false; | ||
| this.add.emit(task); | ||
| description.value = ''; | ||
| if (this.taskGroup.controls.id.value) { | ||
|
Contributor
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. tal vez yo usaria dos componentes diferentes con una herencia y me fijaria en el paso anterior para ver si estoy en edicion o no o sea en la lista cuando agrego con el boton hago una cosa y uso un componente y en el otro caso uso otra. Inclusive se podria usar el mismo componente y con un poco de polimorfismo determinar si esta en edicion o no. |
||
| let task = new TodoItem(); | ||
| task.description = this.taskGroup.controls.description.value; | ||
|
Contributor
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. la copia de los valores es igual en las dos ramas del if, deberian ir afuera para evitar duplicaicon de codigo |
||
| task.isCompleted = false; | ||
| task.url = this.taskGroup.controls.url.value; | ||
| task.id = this.taskGroup.controls.id.value; | ||
| this.service.remove(task.id); | ||
| this.add.emit(task); | ||
| this.taskGroup.reset(); | ||
| console.log(this.taskGroup.controls.id.value) | ||
| } | ||
| else{ | ||
| let task = new TodoItem(); | ||
| task.description = this.taskGroup.controls.description.value; | ||
| task.isCompleted = false; | ||
| task.url = this.taskGroup.controls.url.value; | ||
| this.add.emit(task); | ||
| this.taskGroup.reset(); | ||
| console.log(this.selectedTask) | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
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.
me parece que este se podria evitar, pero deberias cambiar la forma en que se hace la edicion tomando los valores del form y copiandolos a la task, me parece que seria mejor