-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
UserList.tsx
121 lines (109 loc) · 3.03 KB
/
UserList.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
import {
Avatar,
Button,
Grid,
Input,
List,
ListItem,
ListItemText,
Typography,
} from "@material-ui/core";
import { DeleteForever, VpnKey } from "@material-ui/icons";
import * as React from "react";
import {
LocalizeContextProps,
Translate,
withLocalize,
} from "react-localize-redux";
import { getUserId } from "../../../backend/localStorage";
import theme from "../../../types/theme";
import { User } from "../../../types/user";
interface UserListProps {
allUsers: User[];
userAvatar: { [key: string]: string };
handleOpenModal: (user: User) => void;
}
interface UserListState {
currentUserId: string;
filterInput: string;
filteredUsers: User[];
prevFilterInput?: string;
}
class UserList extends React.Component<
UserListProps & LocalizeContextProps,
UserListState
> {
constructor(props: UserListProps & LocalizeContextProps) {
super(props);
this.state = {
currentUserId: getUserId(),
filterInput: "",
filteredUsers: [],
};
}
componentDidUpdate() {
if (this.state.prevFilterInput !== this.state.filterInput) {
this.handleChange(this.state.filterInput);
this.setState((state) => ({ prevFilterInput: state.filterInput }));
}
}
handleChange(event: string) {
let filteredUsers: User[] = [];
if (event.length >= 1) {
filteredUsers = this.props.allUsers.filter((user) => {
const name = user.name.toLowerCase();
const username = user.username.toLowerCase();
const email = user.email.toLowerCase();
const filter = event.toLowerCase();
return (
name.includes(filter) ||
username.includes(filter) ||
email.includes(filter)
);
});
}
this.setState({
filterInput: event,
filteredUsers: filteredUsers,
});
}
render() {
return (
<React.Fragment>
<Grid item xs={12}>
<Typography>
<Translate id="projectSettings.invite.searchTitle" />
</Typography>
<Input
type="text"
onChange={(e) => this.handleChange(e.target.value)}
placeholder="Search..."
/>
<List>
{this.state.filteredUsers.map((user) => (
<ListItem key={user.id}>
<Avatar
alt="User Avatar"
src={this.props.userAvatar[user.id]}
style={{ marginRight: theme.spacing(1) }}
/>
<ListItemText primary={`${user.name} (${user.username})`} />
{user.id !== this.state.currentUserId &&
(user.isAdmin ? (
<Button disabled>
<VpnKey />
</Button>
) : (
<Button onClick={() => this.props.handleOpenModal(user)}>
<DeleteForever />
</Button>
))}
</ListItem>
))}
</List>
</Grid>
</React.Fragment>
);
}
}
export default withLocalize(UserList);