Skip to content

Commit

Permalink
Add members to teams (fixes #1629) (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbert authored Jul 13, 2018
1 parent 0461ba0 commit ce4de62
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
11 changes: 1 addition & 10 deletions src/app/meetups/view-meetups/meetups-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,7 @@ export class MeetupsViewComponent implements OnInit, OnDestroy {
}

openInviteMemberDialog() {
this.dialogsListService.getListAndColumns('_users', {
'$nor': [
{ '_id': this.userService.get()._id },
{ '_id': 'org.couchdb.user:satellite' }
],
'$or': [
{ 'roles': { '$in': [ 'learner', 'leader' ] } },
{ 'isUserAdmin': true }
]
}).subscribe((res) => {
this.dialogsListService.getListAndColumns('_users').subscribe((res) => {
res.tableData = res.tableData.filter(tableValue => this.members.indexOf(tableValue.name) === -1);
const data = {
okClick: this.sendInvitations.bind(this),
Expand Down
20 changes: 18 additions & 2 deletions src/app/shared/dialogs/dialogs-list.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { CouchService } from '../couchdb.service';
import { map } from 'rxjs/operators';
import { findDocuments } from '../mangoQueries';
import { UserService } from '../user.service';

const listColumns = {
'resources': [ 'title' ],
Expand All @@ -13,11 +14,26 @@ const listColumns = {
@Injectable()
export class DialogsListService {

defaultSelectors = {
'_users': {
'$nor': [
{ '_id': this.userService.get()._id },
{ '_id': 'org.couchdb.user:satellite' }
],
'$or': [
{ 'roles': { '$in': [ 'learner', 'leader' ] } },
{ 'isUserAdmin': true }
]
}
};

constructor(
private couchService: CouchService
private couchService: CouchService,
private userService: UserService
) {}

getListAndColumns(db: string, selector: any = {}, opts: any = {}) {
getListAndColumns(db: string, selector?: any, opts: any = {}) {
selector = selector || this.defaultSelectors[db] || {};
return this.couchService.post(db + '/_find', findDocuments(selector), opts).pipe(map((res) => {
return { tableData: res.docs, columns: listColumns[db] };
}));
Expand Down
3 changes: 3 additions & 0 deletions src/app/teams/teams-view.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<mat-toolbar class="primary-color font-size-1">
<span class="margin-lr-3"><h3>{{team?.name}}</h3></span>
<span class="toolbar-fill"></span>
<button *ngIf="userStatus === 'member'" mat-button class="margin-lr-3" (click)="openInviteMemberDialog()" i18n>
Add Members
</button>
<ng-container [ngSwitch]="userStatus">
<button *ngSwitchCase="'member'" mat-button class="margin-lr-3" (click)="toggleMembership(team?._id, true)">
Leave
Expand Down
44 changes: 43 additions & 1 deletion src/app/teams/teams-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
import { CouchService } from '../shared/couchdb.service';
import { findDocuments } from '../shared/mangoQueries';
import { ActivatedRoute } from '@angular/router';
import { MatDialog, MatDialogRef } from '@angular/material';
import { UserService } from '../shared/user.service';
import { PlanetMessageService } from '../shared/planet-message.service';
import { TeamsService } from './teams.service';
import { Subject } from 'rxjs';
import { takeUntil, switchMap } from 'rxjs/operators';
import { DialogsListService } from '../shared/dialogs/dialogs-list.service';
import { DialogsListComponent } from '../shared/dialogs/dialogs-list.component';
import { filterSpecificFields } from '../shared/table-helpers';

@Component({
templateUrl: './teams-view.component.html',
Expand All @@ -22,13 +26,16 @@ export class TeamsViewComponent implements OnInit, OnDestroy {
userStatus = 'unrelated';
onDestroy$ = new Subject<void>();
currentUserName = this.userService.get().name;
dialogRef: MatDialogRef<DialogsListComponent>;

constructor(
private couchService: CouchService,
private userService: UserService,
private route: ActivatedRoute,
private planetMessageService: PlanetMessageService,
private teamsService: TeamsService
private teamsService: TeamsService,
private dialog: MatDialog,
private dialogsListService: DialogsListService
) {}

ngOnInit() {
Expand Down Expand Up @@ -100,4 +107,39 @@ export class TeamsViewComponent implements OnInit, OnDestroy {
});
}

openInviteMemberDialog() {
this.dialogsListService.getListAndColumns('_users').subscribe((res) => {
res.tableData = res.tableData.filter(tableValue => this.members.indexOf(tableValue.name) === -1);
const data = {
okClick: this.addMembers.bind(this),
filterPredicate: filterSpecificFields([ 'name' ]),
allowMulti: true,
...res
};
this.dialogRef = this.dialog.open(DialogsListComponent, {
data: data,
height: '500px',
width: '600px',
autoFocus: false
});
});
}

addMembers(selected: string[]) {
const selectedIds = selected.map((s: any) => s._id);
this.couchService.findAll('shelf', { selector: { '_id': { '$in': selectedIds } } }).pipe(
switchMap((shelves) => {
const newShelves = shelves.map((shelf: any) => ({
...shelf,
'myTeamIds': [].concat(shelf.myTeamIds, [ this.teamId ])
}));
return this.couchService.post('shelf/_bulk_docs', { docs: newShelves });
})
).subscribe(res => {
this.getMembers();
this.dialogRef.close();
this.planetMessageService.showMessage('Member' + (selected.length > 1 ? 's' : '') + ' added successfully');
});
}

}

0 comments on commit ce4de62

Please sign in to comment.