-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathNetworkMembers.jsx
More file actions
209 lines (196 loc) · 5.97 KB
/
Copy pathNetworkMembers.jsx
File metadata and controls
209 lines (196 loc) · 5.97 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
Accordion,
AccordionDetails,
AccordionSummary,
Checkbox,
Grid,
IconButton,
Typography,
} from "@material-ui/core";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore.js";
import RefreshIcon from "@material-ui/icons/Refresh.js";
import { useCallback, useEffect, useState } from "react";
import DataTable from "react-data-table-component";
import { useParams } from "react-router-dom";
import API from "utils/API.js";
import { parseValue, replaceValue, setValue } from "utils/ChangeHelper.js";
import { formatDistance } from "date-fns";
import AddMember from "./components/AddMember/AddMember.jsx";
import DeleteMember from "./components/DeleteMember/DeleteMember.jsx";
import ManagedIP from "./components/ManagedIP/ManagedIP.jsx";
import MemberName from "./components/MemberName/MemberName.jsx";
import MemberSettings from "./components/MemberSettings/MemberSettings.jsx";
import { useTranslation } from "react-i18next";
function NetworkMembers({ network }) {
const { nwid } = useParams();
const [members, setMembers] = useState([]);
const fetchData = useCallback(async () => {
try {
const members = await API.get("network/" + nwid + "/member");
setMembers(members.data);
console.log("Members:", members.data);
} catch (err) {
console.error(err);
}
}, [nwid]);
useEffect(() => {
fetchData();
const timer = setInterval(() => fetchData(), 30000);
return () => clearInterval(timer);
}, [nwid, fetchData]);
const sendReq = async (mid, data) => {
const req = await API.post("/network/" + nwid + "/member/" + mid, data);
console.log("Action:", req);
};
const { t, i18n } = useTranslation();
const handleChange =
(member, key1, key2 = null, mode = "text", id = null) =>
(event) => {
const value = parseValue(event, mode, member, key1, key2, id);
const updatedMember = replaceValue({ ...member }, key1, key2, value);
const index = members.findIndex((item) => {
return item["config"]["id"] === member["config"]["id"];
});
let mutableMembers = [...members];
mutableMembers[index] = updatedMember;
setMembers(mutableMembers);
const data = setValue({}, key1, key2, value);
sendReq(member["config"]["id"], data);
};
const columns = [
{
id: "auth",
name: t("authorized"),
minWidth: "80px",
cell: (row) => (
<Checkbox
checked={row.config.authorized}
color="primary"
onChange={handleChange(row, "config", "authorized", "checkbox")}
/>
),
},
{
id: "address",
name: t("address"),
minWidth: "150px",
cell: (row) => (
<Typography variant="body2">{row.config.address}</Typography>
),
},
{
id: "name",
name: t("name") + "/" + t("description"),
minWidth: "250px",
cell: (row) => <MemberName member={row} handleChange={handleChange} />,
},
{
id: "ips",
name: t("managedIPs"),
minWidth: "220px",
cell: (row) => <ManagedIP member={row} handleChange={handleChange} />,
},
{
id: "lastSeen",
name: t("lastSeen"),
minWidth: "100px",
cell: (row) =>
row.online === 1 ? (
<Typography style={{ color: "#008000" }}>{"ONLINE"}</Typography>
) : row.controllerId === row.config.address ? (
<Typography style={{ color: "#c5e31e" }}>{"CONTROLLER"}</Typography>
) : row.online === 0 ? (
<Typography color="error">
{row.lastOnline !== 0
? formatDistance(row.lastOnline, row.clock, {
includeSeconds: false,
addSuffix: true,
})
: "OFFLINE"}
</Typography>
) : (
<Typography style={{ color: "#f1c232" }}>{"RELAYED"}</Typography>
),
},
{
id: "physicalip",
name: t("version") + " / " + t("physIp") + " / " + t("latency"),
minWidth: "220px",
cell: (row) =>
row.online === 1 ? (
<p>
{"v" +
row.config.vMajor +
"." +
row.config.vMinor +
"." +
row.config.vRev}
<br />
{row.physicalAddress + "/" + row.physicalPort}
<br />
{"(" + row.latency + " ms)"}
</p>
) : (
""
),
},
{
id: "delete",
name: t("settings"),
minWidth: "50px",
right: true,
cell: (row) => (
<>
<MemberSettings
member={row}
network={network}
handleChange={handleChange}
/>
<DeleteMember nwid={nwid} mid={row.config.id} callback={fetchData} />
</>
),
},
];
return (
<Accordion defaultExpanded={true}>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Typography>{t("member", { count: members.length })}</Typography>
</AccordionSummary>
<AccordionDetails>
<Grid container direction="column" spacing={3}>
<IconButton color="primary" onClick={fetchData}>
<RefreshIcon />
</IconButton>
<Grid container>
{members.length ? (
<DataTable
noHeader={true}
columns={columns}
data={[...members]}
/>
) : (
<Grid
container
spacing={0}
direction="column"
alignItems="center"
justifyContent="center"
style={{
minHeight: "50vh",
}}
>
<Typography variant="h6" style={{ padding: "10%" }}>
{t("noDevices")} <b>{nwid}</b>.
</Typography>
</Grid>
)}
</Grid>
<Grid item>
<AddMember nwid={nwid} callback={fetchData} />
</Grid>
</Grid>
</AccordionDetails>
</Accordion>
);
}
export default NetworkMembers;