import { AfterViewInit , Component , ElementRef , EventEmitter , Input , OnChanges , OnInit , SimpleChanges , Type } from '@angular/core' ; import { DvService , DvServiceFactory , OnEval , OnExec } from '@deja-vu/core' ; import { Group } from '../shared/group.model' ; import * as _ from 'lodash' ; import { filter , take } from 'rxjs/operators' ; @ Component ( { selector : 'group-join-leave' , templateUrl : './join-leave.component.html' , styleUrls : [ './join-leave.component.css' ] } ) export class JoinLeaveComponent implements AfterViewInit , OnEval , OnExec , OnInit , OnChanges { @ Input ( ) waitOn : string [ ] = [ ] ; @ Input ( ) memberId : string ; @ Input ( ) group : Group ; @ Input ( ) groupId : string ; @ Input ( ) joinGroupText = 'Join Group' ; @ Input ( ) leaveGroupText = 'Leave Group' ; inGroup = false ; private dvs : DvService ; constructor ( private readonly elem , private readonly dvf ) { } ngOnInit ( ) { this . dvs = this . dvf . forComponent ( this ) . withDefaultWaiter ( ) . build ( ) ; } ngAfterViewInit ( ) { this . load ( ) ; } ngOnChanges ( changes ) { if ( this . dvs && this . dvs . waiter . processChanges ( changes ) ) { this . load ( ) ; } } load ( ) { if ( this . canEval ( ) && ! this . group ) { this . dvs . eval ( ) ; } else if ( this . group ) { this . inGroup = this . groupContains ( this . group , this . memberId ) ; } } async dvOnEval ( ) { if ( this . canEval ( ) ) { if ( this . group ) { this . inGroup = this . groupContains ( this . group , this . memberId ) ; this . dvs . noRequest ( ) ; return ; } const res = await this . dvs . waitAndGet < { data : any ; } > ( '/graphql' , ( ) => ( { params : { inputs : { id : this . groupId } , extraInfo : { action : 'is-in-group' , returnFields : `
              id
              memberIds
            ` } } } ) ) ; this . group = res . data . group ; this . inGroup = this . groupContains ( this . group , this . memberId ) ; } else if ( this . dvs ) { this . dvs . noRequest ( ) ; } } joinGroup ( ) { this . dvs . exec ( ) ; } leaveGroup ( ) { this . dvs . exec ( ) ; } async dvOnExec ( ) { if ( ! this . dvs ) { return ; } await this . dvs . post < { data : { groups : Group ; } ; } > ( '/graphql' , { inputs : { groupId : this . group . id , id : this . memberId } , extraInfo : { action : this . getActionToTake ( ) } } ) ; } dvOnExecSuccess ( ) { if ( this . inGroup ) { _ . remove ( this . group . memberIds , this . memberId ) ; this . inGroup = false ; } else { this . group . memberIds . push ( this . memberId ) ; this . inGroup = true ; } } dvOnExecFailure ( reason ) { console . log ( reason . message ) ; } private canEval ( ) { return ! ! ( this . dvs ) ; } private groupContains ( group , memberId ) { return this . group ? _ . includes ( group ! . memberIds , memberId ) : false ; } private getActionToTake ( ) { return this . inGroup ? 'leave' : 'join' ; } }