File

src/app/modules/search/components/user-filter/user-filter.component.ts

Implements

OnInit

Metadata

Index

Properties
Methods

Constructor

constructor(cdr: ChangeDetectorRef, resourceService: ResourceService, router: Router, activatedRoute: ActivatedRoute, userService: UserService, toasterService: ToasterService, profileService: ProfileService, orgDetailsService: OrgDetailsService, permissionService: PermissionService, frameworkService: FrameworkService, userSearchService: UserSearchService, formService: FormService)
Parameters :
Name Type Optional
cdr ChangeDetectorRef No
resourceService ResourceService No
router Router No
activatedRoute ActivatedRoute No
userService UserService No
toasterService ToasterService No
profileService ProfileService No
orgDetailsService OrgDetailsService No
permissionService PermissionService No
frameworkService FrameworkService No
userSearchService UserSearchService No
formService FormService No

Methods

applyFilters
applyFilters()
Returns : void
blockSelected
blockSelected(e)
Parameters :
Name Optional
e No
Returns : void
combineAllApis
combineAllApis()
Returns : void
districtSelected
districtSelected(e)
Parameters :
Name Optional
e No
Returns : void
getBlock
getBlock(districtIds)
Parameters :
Name Optional
districtIds No
Returns : void
getDistrict
getDistrict()
Returns : any
Private getFormatedFilterDetails
getFormatedFilterDetails()
Returns : any
getRoles
getRoles()
Returns : any
getSchool
getSchool(blockIds)
Parameters :
Name Optional
blockIds No
Returns : void
getSortedList
getSortedList(arr, objKey)
Parameters :
Name Optional
arr No
objKey No
Returns : any
getUserType
getUserType()
Returns : any
Private hardRefreshFilter
hardRefreshFilter()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onBlockChange
onBlockChange(blockId)
Parameters :
Name Optional
blockId No
Returns : void
onDistrictChange
onDistrictChange(districtId)
Parameters :
Name Optional
districtId No
Returns : void
onSchoolChange
onSchoolChange(schoolId)
Parameters :
Name Optional
schoolId No
Returns : void
resetFilters
resetFilters()
Returns : void
schoolSelected
schoolSelected(e)
Parameters :
Name Optional
e No
Returns : void
selectedMultiValues
selectedMultiValues(roleSelected, code)
Parameters :
Name Optional
roleSelected No
code No
Returns : void
selectedValue
selectedValue(event, code)
Parameters :
Name Optional
event No
code No
Returns : void
settelemetryData
settelemetryData()
Returns : void
sortAndCapitaliseFilters
sortAndCapitaliseFilters(object)
Parameters :
Name Optional
object No
Returns : any
sortFilters
sortFilters(object)
Parameters :
Name Optional
object No
Returns : any
Private subscribeToQueryParams
subscribeToQueryParams()
Returns : void

Properties

allBlocks
Type : any
allDistricts
Type : any
allRoles
Type : Array<RolesAndPermissions>
allSchools
Type : any
allUserType
Type : object
Default value : {}
blockIds
Type : any
class
Type : object
Default value : {}
districtIds
Type : any
Public frameworkService
Type : FrameworkService
inputData
Type : any
Default value : {}
medium
Type : object
Default value : {}
Public orgDetailsService
Type : OrgDetailsService
Public permissionService
Type : PermissionService
Public profileService
Type : ProfileService
queryParams
Type : any
refresh
Default value : true
resetInteractEdata
Type : IInteractEventEdata
Public resourceService
Type : ResourceService
selectedBlock
Type : string
selectedDistrict
Type : string
selectedSchool
Type : string
showFilters
Default value : false
stateId
Type : string
subject
Type : object
Default value : {}
submitInteractEdata
Type : IInteractEventEdata
telemetryInteractObject
Type : IInteractEventObject
Public toasterService
Type : ToasterService
userProfile
Type : any
Public userSearchService
Type : UserSearchService
Public userService
Type : UserService
userTypeList
Type : any
valueField
Type : string
Default value : 'code'
import { ResourceService, IUserData, ToasterService } from '@sunbird/shared';
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { UserService, OrgDetailsService, RolesAndPermissions, PermissionService, FrameworkService, FormService } from '@sunbird/core';
import * as _ from 'lodash-es';
import { ProfileService } from '@sunbird/profile';
import { map, catchError } from 'rxjs/operators';
import { of, combineLatest } from 'rxjs';
import { UserSearchService } from './../../services';
import { IInteractEventObject, IInteractEventEdata } from '@sunbird/telemetry';

@Component({
  selector: 'app-user-filter',
  templateUrl: './user-filter.component.html'
})

export class UserFilterComponent implements OnInit {
  valueField = 'code';
  queryParams: any;
  refresh = true;
  userProfile: any;
  stateId: string;
  allDistricts: any;
  allBlocks: any;
  allSchools: any;
  allRoles: Array<RolesAndPermissions>;
  allUserType: object = {};
  medium: object = {};
  class: object = {};
  subject: object = {};
  inputData: any = {};
  showFilters = false;
  selectedDistrict: string;
  selectedBlock: string;
  selectedSchool: string;
  submitInteractEdata: IInteractEventEdata;
  resetInteractEdata: IInteractEventEdata;
  telemetryInteractObject: IInteractEventObject;
  districtIds: any;
  blockIds: any;
  userTypeList: any;
  constructor(private cdr: ChangeDetectorRef, public resourceService: ResourceService,
    private router: Router, private activatedRoute: ActivatedRoute,
    public userService: UserService, public toasterService: ToasterService,
    public profileService: ProfileService, public orgDetailsService: OrgDetailsService,
    public permissionService: PermissionService, public frameworkService: FrameworkService,
    public userSearchService: UserSearchService,
    private formService: FormService) {
    this.router.onSameUrlNavigation = 'reload';
  }

  ngOnInit() {
    this.userService.userData$.subscribe(
      (user: IUserData) => {
        if (user && !user.err) {
          this.userProfile = user.userProfile;
          const rootOrgDetails = _.filter(this.userProfile.organisations, (org) => {
            return org.organisationId === this.userProfile.rootOrgId;
          });
          this.stateId = _.get(rootOrgDetails[0], 'locationIds[0]');
          this.subscribeToQueryParams();
          this.combineAllApis();
          this.settelemetryData();
        } else {
          this.toasterService.error(this.resourceService.messages.emsg.m0005);
        }
      });
  }

  private subscribeToQueryParams() {
    this.activatedRoute.queryParams.subscribe((params) => {
      this.queryParams = params;
      this.inputData = {};
      if (_.get(params, 'Usertype')) {
        const index = _.indexOf(params.Usertype, 'administrator');
        if (index >= 0) {
          params.Usertype[index] = 'School head or officials';
        } else if (params.Usertype === 'administrator') {
          params.Usertype = ['School head or officials'];
        }
      }
      _.forIn(params, (value, key) => this.inputData[key] = typeof value === 'string' && key !== 'key' ? [value] : value);
      this.hardRefreshFilter();
    });
  }

  combineAllApis() {
    const userType = this.getUserType();
    const frameworkDetails = this.getFormatedFilterDetails();
    const district = this.getDistrict();
    const roles = this.getRoles();
    combineLatest(userType, district, roles, frameworkDetails)
      .subscribe(val => {
        this.showFilters = true;
      });
  }

  getUserType() {
    const formServiceInputParams = { formType: 'config', formAction: 'get', contentType: 'userType', component: 'portal' };
    return this.formService.getFormConfig(formServiceInputParams).pipe(map((res: any) => {
      this.allUserType['code'] = 'Usertype';
      this.allUserType['label'] = this.resourceService.frmelmnts.lbl.userType;
      const userTypeArray = [];
      _.forEach(_.filter(res, 'visibility'), (type) => {
        userTypeArray.push({ code: type.code, name: type.name });
      });
      this.allUserType['range'] = this.sortAndCapitaliseFilters(userTypeArray);
      return 'User type API success';
    }), catchError(e => of('User type API error')));
  }

  private getFormatedFilterDetails() {
    this.frameworkService.initialize();
    return this.frameworkService.frameworkData$.pipe(map((res) => {
      const categoryMasterList = _.cloneDeep(res.frameworkdata['defaultFramework'].categories);
      // Preparing data for multi-select filter
      const medium: any = _.find(categoryMasterList, { code: 'medium' });
      medium['label'] = medium.name;
      medium['range'] = this.sortFilters(medium.terms);
      this.medium = medium;

      const gradeLevel: any = _.find(categoryMasterList, { code: 'gradeLevel' });
      gradeLevel['label'] = gradeLevel.name;
      gradeLevel['range'] = this.sortFilters(gradeLevel.terms);
      this.class = gradeLevel;

      const subject: any = _.find(categoryMasterList, { code: 'subject' });
      subject['label'] = subject.name;
      subject['range'] = this.sortFilters(subject.terms);
      this.subject = subject;
      return 'Framework API success';
    }), catchError(e => of('Framework API error')));
  }

  getDistrict() {
    if (this.stateId) {
      const requestData = { 'filters': { 'type': 'district', parentId: this.stateId } };
      return this.profileService.getUserLocation(requestData).pipe(map((res: any) => {
        this.allDistricts = this.sortAndCapitaliseFilters(res.result.response);
        // Get Blocks API call
        this.districtIds = _.map(this.allDistricts, 'id');
        this.selectedDistrict = this.queryParams.District;
        this.getBlock(this.districtIds);
        return 'District API success';
      }), catchError(e => of('District API error')));
    }
  }

  getBlock(districtIds) {
    if (!_.isEmpty(districtIds)) {
      const requestData = { 'filters': { 'type': 'block', parentId: districtIds } };
      this.profileService.getUserLocation(requestData).subscribe(res => {
        this.allBlocks = this.sortAndCapitaliseFilters(res.result.response);
        this.selectedBlock = this.queryParams.Block;
        // this.hardRefreshFilter();
        // Get school API call
        this.blockIds = _.map(this.allBlocks, 'id');
        this.getSchool(this.blockIds);
      });
    }
  }

  getSchool(blockIds) {
    if (!_.isEmpty(blockIds)) {
      const requestData = { 'filters': { locationIds: blockIds } };
      this.orgDetailsService.fetchOrgs(requestData).subscribe(res => {
        this.allSchools = _.sortBy(res.result.response.content, [(sort) => {
          return sort.orgName = _.capitalize(sort.orgName); }]);
        this.selectedSchool = this.queryParams.School;
        // this.hardRefreshFilter();
      });
    }
  }

  getRoles() {
    return this.permissionService.availableRoles$.pipe(map((res) => {
      this.allRoles = this.permissionService.allRoles;
      this.allRoles = _.filter(this.allRoles, (role) => {
        return role.role !== 'ORG_ADMIN' && role.role !== 'SYSTEM_ADMINISTRATION' && role.role !== 'ADMIN';
      });
      _.remove(this.allRoles, { role: 'PUBLIC' });
      // Preparing data for multi-select filter
      this.allRoles['code'] = 'Roles';
      this.allRoles['label'] = this.resourceService.frmelmnts.lbl.role;
      let roleArray = [];
      _.forEach(this.allRoles, (role) => {
        roleArray.push({ code: role.role, name: role.roleName });
      });
      roleArray = this.sortFilters(roleArray);
      this.allRoles['range'] = roleArray;
      return 'Roles API success';
    }), catchError(e => of('Roles API error')));
  }

  applyFilters() {
    const queryParams: any = {};
    _.forIn(this.inputData, (eachInputs: Array<any | object>, key) => {
      const formatedValue = typeof eachInputs === 'string' ? eachInputs :
        _.compact(_.map(eachInputs, value => typeof value === 'string' ? value : _.get(value, 'identifier')));
      if (formatedValue.length) {
        queryParams[key] = formatedValue;
      }
    });
    if (!_.isEmpty(queryParams)) {
      queryParams['appliedFilters'] = true;
      this.router.navigate([], { relativeTo: this.activatedRoute.parent, queryParams: queryParams });
    }
  }

  resetFilters() {
    this.inputData = {};
    this.queryParams = {};
    this.router.navigate([], { relativeTo: this.activatedRoute.parent, queryParams: this.queryParams });
    this.selectedDistrict = '';
    this.selectedBlock = '';
    this.selectedSchool = '';
    this.getBlock(this.districtIds);
    this.hardRefreshFilter();
  }

  private hardRefreshFilter() {
    this.refresh = false;
    this.cdr.detectChanges();
    this.refresh = true;
  }

  selectedValue(event, code) {
    this.inputData[code] = event;
    this.settelemetryData();
  }

  onDistrictChange(districtId) {
    this.selectedValue([districtId], 'District');
    this.selectedValue('', 'Block');
    this.selectedValue('', 'School');
    this.getBlock([districtId]);
  }

  onBlockChange(blockId) {
    this.selectedValue([blockId], 'Block');
    this.selectedValue('', 'School');
    this.getSchool([blockId]);
  }

  onSchoolChange(schoolId) {
    this.selectedValue([schoolId], 'School');
  }

  sortFilters (object) {
    return _.sortBy(object, [(sort) => {
      return sort.name; }]);
  }

  sortAndCapitaliseFilters (object) {
    return _.sortBy(object, [(sort) => {
      return sort.name = _.capitalize(sort.name); }]);
  }

  settelemetryData() {
    setTimeout(() => { // wait for model to change
      const filters = _.pickBy(this.inputData, (val, key) =>
        (!_.isEmpty(val)));
      this.submitInteractEdata = {
        id: 'submit-user-filter',
        type: 'click',
        pageid: 'user-search',
        extra: { filters: filters }
      };
    }, 5);

    this.resetInteractEdata = {
      id: 'reset-user-filter',
      type: 'click',
      pageid: 'user-search'
    };

    this.telemetryInteractObject = {
      id: this.userService.userid,
      type: 'User',
      ver: '1.0'
    };
  }

  districtSelected(e) {
    this.onDistrictChange(_.get(e, 'value.id'));
  }

  blockSelected(e) {
    this.onBlockChange(_.get(e, 'value.id'));
  }

  schoolSelected(e) {
    this.onSchoolChange(_.get(e, 'value.identifier'));
  }

  selectedMultiValues(roleSelected, code) {
    this.selectedValue(_.get(roleSelected, 'value'), code);
  }

  getSortedList(arr, objKey) {
    try {
      return arr.sort((a, b) => a[objKey].localeCompare(b[objKey], 'en', { numeric: true }))
    } catch (error) {
      return arr;
    }
  }
}
<div class="sb-prominent-filter" *ngIf="showFilters && refresh">
  <div class="ui container">

    <!--Header-->
    <h5 class="sb-prominent-filter-header">
      {{resourceService.frmelmnts?.lbl?.userFilterForm | translate}}
      <i class="mobile only large angle down icon link right-floated" 
        [ngStyle]="{'transform': collapse === true ? 'rotate(0deg)' : 'rotate(180deg)'}"
        tabindex="0" (click)="collapse = !collapse"
        title="{{collapse==true?'Expand':'Collapse'}}" 
        popupInverted>
      </i>
    </h5>
    <!--/Header-->

    <!--Filter-->
    <div class="ui active sb-prominent-filter-container" [suiCollapse]="collapse">
      
      <div *ngIf="allUserType" class="sb-prominent-filter-field">

        <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.userType | translate}}</mat-label>
          <mat-select [(ngModel)]="inputData.Usertype" role="listbox" multiple [aria-label]="resourceService.frmelmnts?.lbl?.userType" 
            class="selection" (selectionChange)="selectedMultiValues($event, allUserType?.code)">
            <mat-select-trigger>
              {{inputData.Usertype ? inputData.Usertype[0] : ''}}
              <span *ngIf="inputData.Usertype?.length > 1" class="example-additional-selection">
                (+{{inputData.Usertype.length - 1}} {{inputData.Usertype?.length === 2 ? 'other' : 'others'}})
              </span>
            </mat-select-trigger>
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let userType of allUserType?.range" [value]="userType.name"
            attr.aria-label="{{userType.name}}">{{userType.name}}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <div *ngIf="medium" class="sb-prominent-filter-field">
        
        <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.medium | transposeTerms: 'frmelmnts.lbl.medium' : resourceService?.selectedLang | translate}}</mat-label>
          <mat-select [(ngModel)]="inputData.medium" role="listbox" multiple [aria-label]="resourceService.frmelmnts?.lbl?.medium"
            class="selection" (selectionChange)="selectedMultiValues($event, medium?.code)">
            <mat-select-trigger>
              {{inputData.medium ? inputData.medium[0] : ''}}
              <span *ngIf="inputData.medium?.length > 1" class="example-additional-selection">
                (+{{inputData.medium.length - 1}} {{inputData.medium?.length === 2 ? 'other' : 'others'}})
              </span>
            </mat-select-trigger>
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let option of medium?.range" [value]="option.name"
            attr.aria-label="{{option.name}}">{{option.name}}</mat-option>
          </mat-select>
        </mat-form-field>

      </div>

      <div *ngIf="class" class="sb-prominent-filter-field">

         <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.class | transposeTerms: 'frmelmnts.lbl.class' : resourceService?.selectedLang | translate}}</mat-label>
          <mat-select [(ngModel)]="inputData.gradeLevel" role="listbox" multiple [aria-label]="resourceService.frmelmnts?.lbl?.class"
            class="selection" (selectionChange)="selectedMultiValues($event, class?.code)">
            <mat-select-trigger>
              {{inputData.gradeLevel ? inputData.gradeLevel[0] : ''}}
              <span *ngIf="inputData.gradeLevel?.length > 1" class="example-additional-selection">
                (+{{inputData.gradeLevel.length - 1}} {{inputData.gradeLevel?.length === 2 ? 'other' : 'others'}})
              </span>
            </mat-select-trigger>
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let option of getSortedList(class?.range, 'name')" [value]="option.name"
            attr.aria-label="{{option.name}}">{{option.name}}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <div *ngIf="subject" class="sb-prominent-filter-field">
        
         <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.subject | transposeTerms: 'frmelmnts.lbl.subject': resourceService?.selectedLang | translate}}</mat-label>
          <mat-select [(ngModel)]="inputData.subject" role="listbox" multiple [aria-label]="resourceService.frmelmnts?.lbl?.subject"
            class="selection" (selectionChange)="selectedMultiValues($event, subject?.code)">
            <mat-select-trigger>
              {{inputData.subject ? inputData.subject[0] : ''}}
              <span *ngIf="inputData.subject?.length > 1" class="example-additional-selection">
                (+{{inputData.subject.length - 1}} {{inputData.subject?.length === 2 ? 'other' : 'others'}})
              </span>
            </mat-select-trigger>
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let option of subject?.range" [value]="option.name"
            attr.aria-label="{{option.name}}">{{option.name}}</mat-option>
          </mat-select>
        </mat-form-field>

      </div>

      <div class="sb-prominent-filter-field">
         <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.district}}</mat-label>
          <mat-select role="listbox" [aria-label]="resourceService.frmelmnts?.lbl?.district"
            class="selection" (selectionChange)="districtSelected($event)">
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let district of allDistricts" [value]="district"
            attr.aria-label="{{district.name}}">{{district.name}}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <div class="sb-prominent-filter-field">
         <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.block | translate}}</mat-label>
          <mat-select role="listbox" [aria-label]="resourceService.frmelmnts?.lbl?.block"
            class="selection" (selectionChange)="blockSelected($event)">
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let block of allBlocks" [value]="block"
            attr.aria-label="{{block.name}}">{{block.name}}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <div class="sb-prominent-filter-field">
         <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.school | translate}}</mat-label>
          <mat-select role="listbox" [aria-label]="resourceService.frmelmnts?.lbl?.school"
            class="selection" (selectionChange)="schoolSelected($event)">
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let school of allSchools" [value]="school"
            attr.aria-label="{{school.orgName}}">{{school.orgName}}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <div *ngIf="allRoles" class="sb-prominent-filter-field">
        <mat-form-field appearance="fill" class="sb-mat__dropdown w-100 mb-16">
          <mat-label>{{resourceService.frmelmnts?.lbl?.role | translate}}</mat-label>
          <mat-select [(ngModel)]="inputData.Roles" role="listbox" multiple [aria-label]="resourceService.frmelmnts?.lbl?.role"
            class="selection" (selectionChange)="selectedMultiValues($event, allRoles?.code)">
            <mat-select-trigger>
              {{inputData.Roles ? inputData.Roles[0] : ''}}
              <span *ngIf="inputData.Roles?.length > 1" class="example-additional-selection">
                (+{{inputData.Roles.length - 1}} {{inputData.Roles?.length === 2 ? 'other' : 'others'}})
              </span>
            </mat-select-trigger>
            <mat-option class="mat-dropdown__options" role="option" *ngFor="let role of allRoles?.range" [value]="role.name"
            attr.aria-label="{{role.name}}">{{role.name}}</mat-option>
          </mat-select>
        </mat-form-field>
      </div>

      <div class="sb-prominent-filter-field">
        <button appTelemetryInteract [telemetryInteractObject]="telemetryInteractObject" [telemetryInteractEdata]="resetInteractEdata" 
          class="sb-btn sb-btn-normal sb-btn-outline-primary" tabindex="0" (click)="resetFilters()">
            {{resourceService.frmelmnts?.btn?.reset | translate}}
        </button>
        <button appTelemetryInteract [telemetryInteractObject]="telemetryInteractObject" [telemetryInteractEdata]="submitInteractEdata" 
          class="sb-btn sb-btn-normal sb-btn-primary ml-8" tabindex="0" (click)="applyFilters()">
            {{resourceService.frmelmnts?.btn?.submit | translate}}
        </button>
      </div>

    </div>
    <!--/Filter-->

  </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""