import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { TacheService } from '../tache.service';
import { Router } from '@angular/router';
import { LoadingController } from '@ionic/angular';
import { Tache } from '../tache.model';
@Component({
selector: 'app-new-todo',
templateUrl: './new-todo.page.html',
styleUrls: ['./new-todo.page.scss'],
})
export class NewTodoPage implements OnInit {
form: FormGroup;
newTodo: Tache;
constructor(
private tacheService: TacheService,
private router: Router,
private loaderCtrl: LoadingController) { }
ngOnInit() {
// add formGroup that takes a javascript object
this.form = new FormGroup({
title: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required]
}),
description: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required, Validators.maxLength(500)]
}),
dateFrom: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required]
}),
dateTo: new FormControl(null, {
updateOn: 'blur',
validators: [Validators.required]
})
});
}
getDummyDate() {
return new Date();
}
onCreateTodo() {
if (!this.form.valid) {
return;
}
this.loaderCtrl.create(
{message: 'Creation en cour ...'}
).then(loadingEl => {
loadingEl.present();
this.tacheService.addTache(
this.form.value.title,
this.form.value.description,
new Date(this.form.value.dateFrom),
new Date(this.form.value.dateTo)
)
.subscribe(() => {
loadingEl.dismiss();
this.form.reset();
this.router.navigate(['/tache/tabs/todo']);
// console.log('Ajout d\'un nouveau Todo'))
});
});
}
}