-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-group.component.ts
More file actions
156 lines (132 loc) · 4.06 KB
/
create-group.component.ts
File metadata and controls
156 lines (132 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Component, EventEmitter, inject, Input, Output } from '@angular/core';
import {
CdkDragDrop,
moveItemInArray,
transferArrayItem,
CdkDrag,
CdkDropList,
} from '@angular/cdk/drag-drop';
import { User } from '../../interfaces';
import { MiniUserComponent } from '../mini-user/mini-user.component';
import { MatButtonModule } from '@angular/material/button';
import { MatIcon } from '@angular/material/icon';
import { MatSnackBar } from '@angular/material/snack-bar';
import { GroupService } from '../../services/group.service';
@Component({
selector: 'app-create-group',
imports: [
MiniUserComponent,
// Angular material
CdkDropList,
CdkDrag,
MatButtonModule,
MatIcon
],
templateUrl: './create-group.component.html',
styleUrl: './create-group.component.less'
})
export class CreateGroupComponent {
// Snackbar
private snackbar = inject(MatSnackBar);
// The assignment id to which the group belongs
@Input() assignmentId?: string = "";
// The members of the class that need to be devided into groups
@Input() members: User[] = [];
// We will let subscribers know when the groups are succesfully created.
@Output() groupsCreated: EventEmitter<void> = new EventEmitter<void>();
// The groups
private _groups: User[][] = [];
// Injected services
public constructor(
private groupService: GroupService
) { }
public get groups() {
return this._groups;
}
/**
* Creates a new empty group.
* Appends an empty list to the end of the groups list.
*/
public newGroupList() {
this._groups.push([]);
}
/**
* Delete a group with the given index form the group list.
* All users in the groups are placed back in the members list.
*
* @param index The index of the group to be deleted
*/
public deleteGroupList(index: number) {
const group = this._groups[index];
this._groups.splice(index, 1);
this.members.push(...group);
}
/**
* Handle a drag and drop event between the members list and the group lists.
* If the user drops a user onto the create list it creates a new group.
*
* @param event Drag and drop event generated by Angular CDK
*/
public drop(event: CdkDragDrop<User[]>) {
const from: CdkDropList<User[]> = event.previousContainer;
const to: CdkDropList<User[]> = event.container;
if (to.id === "new-group") {
// Make a new group by dragging and dropping
const memberIndex: number = event.previousIndex;
const newGroup: User[] = [from.data[memberIndex]];
// Delete from old list
from.data.splice(memberIndex, 1);
this._groups.push(newGroup);
}
else if (from === to) {
moveItemInArray(to.data, event.previousIndex, event.currentIndex);
} else {
transferArrayItem(
from.data,
to.data,
event.previousIndex,
event.currentIndex
);
}
}
/**
* Make a call to the API to create the made groups.
*/
public createGroups(): void {
const nonEmptyGroups = this.groups.filter(group => group.length > 0);
console.log(nonEmptyGroups)
this.groupService.createGroups(nonEmptyGroups, this.assignmentId!)
.subscribe((response) => {
if (response) {
this.openSnackBar($localize`Groups created successfully.`);
this.groupsCreated.emit();
} else {
this.openSnackBar($localize`Failed to create groups.`);
}
});
}
/**
* Used by Angular to create the drag and drop lists.
* @returns The list of connected drop lists
*/
public get connectedDropLists(): string[] {
return ['members-list', 'new-group', ...this.groups.map((_, i) => `group-list-${i}`)];
}
/**
* Used in the html for making the "create" droplist which is always empty.
*/
public get emptyGroup(): User[] {
return [];
}
public partitionMembers(): void {
this.members.forEach((member) => {
this._groups.push([member]);
});
this.members = [];
}
private openSnackBar(message: string, action: string = "Ok") {
this.snackbar.open(message, action, {
duration: 2500
});
}
}