Skip to content

Commit

Permalink
Resource send to child (fixes #2122) (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
singharpita authored and paulbert committed Sep 20, 2018
1 parent 3a35254 commit d677f1b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/app/courses/courses.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,8 @@ export class CoursesComponent implements OnInit, AfterViewInit, OnDestroy {

sendCourse(db: string) {
return (selected: any) => {
const coursesToSend = this.selection.selected.map(courseId => ({
db, sendTo: selected[0].code, item: findByIdInArray(this.courses.data, courseId)
}));
this.couchService.post('send_items/_bulk_docs', { 'docs': coursesToSend }).subscribe(() => {
const coursesToSend = this.selection.selected.map(id => findByIdInArray(this.courses.data, id));
this.syncService.createChildPullDoc(coursesToSend, 'courses', selected[0].code).subscribe(() => {
this.dialogRef.close();
}, () => this.planetMessageService.showAlert('There was an error sending these courses'));
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/manager-dashboard/manager-dashboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class ManagerDashboardComponent implements OnInit {

getPushedList() {
this.couchService.post(`send_items/_find`,
findDocuments({ 'sendTo': this.userService.getConfig().name }),
findDocuments({ 'sendTo': this.userService.getConfig().code }),
{ domain: this.userService.getConfig().parentDomain })
.subscribe(data => {
this.pushedItems = data.docs.reduce((items, item) => {
Expand Down
5 changes: 5 additions & 0 deletions src/app/resources/resources.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<mat-icon aria-hidden="true" class="margin-lr-3">library_add</mat-icon><span i18n>Add to myLibrary</span>
<span *ngIf="selection?.selected?.length">({{selection?.selected?.length}})</span>
</button>
<button *ngIf="planetType !== 'community'" mat-button (click)="openSendResourceDialog()" [disabled]="!selection.selected.length">
<mat-icon aria-hidden="true" class="margin-lr-3" >compare_arrows</mat-icon>
<span i18n>Push To Child</span>
<span *ngIf="selection?.selected?.length">({{selection?.selected?.length}})</span>
</button>
<button *ngIf="currentUser.isUserAdmin" mat-button (click)="deleteSelected()" [disabled]="!selection.selected.length">
<mat-icon aria-hidden="true" class="margin-lr-3">delete_forever</mat-icon><span i18n>Delete</span>
<span *ngIf="selection?.selected?.length">({{selection?.selected?.length}})</span>
Expand Down
31 changes: 29 additions & 2 deletions src/app/resources/resources.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit, ViewChild, AfterViewInit, OnDestroy } from '@angular/core';
import { CouchService } from '../shared/couchdb.service';
import { DialogsPromptComponent } from '../shared/dialogs/dialogs-prompt.component';
import { MatTableDataSource, MatPaginator, MatSort, MatDialog, PageEvent } from '@angular/material';
import { MatTableDataSource, MatPaginator, MatSort, MatDialog, PageEvent, MatDialogRef } from '@angular/material';
import { SelectionModel } from '@angular/cdk/collections';
import { Router, ActivatedRoute } from '@angular/router';
import { takeUntil, map, switchMap } from 'rxjs/operators';
Expand All @@ -15,6 +15,9 @@ import { debug } from '../debug-operator';
import { SyncService } from '../shared/sync.service';
import { FormControl } from '../../../node_modules/@angular/forms';
import { PlanetTagInputComponent } from '../shared/forms/planet-tag-input.component';
import { DialogsListService } from '../shared/dialogs/dialogs-list.service';
import { DialogsListComponent } from '../shared/dialogs/dialogs-list.component';
import { findByIdInArray } from '../shared/utils';

@Component({
templateUrl: './resources.component.html',
Expand All @@ -40,12 +43,14 @@ export class ResourcesComponent implements OnInit, AfterViewInit, OnDestroy {
pageEvent: PageEvent;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
dialogRef: MatDialogRef<DialogsListComponent>;
readonly dbName = 'resources';
message = '';
deleteDialog: any;
selection = new SelectionModel(true, []);
onDestroy$ = new Subject<void>();
parent = this.route.snapshot.data.parent;
planetType = this.userService.getConfig().planetType;
displayedColumns = [ 'select', 'title', 'rating' ];
getOpts = this.parent ? { domain: this.userService.getConfig().parentDomain } : {};
currentUser = this.userService.get();
Expand Down Expand Up @@ -73,7 +78,8 @@ export class ResourcesComponent implements OnInit, AfterViewInit, OnDestroy {
private planetMessageService: PlanetMessageService,
private userService: UserService,
private resourcesService: ResourcesService,
private syncService: SyncService
private syncService: SyncService,
private dialogsListService: DialogsListService,
) {}

ngOnInit() {
Expand Down Expand Up @@ -258,4 +264,25 @@ export class ResourcesComponent implements OnInit, AfterViewInit, OnDestroy {
this.tagInputComponent.addTag(tag);
}

openSendResourceDialog() {
this.dialogsListService.getListAndColumns('communityregistrationrequests', { 'registrationRequest': 'accepted' })
.subscribe((planet) => {
const data = { okClick: this.sendResource().bind(this),
filterPredicate: filterSpecificFields([ 'name' ]),
allowMulti: false,
...planet };
this.dialogRef = this.dialog.open(DialogsListComponent, {
data, height: '500px', width: '600px', autoFocus: false
});
});
}

sendResource() {
return (selectedPlanet: any) => {
const items = this.selection.selected.map(id => findByIdInArray(this.resources.data, id));
this.syncService.createChildPullDoc(items, 'resources', selectedPlanet[0].code).subscribe(() => {
this.dialogRef.close();
}, () => this.planetMessageService.showAlert('There was an error sending these resources'));
};
}
}
5 changes: 5 additions & 0 deletions src/app/shared/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export class SyncService {
private managerService: ManagerService
) {}

createChildPullDoc(items: any[], db, planetCode) {
const resourcesToSend = items.map(item => ({ db, sendTo: planetCode, item }));
return this.couchService.post('send_items/_bulk_docs', { 'docs': resourcesToSend });
}

confirmPasswordAndRunReplicators(replicators) {
return this.managerService.openPasswordConfirmation().pipe(switchMap((credentials) => {
return forkJoin(replicators.map((replicator) => this.sync(replicator, credentials)));
Expand Down

0 comments on commit d677f1b

Please sign in to comment.