-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ProjectUsers.tsx
165 lines (149 loc) · 4.38 KB
/
ProjectUsers.tsx
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
import { Button, Grid, Typography } from "@material-ui/core";
import React from "react";
import { Translate } from "react-localize-redux";
import Modal from "react-modal";
import { toast, ToastContainer } from "react-toastify";
//styles the ToastContainer so that it appears on the upper right corner with the message.
import "react-toastify/dist/ReactToastify.min.css";
import * as backend from "../../../backend";
import { getUserId } from "../../../backend/localStorage";
import { Project } from "../../../types/project";
import { User } from "../../../types/user";
import EmailInvite from "./EmailInvite";
import UserList from "./UserList";
const customStyles = {
content: {
top: "50%",
left: "50%",
right: "auto",
bottom: "auto",
marginRight: "-50%",
transform: "translate(-50%, -50%)",
},
};
interface UserProps {
project: Project;
}
interface UserState {
allUsers: User[];
projUsers: User[];
openUser?: User;
userAvatar: { [key: string]: string };
showModal: boolean;
}
class ProjectUsers extends React.Component<UserProps, UserState> {
constructor(props: UserProps) {
super(props);
this.state = {
allUsers: [],
projUsers: [],
userAvatar: {},
showModal: false,
};
}
componentDidMount() {
Modal.setAppElement("body");
this.populateUsers();
}
handleOpenModal = () => {
this.setState({ showModal: true });
};
handleCloseModal = () => {
this.setState({ showModal: false });
};
componentDidUpdate(prevProps: UserProps) {
if (this.props.project.name !== prevProps.project.name) {
this.populateUsers();
}
}
private populateUsers() {
backend
.getAllUsersInCurrentProject()
.then((projUsers) => {
this.setState({ projUsers });
backend
.getAllUsers()
.then(async (returnedUsers) => {
this.setState((prevState) => ({
allUsers: returnedUsers.filter(
(user) => !prevState.projUsers.find((u) => u.id === user.id)
),
}));
const userAvatar = this.state.userAvatar;
let u: User | undefined;
// for loop rather than .forEach forces each await to finish
for (let i = 0; i < returnedUsers.length; i++) {
u = returnedUsers[i];
if (u.hasAvatar) {
userAvatar[u.id] = await backend.avatarSrc(u.id);
}
}
this.setState({ userAvatar });
})
.catch((err) => console.error(err));
})
.catch((err) => console.error(err));
}
addToProject(user: User) {
const currentUserId: string = getUserId();
if (user.id !== currentUserId) {
backend
.addUserRole([3, 2, 1], user)
.then(() => {
toast(<Translate id="projectSettings.invite.toastSuccess" />);
this.populateUsers();
})
.catch((err: string) => {
console.log(err);
toast(<Translate id="projectSettings.invite.toastFail" />);
});
}
}
render() {
return (
<React.Fragment>
<Grid container spacing={1}>
<UserList
allUsers={this.state.allUsers}
projUsers={this.state.projUsers}
userAvatar={this.state.userAvatar}
addToProject={(user: User) => this.addToProject(user)}
/>
<ToastContainer
position="top-right"
autoClose={5000}
hideProgressBar
newestOnTop={false}
closeOnClick
rtl={false}
pauseOnFocusLoss
draggable
pauseOnHover
/>
</Grid>
<Grid container spacing={1}>
<Grid item xs={12}>
<Typography>
<Translate id="projectSettings.invite.or" />
</Typography>
</Grid>
<Grid item xs={12}>
<Button variant="contained" onClick={this.handleOpenModal}>
<Translate id="projectSettings.invite.inviteByEmailLabel" />
</Button>
</Grid>
</Grid>
<Modal
isOpen={this.state.showModal}
style={customStyles}
shouldCloseOnOverlayClick={true}
onRequestClose={this.handleCloseModal}
>
<EmailInvite close={this.handleCloseModal} />
</Modal>
{/* </Grid> */}
</React.Fragment>
);
}
}
export default ProjectUsers;