file detail edit server sync

This commit is contained in:
Benjamin Ifland
2019-03-28 15:47:53 +01:00
parent cccdfb4a44
commit 127adea235
5 changed files with 78 additions and 27 deletions

View File

@@ -1,8 +1,16 @@
import { faLongArrowAltLeft } from '@fortawesome/free-solid-svg-icons'; import { SongsService } from './../../../data/songs.service';
import { FileType } from './../../../models/files-types.model.ts'; import { FileType } from './../../../models/files-types.model.ts';
import { FormGroup } from '@angular/forms'; import { FormGroup } from '@angular/forms';
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core'; import {
Component,
OnInit,
OnDestroy,
Input,
Output,
EventEmitter
} from '@angular/core';
import { EditSongService } from 'src/app/data/edit-song.service'; import { EditSongService } from 'src/app/data/edit-song.service';
import { Subscription } from 'rxjs';
@Component({ @Component({
selector: 'app-song-file-edit', selector: 'app-song-file-edit',
@@ -13,22 +21,30 @@ export class SongFileEditComponent implements OnInit, OnDestroy {
@Input() fileId: number; @Input() fileId: number;
@Output() back = new EventEmitter(); @Output() back = new EventEmitter();
public form: FormGroup; public form: FormGroup;
public subscription: Subscription;
public fileTypes = [ public fileTypes = [
{value: FileType.None, text: null}, { value: FileType.None, text: null },
{value: FileType.Sheet, text: 'Text'}, { value: FileType.Sheet, text: 'Text' },
{value: FileType.Chords, text: 'Text + Akkorde'}, { value: FileType.Chords, text: 'Text + Akkorde' },
{value: FileType.MuseScore, text: 'MuseScore'}, { value: FileType.MuseScore, text: 'MuseScore' }
]; ];
constructor(private editSongService: EditSongService) { } constructor(
private editSongService: EditSongService,
private songService: SongsService
) {}
public ngOnInit(): void { public ngOnInit(): void {
this.form = this.editSongService.initFileEditForm(this.fileId); const form = this.editSongService.initFileEditForm(
this.songService.selectedSong.value.ID,
this.fileId
);
this.form = form.form;
this.subscription = form.changeSubscription;
} }
public ngOnDestroy(): void { public ngOnDestroy(): void {
this.form = null; this.form = null;
this.subscription.unsubscribe();
} }
} }

View File

@@ -35,7 +35,7 @@ export class SongFilesComponent {
this.selectedSongId = _.ID; this.selectedSongId = _.ID;
this.song = _; this.song = _;
this.newFileUploader = this.fileuploadFactory.provideForNewFiles(_.ID); this.newFileUploader = this.fileuploadFactory.provideForNewFiles(_.ID);
this.newFileUploader.onCompleteItem = () => songService.selectSong(_.ID); this.newFileUploader.onCompleteItem = () => songService.selectSong(_.ID).subscribe();
this.newFileUploader.onProgressItem = () => change.markForCheck; this.newFileUploader.onProgressItem = () => change.markForCheck;
} else { } else {
this.selectedSongId = 0; this.selectedSongId = 0;

View File

@@ -43,12 +43,12 @@ export class TableComponent {
} }
public onClick(id: number): void { public onClick(id: number): void {
this.songsService.selectSong(id); this.songsService.selectSong(id).subscribe();
this.change.detectChanges(); this.change.detectChanges();
} }
public onClickNew(): void { public onClickNew(): void {
this.songsService.selectSong(null); this.songsService.selectSong(null).subscribe();
this.songsService.state = State.new; this.songsService.state = State.new;
this.change.detectChanges(); this.change.detectChanges();
} }

View File

@@ -1,8 +1,10 @@
import { FileType } from './../models/files-types.model.ts';
import { SongsService } from 'src/app/data/songs.service'; import { SongsService } from 'src/app/data/songs.service';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms'; import { FormGroup, FormControl, Validators } from '@angular/forms';
import { switchMap } from 'rxjs/operators'; import { switchMap } from 'rxjs/operators';
import { Song } from '../models/song.model'; import { Song } from '../models/song.model';
import { Subscription } from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -43,7 +45,7 @@ export class EditSongService {
return form; return form;
} }
public initFileEditForm(fileId: number): FormGroup { public initFileEditForm(songId: number, fileId: number): {form: FormGroup, changeSubscription: Subscription } {
const file = this.songsService.selectedSong.value.Files.filter( const file = this.songsService.selectedSong.value.Files.filter(
_ => _.ID === fileId _ => _.ID === fileId
)[0]; )[0];
@@ -57,7 +59,12 @@ export class EditSongService {
}) })
}); });
return form; const changeSubscription = form.valueChanges.pipe(
switchMap(_ => this.songsService.updateFile$(songId, fileId, _.Name, _.FileType)),
switchMap(() => this.songsService.selectSong(songId))
).subscribe();
return {form : form, changeSubscription: changeSubscription};
} }
private attachSync(form: FormGroup, song: Song) { private attachSync(form: FormGroup, song: Song) {
@@ -65,7 +72,8 @@ export class EditSongService {
controls.forEach(control => { controls.forEach(control => {
form.controls[control].valueChanges form.controls[control].valueChanges
.pipe( .pipe(
switchMap(value => this.songsService.patch$(song.ID, control, value)) switchMap(value => this.songsService.patch$(song.ID, control, value)),
switchMap(() => this.songsService.selectSong(song.ID))
) )
.subscribe(); .subscribe();
}); });

View File

@@ -1,3 +1,5 @@
import { HttpClient } from '@angular/common/http';
import { FileType } from './../models/files-types.model.ts';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ODataService } from 'odata-v4-ng'; import { ODataService } from 'odata-v4-ng';
import { OdataService } from './odata.service'; import { OdataService } from './odata.service';
@@ -5,6 +7,7 @@ import { Song } from '../models/song.model';
import { BehaviorSubject, Observable } from 'rxjs'; import { BehaviorSubject, Observable } from 'rxjs';
import { tap, switchMap } from 'rxjs/operators'; import { tap, switchMap } from 'rxjs/operators';
import { State } from './state'; import { State } from './state';
import { base } from './urls';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -15,7 +18,7 @@ export class SongsService extends OdataService {
public songs: BehaviorSubject<Song[]> = new BehaviorSubject<Song[]>([]); public songs: BehaviorSubject<Song[]> = new BehaviorSubject<Song[]>([]);
public selectedSong: BehaviorSubject<Song> = new BehaviorSubject<Song>(null); public selectedSong: BehaviorSubject<Song> = new BehaviorSubject<Song>(null);
constructor(odataService: ODataService) { constructor(odataService: ODataService, private httpClient: HttpClient) {
super(odataService, 'songs'); super(odataService, 'songs');
} }
@@ -27,17 +30,19 @@ export class SongsService extends OdataService {
return list; return list;
} }
public loadSongListAndGoTo$(id: number): Observable<Song[]> { public loadSongListAndGoTo$(id: number): Observable<Song> {
const properties = ['ID', 'Name', 'Number', 'SongType', 'Key', 'Tempo']; const properties = ['ID', 'Name', 'Number', 'SongType', 'Key', 'Tempo'];
const list = this.list$<Song>(properties).pipe(tap(_ => { const list = this.list$<Song>(properties).pipe(
this.songs.next(_); tap(_ => {
this.selectSong(id); this.songs.next(_);
})); }),
switchMap(() => this.selectSong(id))
);
return list; return list;
} }
public selectSong(id: number): void { public selectSong(id: number): Observable<Song> {
this.state = State.read; this.state = State.read;
const filter = this.songs.value.filter(_ => _.ID === id); const filter = this.songs.value.filter(_ => _.ID === id);
const song = filter.length === 1 ? filter[0] : null; const song = filter.length === 1 ? filter[0] : null;
@@ -45,12 +50,14 @@ export class SongsService extends OdataService {
return; return;
} }
this.get$<Song>(id, ['Text', 'Comments'], ['Files']).subscribe(_ => { const get = this.get$<Song>(id, ['Text', 'Comments'], ['Files']).pipe(tap(_ => {
song.Text = _.Text; song.Text = _.Text;
song.Comments = _.Comments; song.Comments = _.Comments;
song.Files = _.Files; song.Files = _.Files;
this.selectedSong.next(song); this.selectedSong.next(song);
}); }));
return get;
} }
public resetSelectedSong() { public resetSelectedSong() {
@@ -72,11 +79,31 @@ export class SongsService extends OdataService {
return patch; return patch;
} }
public saveNewSong$(values: any): Observable<Song[]> { public saveNewSong$(values: any): Observable<Song> {
const newSong = super const newSong = super
.post$<Song>(values) .post$<Song>(values)
.pipe(switchMap(_ => this.loadSongListAndGoTo$(_.ID))); .pipe(switchMap(_ => this.loadSongListAndGoTo$(_.ID)));
return newSong; return newSong;
}
public updateFile$(
songId: number,
fileId: number,
name: string,
fileType: FileType
): Observable<any> {
const url =
base +
'/api/songs/' +
songId +
'/files/' +
fileId +
'/edit?Name=' +
name +
'&FileType=' +
fileType;
const get = this.httpClient.get(url);
return get;
} }
} }