Advertisement
attilan

User filter for modal

Mar 20th, 2019
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
  2. import { TagService } from '../../../tag/services/tag.service';
  3. import { Tag } from '../../../../classes/Tag';
  4. import { MatDialogRef, MatSelect } from '@angular/material';
  5. import { FormControl, FormGroup, Validators } from '@angular/forms';
  6. import { Level } from '../../../../classes/Level';
  7. import { LevelsService } from '../../../levels/services/level.service';
  8. import { Subscription } from 'rxjs/Rx';
  9. import { User } from '../../../../classes/User';
  10. import { UserService } from '../../../user/services/user.service';
  11. import { Skill } from '../../../../classes/Skill';
  12. import { SkillService } from '../../../skills/services/skill.service';
  13. import { UserCount } from '../../../../classes/interfaces/UserCount';
  14. import { DiagramData } from '../../../../classes/interfaces/DiagramData';
  15.  
  16. @Component({
  17.   selector: 'app-add-chart-modal',
  18.   templateUrl: './add-chart-modal.component.html',
  19.   styleUrls: ['./add-chart-modal.component.scss'],
  20. })
  21. export class AddChartModalComponent implements OnInit, AfterViewInit, OnDestroy {
  22.   form: FormGroup;
  23.   tags: Tag[];
  24.   childTags: Tag[];
  25.   levels: Level[] = [];
  26.   levelsSave: Level[] = [];
  27.   users: User[];
  28.   subs: Subscription[] = [];
  29.   skills: Skill[];
  30.  
  31.   @ViewChild('parentTag') parentTag: MatSelect;
  32.  
  33.   constructor(
  34.     public tagService: TagService,
  35.     public levelService: LevelsService,
  36.     public userService: UserService,
  37.     public skillService: SkillService,
  38.     public dialogRef: MatDialogRef<AddChartModalComponent>,
  39.   ) {}
  40.  
  41.   ngAfterViewInit() {
  42.     setTimeout(() => {
  43.       const tagSub = this.tagService.getAllTags().subscribe((data: Tag[]) => (this.tags = data));
  44.       const levelSub = this.levelService.getLevels().subscribe((data: Level[]) => {
  45.         for (const level of data) {
  46.           if (!level.name.startsWith('Junior') && !level.name.startsWith('Architect')) {
  47.             this.levels.push(level);
  48.             this.levelsSave.push(level);
  49.           }
  50.         }
  51.       });
  52.       const userSub = this.userService
  53.         .getAllUsers()
  54.         .subscribe((data: User[]) => (this.users = data));
  55.       const skillSub = this.skillService
  56.         .getAllSkills()
  57.         .subscribe((data: Skill[]) => (this.skills = data));
  58.       this.subs.push(tagSub, levelSub, userSub, skillSub);
  59.     }, 200);
  60.   }
  61.  
  62.   ngOnInit() {
  63.     this.initForm();
  64.   }
  65.  
  66.   ngOnDestroy() {
  67.     this.subs.forEach((sub: Subscription) => sub.unsubscribe());
  68.   }
  69.  
  70.   initForm() {
  71.     this.form = new FormGroup({
  72.       title: new FormControl(''),
  73.       subTitle: new FormControl(''),
  74.       parentTag: new FormControl(''),
  75.       level: new FormGroup({
  76.         1: new FormControl(true),
  77.         2: new FormControl(''),
  78.         3: new FormControl(''),
  79.         4: new FormControl(''),
  80.       }),
  81.     });
  82.   }
  83.  
  84.   onParentTagChange() {
  85.     this.findChildTags();
  86.   }
  87.  
  88.   findChildTags() {
  89.     const parentId = +this.form.controls.parentTag.value;
  90.     const childTags: Tag[] = [];
  91.     for (const tag of this.tags) {
  92.       if (tag.parentId === parentId) {
  93.         childTags.push(tag);
  94.       }
  95.     }
  96.     this.childTags = childTags;
  97.   }
  98.  
  99.   removeTag(tag: Tag) {
  100.     const index = this.childTags.findIndex((t: Tag) => t === tag);
  101.     this.childTags.splice(index, 1);
  102.     if (this.childTags.length === 0) {
  103.       this.parentTag.value = '';
  104.       this.childTags = null;
  105.     }
  106.   }
  107.  
  108.   removeLevel(level: Level) {
  109.     const index = this.levels.findIndex((l: Level) => l === level);
  110.     const standard2Id = this.findLevelId('standard 2');
  111.     const senior2Id = this.findLevelId('senior 2');
  112.     if (level.id === standard2Id || level.id === senior2Id) {
  113.       this.levels.splice(index - 1, 2);
  114.     } else {
  115.       this.levels.splice(index, 1);
  116.     }
  117.   }
  118.  
  119.   formSubmit() {
  120.     const userCount: UserCount[] = this.filterUser();
  121.     const f = this.form.controls;
  122.     const values: DiagramData = {
  123.       title: f.title.value,
  124.       subTitle: f.subTitle.value,
  125.       userCount: userCount,
  126.       colors: null,
  127.     };
  128.     this.dialogRef.close(values);
  129.   }
  130.  
  131.   filterUser(): UserCount[] {
  132.     console.log('Child tagek:');
  133.     console.log(this.childTags);
  134.     const checkedLevels: number[] = [];
  135.  
  136.     // fill countArray with the childTags initially, plus append the required props - reset the array
  137.     const countArray: UserCount[] = this.childTags.map((tag: Tag) => {
  138.       return {
  139.         childTag: tag,
  140.         userList: [],
  141.         otherCount: 0,
  142.         seniorCount: 0,
  143.         standardCount: 0,
  144.       };
  145.     });
  146.  
  147.     for (const user of this.users) {
  148.       // skills for each user
  149.       for (const s of user.skills) {
  150.         // find skill
  151.         const skill = this.skills.find((skill: Skill) => skill.id === s.skillId);
  152.         for (const t of skill.tags) {
  153.           for (const ct of this.childTags) {
  154.             if (t === ct.id) {
  155.               const res = countArray.find((item: UserCount) => item.childTag === ct);
  156.               const userRes = res.userList.find((u: User) => u === user);
  157.               if (!userRes) {
  158.                 res.userList.push(user);
  159.               }
  160.             }
  161.           }
  162.         }
  163.       }
  164.     }
  165.  
  166.     // filter users based on selected skill level
  167.     if (this.form.get('level').get('1').value) checkedLevels.push(1);
  168.     if (this.form.get('level').get('2').value) checkedLevels.push(2);
  169.     if (this.form.get('level').get('3').value) checkedLevels.push(3);
  170.     if (this.form.get('level').get('4').value) checkedLevels.push(4);
  171.  
  172.     for (const i of countArray) {
  173.       const updatedUserList: User[] = [];
  174.       for (const user of i.userList) {
  175.         const foundArray: boolean[] = [];
  176.         for (const skill of user.skills) {
  177.           const acceptedLevels = checkedLevels;
  178.           for (const l of acceptedLevels) {
  179.             if (skill.level === l) {
  180.               foundArray.push(true);
  181.             }
  182.           }
  183.         }
  184.         if (foundArray.length === user.skills.length && this.allTrue(foundArray)) {
  185.           updatedUserList.push(user);
  186.         }
  187.       }
  188.       i.userList = updatedUserList;
  189.     }
  190.  
  191.     // count how many standard and seniors are in the filtered list
  192.     const standard1Id = this.findLevelId('standard 1');
  193.     const standard2Id = this.findLevelId('standard 2');
  194.     const senior1Id = this.findLevelId('senior 1');
  195.     const senior2Id = this.findLevelId('senior 2');
  196.  
  197.     for (const i of countArray) {
  198.       for (const user of i.userList) {
  199.         if (user.levelId === standard1Id || user.levelId === standard2Id) i.standardCount++;
  200.         else if (user.levelId === senior1Id || user.levelId === senior2Id) i.seniorCount++;
  201.         else i.otherCount++;
  202.       }
  203.     }
  204.  
  205.     return countArray;
  206.   }
  207.  
  208.   allTrue(array: boolean[]): boolean {
  209.     let allTrue = true;
  210.     for (const item of array) {
  211.       if (!item) allTrue = false;
  212.       break;
  213.     }
  214.     return allTrue;
  215.   }
  216.  
  217.   findLevelId(levelName: string): number {
  218.     const result = this.levels.find((level: Level) =>
  219.       level.name.toLowerCase().startsWith(levelName)
  220.     );
  221.     return result ? result.id : -1;
  222.   }
  223.  
  224.   selectAllLevels() {
  225.     const formGroup = this.form.get('level');
  226.     for (let i = 1; i <= Object.keys(formGroup.value).length; i++) {
  227.       formGroup.get(i.toString()).setValue(true);
  228.     }
  229.   }
  230.  
  231.   restoreLevels() {
  232.     console.log(this.levelsSave);
  233.     this.levels = this.levelsSave;
  234.   }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement