Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
- import { TagService } from '../../../tag/services/tag.service';
- import { Tag } from '../../../../classes/Tag';
- import { MatDialogRef, MatSelect } from '@angular/material';
- import { FormControl, FormGroup, Validators } from '@angular/forms';
- import { Level } from '../../../../classes/Level';
- import { LevelsService } from '../../../levels/services/level.service';
- import { Subscription } from 'rxjs/Rx';
- import { User } from '../../../../classes/User';
- import { UserService } from '../../../user/services/user.service';
- import { Skill } from '../../../../classes/Skill';
- import { SkillService } from '../../../skills/services/skill.service';
- import { UserCount } from '../../../../classes/interfaces/UserCount';
- import { DiagramData } from '../../../../classes/interfaces/DiagramData';
- @Component({
- selector: 'app-add-chart-modal',
- templateUrl: './add-chart-modal.component.html',
- styleUrls: ['./add-chart-modal.component.scss'],
- })
- export class AddChartModalComponent implements OnInit, AfterViewInit, OnDestroy {
- form: FormGroup;
- tags: Tag[];
- childTags: Tag[];
- levels: Level[] = [];
- levelsSave: Level[] = [];
- users: User[];
- subs: Subscription[] = [];
- skills: Skill[];
- @ViewChild('parentTag') parentTag: MatSelect;
- constructor(
- public tagService: TagService,
- public levelService: LevelsService,
- public userService: UserService,
- public skillService: SkillService,
- public dialogRef: MatDialogRef<AddChartModalComponent>,
- ) {}
- ngAfterViewInit() {
- setTimeout(() => {
- const tagSub = this.tagService.getAllTags().subscribe((data: Tag[]) => (this.tags = data));
- const levelSub = this.levelService.getLevels().subscribe((data: Level[]) => {
- for (const level of data) {
- if (!level.name.startsWith('Junior') && !level.name.startsWith('Architect')) {
- this.levels.push(level);
- this.levelsSave.push(level);
- }
- }
- });
- const userSub = this.userService
- .getAllUsers()
- .subscribe((data: User[]) => (this.users = data));
- const skillSub = this.skillService
- .getAllSkills()
- .subscribe((data: Skill[]) => (this.skills = data));
- this.subs.push(tagSub, levelSub, userSub, skillSub);
- }, 200);
- }
- ngOnInit() {
- this.initForm();
- }
- ngOnDestroy() {
- this.subs.forEach((sub: Subscription) => sub.unsubscribe());
- }
- initForm() {
- this.form = new FormGroup({
- title: new FormControl(''),
- subTitle: new FormControl(''),
- parentTag: new FormControl(''),
- level: new FormGroup({
- 1: new FormControl(true),
- 2: new FormControl(''),
- 3: new FormControl(''),
- 4: new FormControl(''),
- }),
- });
- }
- onParentTagChange() {
- this.findChildTags();
- }
- findChildTags() {
- const parentId = +this.form.controls.parentTag.value;
- const childTags: Tag[] = [];
- for (const tag of this.tags) {
- if (tag.parentId === parentId) {
- childTags.push(tag);
- }
- }
- this.childTags = childTags;
- }
- removeTag(tag: Tag) {
- const index = this.childTags.findIndex((t: Tag) => t === tag);
- this.childTags.splice(index, 1);
- if (this.childTags.length === 0) {
- this.parentTag.value = '';
- this.childTags = null;
- }
- }
- removeLevel(level: Level) {
- const index = this.levels.findIndex((l: Level) => l === level);
- const standard2Id = this.findLevelId('standard 2');
- const senior2Id = this.findLevelId('senior 2');
- if (level.id === standard2Id || level.id === senior2Id) {
- this.levels.splice(index - 1, 2);
- } else {
- this.levels.splice(index, 1);
- }
- }
- formSubmit() {
- const userCount: UserCount[] = this.filterUser();
- const f = this.form.controls;
- const values: DiagramData = {
- title: f.title.value,
- subTitle: f.subTitle.value,
- userCount: userCount,
- colors: null,
- };
- this.dialogRef.close(values);
- }
- filterUser(): UserCount[] {
- console.log('Child tagek:');
- console.log(this.childTags);
- const checkedLevels: number[] = [];
- // fill countArray with the childTags initially, plus append the required props - reset the array
- const countArray: UserCount[] = this.childTags.map((tag: Tag) => {
- return {
- childTag: tag,
- userList: [],
- otherCount: 0,
- seniorCount: 0,
- standardCount: 0,
- };
- });
- for (const user of this.users) {
- // skills for each user
- for (const s of user.skills) {
- // find skill
- const skill = this.skills.find((skill: Skill) => skill.id === s.skillId);
- for (const t of skill.tags) {
- for (const ct of this.childTags) {
- if (t === ct.id) {
- const res = countArray.find((item: UserCount) => item.childTag === ct);
- const userRes = res.userList.find((u: User) => u === user);
- if (!userRes) {
- res.userList.push(user);
- }
- }
- }
- }
- }
- }
- // filter users based on selected skill level
- if (this.form.get('level').get('1').value) checkedLevels.push(1);
- if (this.form.get('level').get('2').value) checkedLevels.push(2);
- if (this.form.get('level').get('3').value) checkedLevels.push(3);
- if (this.form.get('level').get('4').value) checkedLevels.push(4);
- for (const i of countArray) {
- const updatedUserList: User[] = [];
- for (const user of i.userList) {
- const foundArray: boolean[] = [];
- for (const skill of user.skills) {
- const acceptedLevels = checkedLevels;
- for (const l of acceptedLevels) {
- if (skill.level === l) {
- foundArray.push(true);
- }
- }
- }
- if (foundArray.length === user.skills.length && this.allTrue(foundArray)) {
- updatedUserList.push(user);
- }
- }
- i.userList = updatedUserList;
- }
- // count how many standard and seniors are in the filtered list
- const standard1Id = this.findLevelId('standard 1');
- const standard2Id = this.findLevelId('standard 2');
- const senior1Id = this.findLevelId('senior 1');
- const senior2Id = this.findLevelId('senior 2');
- for (const i of countArray) {
- for (const user of i.userList) {
- if (user.levelId === standard1Id || user.levelId === standard2Id) i.standardCount++;
- else if (user.levelId === senior1Id || user.levelId === senior2Id) i.seniorCount++;
- else i.otherCount++;
- }
- }
- return countArray;
- }
- allTrue(array: boolean[]): boolean {
- let allTrue = true;
- for (const item of array) {
- if (!item) allTrue = false;
- break;
- }
- return allTrue;
- }
- findLevelId(levelName: string): number {
- const result = this.levels.find((level: Level) =>
- level.name.toLowerCase().startsWith(levelName)
- );
- return result ? result.id : -1;
- }
- selectAllLevels() {
- const formGroup = this.form.get('level');
- for (let i = 1; i <= Object.keys(formGroup.value).length; i++) {
- formGroup.get(i.toString()).setValue(true);
- }
- }
- restoreLevels() {
- console.log(this.levelsSave);
- this.levels = this.levelsSave;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement