edit song

This commit is contained in:
Benjamin Ifland
2019-03-25 12:19:33 +01:00
parent a46dae93db
commit 38268c3fc3
10 changed files with 107 additions and 58 deletions

View File

@@ -1,27 +1,40 @@
import { Song } from 'src/app/models/song.model';
import { SongsService } from 'src/app/data/songs.service';
import { Injectable } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { switchMap, tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class EditSongService {
constructor(private songsService: SongsService) {}
constructor(private songsService: SongsService) { }
public initEditForm(): FormGroup {
const song = this.songsService.selectedSong.value;
const form = new FormGroup({
ID: new FormControl(song.ID, { updateOn: 'blur' }),
Number: new FormControl(song.Number, { updateOn: 'blur' }),
Name: new FormControl(song.Name, {
updateOn: 'blur',
validators: Validators.required
}),
Text: new FormControl(song.Text, { updateOn: 'blur' }),
SongType: new FormControl(song.SongType, {
updateOn: 'blur',
validators: Validators.required
}),
Key: new FormControl(song.Key, { updateOn: 'blur' }),
Tempo: new FormControl(song.Tempo, { updateOn: 'blur' }),
Comments: new FormControl(song.Comments, { updateOn: 'blur' }),
});
public initEditForm(): FormGroup {
const song = this.songsService.selectedSong.value;
const form = new FormGroup({
ID: new FormControl(song.ID),
Number: new FormControl(song.Number),
Name: new FormControl(song.Name, Validators.required),
Text: new FormControl(song.Text),
SongType: new FormControl(song.SongType, Validators.required),
Key: new FormControl(song.Key),
Tempo: new FormControl(song.Tempo)
});
const controls = Object.keys(form.controls);
controls.forEach(control => {
form.controls[control].valueChanges.pipe(
switchMap(value => this.songsService.patch(song.ID, control, value))
).subscribe();
});
return form;
}
return form;
}
}