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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
import * as React from "react";
import { observable, action, runInAction, computed } from "mobx";
import { SelectionManager } from "./SelectionManager";
import MainViewModal from "../views/MainViewModal";
import { observer } from "mobx-react";
import { Doc, DocListCast } from "../../fields/Doc";
import { List } from "../../fields/List";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as fa from '@fortawesome/free-solid-svg-icons';
import { library } from "@fortawesome/fontawesome-svg-core";
import SharingManager, { User } from "./SharingManager";
import { Utils } from "../../Utils";
import * as RequestPromise from "request-promise";
import Select from 'react-select';
library.add(fa.faWindowClose);
@observer
export default class GroupManager extends React.Component<{}> {
static Instance: GroupManager;
@observable private isOpen: boolean = false; // whether the menu is open or not
@observable private dialogueBoxOpacity: number = 1;
@observable private overlayOpacity: number = 0.4;
@observable private users: string[] = [];
@observable private selectedUsers: string[] | null = null;
constructor(props: Readonly<{}>) {
super(props);
GroupManager.Instance = this;
}
componentDidMount() {
console.log("mounted");
}
populateUsers = async () => {
const userList: User[] = JSON.parse(await RequestPromise.get(Utils.prepend("/getUsers")));
const currentUserIndex = userList.findIndex(user => user.email === Doc.CurrentUserEmail);
currentUserIndex !== -1 && userList.splice(currentUserIndex, 1);
return userList.map(user => user.email);
}
@computed get options() {
return this.users.map(user => ({ label: user, value: user }));
}
open = action(() => {
SelectionManager.DeselectAll();
this.isOpen = true;
this.populateUsers().then(resolved => runInAction(() => this.users = resolved));
});
close = action(() => {
this.isOpen = false;
});
get GroupManagerDoc(): Doc | undefined {
return Doc.UserDoc().globalGroupDatabase as Doc;
}
getAllGroups(): Doc[] {
const groupDoc = GroupManager.Instance.GroupManagerDoc;
return groupDoc ? DocListCast(groupDoc.data) : [];
}
getGroup(groupName: string): Doc | undefined {
const groupDoc = GroupManager.Instance.getAllGroups().find(group => group.name === groupName);
return groupDoc;
}
get adminGroupMembers(): string[] {
return JSON.parse(GroupManager.Instance.getGroup("admin")!.members as string);
}
hasEditAccess(groupDoc: Doc): boolean {
const accessList: string[] = JSON.parse(groupDoc.owners as string);
return accessList.includes(Doc.CurrentUserEmail) || GroupManager.Instance.adminGroupMembers.includes(Doc.CurrentUserEmail);
}
createGroupDoc(groupName: string, memberEmails: string[]) {
const groupDoc = new Doc;
groupDoc.groupName = groupName;
groupDoc.owners = JSON.stringify([Doc.CurrentUserEmail]);
groupDoc.members = JSON.stringify(memberEmails);
this.addGroup(groupDoc);
}
addGroup(groupDoc: Doc): boolean {
// const groupList = GroupManager.Instance.getAllGroups();
// groupList.push(groupDoc);
if (GroupManager.Instance.GroupManagerDoc) {
Doc.AddDocToList(GroupManager.Instance.GroupManagerDoc, "data", groupDoc);
// GroupManager.Instance.GroupManagerDoc.data = new List<Doc>(groupList);
return true;
}
return false;
}
deleteGroup(groupName: string): boolean {
// const groupList = GroupManager.Instance.getAllGroups();
// const index = groupList.indexOf(groupDoc);
// if (index !== -1) {
// groupList.splice(index, 1);
const groupDoc = GroupManager.Instance.getGroup(groupName);
if (groupDoc) {
if (GroupManager.Instance.GroupManagerDoc && GroupManager.Instance.hasEditAccess(groupDoc)) {
// GroupManager.Instance.GroupManagerDoc.data = new List<Doc>(groupList);
Doc.RemoveDocFromList(GroupManager.Instance.GroupManagerDoc, "data", groupDoc);
return true;
}
}
return false;
}
addMemberToGroup(groupDoc: Doc, email: string) {
if (GroupManager.Instance.hasEditAccess(groupDoc)) {
const memberList: string[] = JSON.parse(groupDoc.members as string);
!memberList.includes(email) && memberList.push(email);
groupDoc.members = JSON.stringify(memberList);
}
}
removeMemberFromGroup(groupDoc: Doc, email: string) {
if (GroupManager.Instance.hasEditAccess(groupDoc)) {
const memberList: string[] = JSON.parse(groupDoc.members as string);
const index = memberList.indexOf(email);
index !== -1 && memberList.splice(index, 1);
groupDoc.members = JSON.stringify(memberList);
}
}
@action
handleChange = (selectedOptions: any) => {
const castOptions = selectedOptions as { label: string, value: string }[];
console.log(castOptions);
this.selectedUsers = castOptions.map(option => option.value);
}
@action
resetSelection = () => {
console.log(this.selectedUsers?.[0]);
this.selectedUsers = null;
}
createGroup = () => {
this.selectedUsers = null;
}
private get groupInterface() {
return (
<div className="settings-interface">
<div className="settings-heading">
<h1>Groups</h1>
<div className={"close-button"} onClick={this.close}>
<FontAwesomeIcon icon={fa.faWindowClose} size={"lg"} />
</div>
<button onClick={this.resetSelection} style={{ width: "50%" }}>Create group</button>
</div>
<span style={{ width: "50%" }}>
<Select
isMulti={true}
isSearchable={true}
options={this.options}
onChange={this.handleChange}
placeholder={"Select users"}
value={this.selectedUsers}
/>
</span>
<span>
<input type="text" id="groupNameInput" />
</span>
</div>
);
}
render() {
return (
<MainViewModal
contents={this.groupInterface}
isDisplayed={this.isOpen}
interactive={true}
dialogueBoxDisplayedOpacity={this.dialogueBoxOpacity}
overlayDisplayedOpacity={this.overlayOpacity}
/>
);
}
}
|