54472: Create community/collection with parent community + error messages

This commit is contained in:
Kristof De Langhe
2018-08-20 16:09:42 +02:00
parent 2a01838941
commit dfa42acab5
8 changed files with 103 additions and 25 deletions

View File

@@ -5,6 +5,13 @@ import { NormalizedCommunity } from '../../core/cache/models/normalized-communit
import { CommunityDataService } from '../../core/data/community-data.service';
import { CollectionDataService } from '../../core/data/collection-data.service';
import { Collection } from '../../core/shared/collection.model';
import { RouteService } from '../../shared/services/route.service';
import { Router } from '@angular/router';
import { DSOSuccessResponse, ErrorResponse } from '../../core/cache/response-cache.models';
import { Observable } from 'rxjs/Observable';
import { ResponseCacheEntry } from '../../core/cache/response-cache.reducer';
import { map } from 'rxjs/operators';
import { RemoteData } from '../../core/data/remote-data';
@Component({
selector: 'ds-create-collection',
@@ -13,15 +20,38 @@ import { Collection } from '../../core/shared/collection.model';
})
export class CreateCollectionPageComponent {
public constructor(private collectionDataService: CollectionDataService) {
private error$: Observable<ErrorResponse>;
private parentUUID$: Observable<string>;
private communityRDObs: Observable<RemoteData<Community>>;
public constructor(private collectionDataService: CollectionDataService, private communityDataService: CommunityDataService, private routeService: RouteService, private router: Router) {
this.parentUUID$ = this.routeService.getQueryParameterValue('parent');
this.parentUUID$.subscribe((uuid: string) => {
this.communityRDObs = this.communityDataService.findById(uuid);
});
}
onSubmit(data: any) {
const collection = Object.assign(new Collection(), {
name: data.name
});
this.collectionDataService.create(collection);
this.parentUUID$.subscribe((uuid: string) => {
let response$: Observable<ResponseCacheEntry>;
if (uuid) {
response$ = this.collectionDataService.create(collection, uuid);
} else {
response$ = this.collectionDataService.create(collection);
}
this.error$ = response$.pipe(
map((response: ResponseCacheEntry) => {
if (!response.response.isSuccessful && response.response instanceof ErrorResponse) {
return response.response;
} else if (response.response instanceof DSOSuccessResponse) {
this.router.navigateByUrl('');
}
})
);
});
}
}