src/app/plugins/profile/components/choose-user/choose-user.component.ts
OnInit
OnDestroy
selector | app-choose-user |
styleUrls | ./choose-user.component.scss |
templateUrl | ./choose-user.component.html |
Properties |
|
Methods |
constructor(userService: UserService, navigationHelperService: NavigationHelperService, toasterService: ToasterService, router: Router, utilService: UtilService, resourceService: ResourceService, telemetryService: TelemetryService, configService: ConfigService, managedUserService: ManagedUserService, activatedRoute: ActivatedRoute, courseService: CoursesService, connectionService: ConnectionService, layoutService: LayoutService)
|
||||||||||||||||||||||||||||||||||||||||||
Parameters :
|
closeSwitchUser |
closeSwitchUser()
|
Returns :
void
|
getManagedUserList |
getManagedUserList()
|
Returns :
void
|
goBack |
goBack()
|
Returns :
void
|
initializeManagedUser |
initializeManagedUser()
|
Returns :
void
|
navigateToCreateUser |
navigateToCreateUser()
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
selectUser | ||||
selectUser(event)
|
||||
Parameters :
Returns :
void
|
setTelemetryData |
setTelemetryData()
|
Returns :
void
|
switchUser |
switchUser()
|
Returns :
void
|
telemetryImpressionEvent |
telemetryImpressionEvent()
|
Returns :
void
|
Public activatedRoute |
Type : ActivatedRoute
|
Public courseService |
Type : CoursesService
|
instance |
Type : string
|
isConnected |
Default value : false
|
layoutConfiguration |
Type : any
|
Public layoutService |
Type : LayoutService
|
memberCardConfig |
Type : object
|
Default value : {
size: this.configService.constants.SIZE.MEDIUM,
isSelectable: true,
view: this.configService.constants.VIEW.HORIZONTAL,
isBold: false
}
|
Public navigationHelperService |
Type : NavigationHelperService
|
Public resourceService |
Type : ResourceService
|
Public router |
Type : Router
|
selectedUser |
Type : any
|
submitInteractEdata |
Type : IInteractEventEdata
|
Public toasterService |
Type : ToasterService
|
Public unsubscribe |
Default value : new Subject<void>()
|
unsubscribe$ |
Default value : new Subject<void>()
|
userDataSubscription |
Type : any
|
userList |
Type : []
|
Default value : []
|
Public userService |
Type : UserService
|
import {Component, OnDestroy, OnInit} from '@angular/core';
import {CoursesService, ManagedUserService, UserService} from '@sunbird/core';
import {
ConfigService,
ResourceService,
ToasterService, ConnectionService,
IUserData, NavigationHelperService, UtilService, LayoutService
} from '@sunbird/shared';
import {ActivatedRoute, Router} from '@angular/router';
import {IInteractEventEdata, TelemetryService} from '@sunbird/telemetry';
import {environment} from '@sunbird/environment';
import * as _ from 'lodash-es';
import { zip, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Component({
selector: 'app-choose-user',
templateUrl: './choose-user.component.html',
styleUrls: ['./choose-user.component.scss']
})
export class ChooseUserComponent implements OnInit, OnDestroy {
constructor(public userService: UserService, public navigationHelperService: NavigationHelperService,
public toasterService: ToasterService, public router: Router, private utilService: UtilService,
public resourceService: ResourceService, private telemetryService: TelemetryService,
private configService: ConfigService, private managedUserService: ManagedUserService,
public activatedRoute: ActivatedRoute, public courseService: CoursesService,
private connectionService: ConnectionService, public layoutService: LayoutService) {
this.instance = (<HTMLInputElement>document.getElementById('instance'))
? (<HTMLInputElement>document.getElementById('instance')).value.toUpperCase() : 'SUNBIRD';
}
userList = [];
instance: string;
memberCardConfig = {
size: this.configService.constants.SIZE.MEDIUM,
isSelectable: true,
view: this.configService.constants.VIEW.HORIZONTAL,
isBold: false
};
selectedUser: any;
submitInteractEdata: IInteractEventEdata;
userDataSubscription: any;
isConnected = false;
layoutConfiguration: any;
unsubscribe$ = new Subject<void>();
public unsubscribe = new Subject<void>();
ngOnInit() {
this.navigationHelperService.setNavigationUrl();
this.userDataSubscription = this.userService.userData$.subscribe((user: IUserData) => {
this.getManagedUserList();
});
this.telemetryImpressionEvent();
this.setTelemetryData();
this.connectionService.monitor()
.pipe(takeUntil(this.unsubscribe$)).subscribe(isConnected => {
this.isConnected = isConnected;
});
this.layoutConfiguration = this.layoutService.initlayoutConfig();
this.layoutService.switchableLayout().
pipe(takeUntil(this.unsubscribe)).subscribe(layoutConfig => {
if (layoutConfig != null) {
this.layoutConfiguration = layoutConfig.layout;
}
});
}
ngOnDestroy() {
this.unsubscribe$.next();
this.unsubscribe$.complete();
if (this.userDataSubscription) {
this.userDataSubscription.unsubscribe();
}
}
goBack() {
this.navigationHelperService.goBack();
}
switchUser() {
const userId = this.selectedUser.identifier;
const switchUserRequest = {
userId: this.selectedUser.identifier,
isManagedUser: this.selectedUser.managedBy ? true : false
};
this.managedUserService.initiateSwitchUser(switchUserRequest).subscribe((data) => {
this.managedUserService.setSwitchUserData(userId, _.get(data, 'result.userSid'));
this.userService.userData$.subscribe((user: IUserData) => {
if (user && !user.err && user.userProfile.userId === userId) {
if (this.utilService.isDesktopApp && !this.isConnected) {
this.initializeManagedUser();
} else {
this.courseService.getEnrolledCourses().pipe(takeUntil(this.unsubscribe$)).subscribe((enrolledCourse) => {
this.initializeManagedUser();
});
}
}
});
}, (err) => {
this.toasterService.error(_.get(this.resourceService, 'messages.emsg.m0005'));
}
);
}
telemetryImpressionEvent() {
this.telemetryService.impression({
context: {
env: this.activatedRoute.snapshot.data.telemetry.env
},
edata: {
type: this.activatedRoute.snapshot.data.telemetry.type,
pageid: this.activatedRoute.snapshot.data.telemetry.pageid,
uri: this.activatedRoute.snapshot.data.telemetry.uri,
duration: this.navigationHelperService.getPageLoadTime()
}
});
}
selectUser(event) {
this.selectedUser = _.get(event, 'data.data');
if (this.selectedUser.selected) {
this.selectedUser = null;
}
const userId = _.get(event, 'data.data.identifier');
_.forEach(this.userList, (userData, index) => {
if (userData.identifier === userId) {
this.userList[index].selected = !userData.selected;
} else {
this.userList[index].selected = userData.identifier === userId;
}
});
}
setTelemetryData() {
this.submitInteractEdata = {
id: 'submit-choose-managed-user',
type: 'click',
pageid: this.activatedRoute.snapshot.data.telemetry.pageid,
};
}
navigateToCreateUser() {
this.router.navigate(['/profile/create-managed-user']);
}
getManagedUserList() {
const requests = [this.managedUserService.managedUserList$];
if (this.userService.userProfile.managedBy) {
requests.push(this.managedUserService.getParentProfile());
}
zip(...requests).subscribe((data) => {
let userListToProcess = _.get(data[0], 'result.response.content');
if (data[1]) {
userListToProcess = [data[1]].concat(userListToProcess);
}
this.userList = this.managedUserService.processUserList(userListToProcess, this.userService.userid);
}, (err) => {
this.toasterService.error(_.get(this.resourceService, 'messages.emsg.m0005'));
});
}
getTelemetryContext() {
const userProfile = this.userService.userProfile;
const buildNumber = (<HTMLInputElement>document.getElementById('buildNumber'));
const version = buildNumber && buildNumber.value ? buildNumber.value.slice(0, buildNumber.value.lastIndexOf('.')) : '1.0';
return {
userOrgDetails: {
userId: userProfile.userId,
rootOrgId: userProfile.rootOrgId,
rootOrg: userProfile.rootOrg,
organisationIds: userProfile.hashTagIds
},
config: {
pdata: {
id: this.userService.appId,
ver: version,
pid: this.configService.appConfig.TELEMETRY.PID
},
endpoint: this.configService.urlConFig.URLS.TELEMETRY.SYNC,
apislug: this.configService.urlConFig.URLS.CONTENT_PREFIX,
host: '',
uid: userProfile.userId,
sid: this.userService.sessionId,
channel: _.get(userProfile, 'rootOrg.hashTagId'),
env: 'home',
enableValidation: environment.enableTelemetryValidation,
timeDiff: this.userService.getServerTimeDiff
}
};
}
closeSwitchUser() {
this.navigationHelperService.navigateToLastUrl();
}
initializeManagedUser() {
this.telemetryService.syncEvents(false);
this.telemetryService.setInitialization(false);
this.telemetryService.initialize(this.getTelemetryContext());
this.toasterService.custom({
message: this.managedUserService.getMessage(_.get(this.resourceService, 'messages.imsg.m0095'),
this.selectedUser.firstName),
class: 'sb-toaster sb-toast-success sb-toast-normal'
});
setTimeout(() => {
this.utilService.redirect('/resources');
}, 5100);
}
}
<!--profile Content Header -->
<div [ngClass]="layoutConfiguration ? 'sb-back-actionbar' : 'sb-bg-white cc-player__btn-back'" class="relative position mt-0">
<div class="ui container py-0 px-0 d-flex flex-ai-center">
<button type="button" (click)="goBack()" [ngClass]="layoutConfiguration ? 'sb-btn-primary sb-btn-round' : 'sb-btn-link sb-btn-link-primary sb-left-icon-btn px-0'" class="sb-btn sb-btn-normal" tabindex="0" attr.aria-label="{{resourceService?.frmelmnts?.btn?.back}}">
<i class="icon-svg icon-svg--xxs icon-back mr-4"><svg class="icon icon-svg--primary">
<use xlink:href="assets/images/sprite.svg#arrow-long-left"></use>
</svg></i>
<span>{{resourceService?.frmelmnts?.btn?.back}}</span>
</button>
<div class="content-header__content w-100 ml-16">
<div class="d-flex flex-dc flex-basis-1 mr-32 min-w-0 content-header__content__title">
<div class="content-header__title font-weight-bold ellipsis text-left" tabindex="0" role="heading" aria-level="2">{{resourceService.frmelmnts?.lbl?.useInstanceAs | interpolate:'{instance}': instance}}</div>
</div>
</div>
</div>
</div>
<div [ngClass]="layoutConfiguration ? 'sbt-inside-page-container' : ''">
<div class="user-profile-box my-32 p-0">
<div class="d-flex flex-jc-space-between px-16 pt-16">
<div class="user-profile-box__title">
{{resourceService.frmelmnts?.lbl?.useInstanceAs | interpolate:'{instance}': instance}}
</div>
</div>
<div class="px-16">
<button (click)="navigateToCreateUser()" type="submit" appOnlineOnly tabindex="0"
class="sb-btn sb-btn-sm sb-btn-outline-secondary sb-left-icon-btn w-100 mt-16 mb-24">
<i class="chevron plus icon"></i> {{resourceService.frmelmnts?.lbl?.addUser}}
</button>
</div>
<div class="px-16 mb-16 member-list-container">
<sb-member-list [layoutConfig]=memberCardConfig
[memberList]="userList"
[isMenu]="false"
(cardClick)="selectUser($event)">
</sb-member-list>
</div>
<div class="user-profile-box__actions pb-16 px-16">
<button type="submit" class="sb-btn sb-btn-normal" (click)="switchUser()" tabindex="0"
[disabled]="!selectedUser" appOnlineOnly
appTelemetryInteract [telemetryInteractEdata]="submitInteractEdata"
[ngClass]="{'sb-btn-disabled': !selectedUser, 'sb-btn-primary': selectedUser}">
{{resourceService.frmelmnts?.lbl?.switchUser}}
</button>
<button type="submit" (click)="closeSwitchUser()" tabindex="0"
class="sb-btn sb-btn-normal sb-btn-outline-primary mr-8">{{resourceService.frmelmnts?.btn?.cancel}}</button>
</div>
</div>
</div>
./choose-user.component.scss
@use "@project-sunbird/sb-styles/assets/mixins/mixins" as *;
@use 'pages/user-mgmt' as *;