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

@@ -1,8 +1,15 @@
<div class="container">
<div class="row">
<div class="col-12 pb-4">
<h2 id="header" class="border-bottom pb-2">{{ 'collection.create.head' | translate }}</h2>
<ng-container *ngVar="(communityRDObs | async).payload as community">
<h2 *ngIf="!community" id="header" class="border-bottom pb-2">{{ 'collection.create.head' | translate }}</h2>
<h2 *ngIf="community" id="sub-header" class="border-bottom pb-2">{{ 'collection.create.sub-head' | translate:{ parent: community.name } }}</h2>
</ng-container>
</div>
</div>
<div *ngIf="(error$ | async) as error" class="alert alert-danger">
<strong>{{error.statusCode}}</strong>
<p class="mb-0">{{error.errorMessage}}</p>
</div>
<ds-collection-form (submitted)="onSubmit($event)"></ds-collection-form>
</div>

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('');
}
})
);
});
}
}