54472: refactoring feedback pt1

This commit is contained in:
Kristof De Langhe
2018-09-05 16:31:06 +02:00
parent 8a52bf1682
commit e7c5a2a29c
10 changed files with 133 additions and 103 deletions

View File

@@ -12,6 +12,7 @@ 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';
import { isNotEmpty } from '../../shared/empty.util';
@Component({
selector: 'ds-create-collection',
@@ -27,37 +28,34 @@ export class CreateCollectionPageComponent {
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);
if (isNotEmpty(uuid)) {
this.communityRDObs = this.communityDataService.findById(uuid);
}
});
}
onSubmit(data: any) {
const collection = Object.assign(new Collection(), {
name: data.name,
metadata: [
{ key: 'dc.description', value: data.introductory },
{ key: 'dc.description.abstract', value: data.description },
{ key: 'dc.rights', value: data.copyright },
{ key: 'dc.rights.license', value: data.license }
// TODO: metadata for news and provenance
]
});
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('');
}
})
);
const collection = Object.assign(new Collection(), {
name: data.name,
metadata: [
{ key: 'dc.description', value: data.introductory },
{ key: 'dc.description.abstract', value: data.description },
{ key: 'dc.rights', value: data.copyright },
{ key: 'dc.rights.license', value: data.license }
// TODO: metadata for news and provenance
],
owner: Observable.of(new RemoteData(false, false, true, null, Object.assign(new Community(), {
id: uuid,
uuid: uuid
})))
});
this.collectionDataService.create(collection).subscribe((rd: RemoteData<Collection>) => {
console.log(rd);
if (rd.hasSucceeded) {
this.router.navigateByUrl('');
}
});
});
}