-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathNetworkRules.jsx
154 lines (142 loc) · 4.1 KB
/
NetworkRules.jsx
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
import {
Accordion,
AccordionDetails,
AccordionSummary,
Button,
Divider,
Grid,
Hidden,
Snackbar,
Typography,
} from "@material-ui/core";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore.js";
import CodeMirror from "@uiw/react-codemirror";
import "codemirror/theme/3024-day.css";
import { compile } from "external/RuleCompiler.js";
import debounce from "lodash/debounce.js";
import { useState } from "react";
import API from "utils/API.js";
import { useTranslation, Trans } from "react-i18next";
function NetworkRules({ network, callback }) {
const { t, i18n } = useTranslation();
const [editor, setEditor] = useState(null);
const [flowData, setFlowData] = useState({
rules: [...network.config.rules],
capabilities: [...network.config.capabilities],
tags: [...network.config.tags],
});
const [tagCapByNameData, setTagCapByNameData] = useState({
tagsByName: network.tagsByName || {},
capabilitiesByName: network.capabilitiesByName || {},
});
const [errors, setErrors] = useState([]);
const [snackbarOpen, setSnackbarOpen] = useState(false);
const saveChanges = async () => {
if (editor) {
const req = await API.post("/network/" + network["config"]["id"], {
config: { ...flowData },
rulesSource: editor.getValue(),
...tagCapByNameData,
});
console.log("Action", req);
setSnackbarOpen(true);
const timer = setTimeout(() => {
setSnackbarOpen(false);
}, 1500);
callback();
return () => clearTimeout(timer);
}
};
const onChange = debounce((event) => {
const src = event.getValue();
setEditor(event);
let rules = [],
caps = {},
tags = {};
const res = compile(src, rules, caps, tags);
if (!res) {
let capabilitiesByName = {};
for (var key in caps) {
capabilitiesByName[key] = caps[key].id;
}
let tagsByName = { ...tags };
for (let key in tags) {
tags[key] = { id: tags[key].id, default: tags[key].default };
}
setFlowData({
rules: [...rules],
capabilities: [...Object.values(caps)],
tags: [...Object.values(tags)],
});
setTagCapByNameData({
tagsByName: tagsByName,
capabilitiesByName: capabilitiesByName,
});
setErrors([]);
} else {
setErrors(res);
}
}, 100);
return (
<Accordion>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Typography>{t("flowRules")}</Typography>
</AccordionSummary>
<AccordionDetails>
{/* Important note: value in CodeMirror instance means INITAIL VALUE
or it could be used to replace editor state with the new value.
No need to update on every user character input Flow Rules
*/}
<CodeMirror
value={network["rulesSource"]}
onChange={onChange}
options={{ tabSize: 2, lineWrapping: true }}
/>
<Hidden mdDown>
<div>
<CodeMirror
value={JSON.stringify(flowData, null, 2)}
width="100%"
height="50%"
options={{
theme: "3024-day",
readOnly: true,
lineNumbers: false,
lineWrapping: true,
}}
/>
</div>
</Hidden>
<Divider />
<Grid
item
style={{
margin: "1%",
display: "block",
overflowWrap: "break-word",
width: "250px",
}}
>
{errors.length ? (
<Typography color="error">
{"[" + errors[0] + ":" + errors[1] + "] " + errors[2]}
</Typography>
) : (
<Button variant="contained" color="primary" onClick={saveChanges}>
{t("saveChanges")}
</Button>
)}
</Grid>
<Snackbar
open={snackbarOpen}
anchorOrigin={{
vertical: "top",
horizontal: "center",
}}
message="Saved"
/>
</AccordionDetails>
</Accordion>
);
}
export default NetworkRules;