file detail edit form

This commit is contained in:
Benjamin Ifland
2019-03-28 14:14:37 +01:00
parent 787dba4c0e
commit af0d9e724a

View File

@@ -10,8 +10,10 @@ import { Song } from '../models/song.model';
export class EditSongService { export class EditSongService {
constructor(private songsService: SongsService) {} constructor(private songsService: SongsService) {}
public initEditForm(attachSync: boolean): FormGroup { public initSongEditForm(attachSync: boolean): FormGroup {
const song = attachSync ? this.songsService.selectedSong.value : this.defaultValues(); const song = attachSync
? this.songsService.selectedSong.value
: this.defaultValues();
const form = new FormGroup({ const form = new FormGroup({
Number: new FormControl(song.Number, { Number: new FormControl(song.Number, {
updateOn: 'blur', updateOn: 'blur',
@@ -31,10 +33,29 @@ export class EditSongService {
validators: Validators.required validators: Validators.required
}), }),
Tempo: new FormControl(song.Tempo, { updateOn: 'blur' }), Tempo: new FormControl(song.Tempo, { updateOn: 'blur' }),
Comments: new FormControl(song.Comments, { updateOn: 'blur' }), Comments: new FormControl(song.Comments, { updateOn: 'blur' })
}); });
if (attachSync) { this.attachSync(form, song); } if (attachSync) {
this.attachSync(form, song);
}
return form;
}
public initFileEditForm(fileId: number): FormGroup {
const file = this.songsService.selectedSong.value.Files.filter(
_ => _.ID === fileId
)[0];
const form = new FormGroup({
Name: new FormControl(file.Name, {
updateOn: 'blur',
validators: Validators.required
}),
FileType: new FormControl(file.FileType, {
updateOn: 'blur'
})
});
return form; return form;
} }
@@ -42,7 +63,11 @@ export class EditSongService {
private attachSync(form: FormGroup, song: Song) { private attachSync(form: FormGroup, song: Song) {
const controls = Object.keys(form.controls); const controls = Object.keys(form.controls);
controls.forEach(control => { controls.forEach(control => {
form.controls[control].valueChanges.pipe(switchMap(value => this.songsService.patch$(song.ID, control, value))).subscribe(); form.controls[control].valueChanges
.pipe(
switchMap(value => this.songsService.patch$(song.ID, control, value))
)
.subscribe();
}); });
} }