-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathEnvironments.tsx
258 lines (238 loc) · 6.85 KB
/
Environments.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import React, { memo, useEffect } from "react";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import Link from "@mui/material/Link";
import useTheme from "@mui/material/styles/useTheme";
import { EnvironmentsList } from "./EnvironmentsList";
import { debounce } from "lodash";
import { EnvironmentsSearch } from "./EnvironmentsSearch";
import { useLazyFetchEnvironmentsQuery } from "../environmentsApiSlice";
import {
useLazyFetchNamespacesQuery,
useLazyFetchPrimaryNamespaceQuery
} from "../../../features/namespaces";
import { environmentsSlice } from "../reducer";
import { dataFetched } from "../../../features/namespaces/reducer";
import {
isNamespaceListed,
checkMyPrimaryNamespace,
namespacesPermissionsMapper
} from "../../../utils/helpers/namespaces";
import { Namespace } from "../../../common/models";
import { BookIcon } from "../../../components";
import { useAppDispatch, useAppSelector } from "../../../hooks";
export interface IBaseEnvironments {
refreshEnvironments: boolean;
onUpdateRefreshEnvironments: (isUpdated: boolean) => void;
}
const BaseEnvironments = ({
refreshEnvironments,
onUpdateRefreshEnvironments
}: IBaseEnvironments) => {
const size = 100;
const { palette } = useTheme();
const version: string = process.env.REACT_APP_VERSION as string;
const environmentsState = useAppSelector(state => state.environments);
const namespacesState = useAppSelector(state => state.namespaces);
const dispatch = useAppDispatch();
const [triggerNamespacesQuery] = useLazyFetchNamespacesQuery();
const [triggerPrimaryNamespace] = useLazyFetchPrimaryNamespaceQuery();
const [triggerQuery] = useLazyFetchEnvironmentsQuery();
const getNamespaces = async () => {
const { data: namespacesData } = await triggerNamespacesQuery({
page: namespacesState.page,
size
});
if (namespacesData) {
const { primaryNamespace, namespaces } = await getNamespacesData(
namespacesData.data
);
dispatch(
dataFetched({
data: !isNamespaceListed(namespaces, primaryNamespace)
? [...namespaces, { ...primaryNamespace, isPrimary: true }]
: checkMyPrimaryNamespace(namespaces, primaryNamespace),
count: namespacesData.count
})
);
await getEnvironments();
}
};
const getNamespacesData = async (namespaces: Namespace[]) => {
try {
const { data: permissions } = await triggerPrimaryNamespace();
const namespacesWithPermissions = namespacesPermissionsMapper(
namespaces,
permissions
);
return {
namespaces: namespacesWithPermissions,
primaryNamespace: {
id: undefined, // API does not retrieve an ID if the namespace is empty
name: permissions.data.primary_namespace,
canCreate: true,
canUpdate: true
}
};
} catch (e) {
return {
namespaces: [],
primaryNamespace: {
id: undefined,
name: "default",
canCreate: true,
canUpdate: true
}
};
}
};
const getEnvironments = async () => {
const { data: environmentsData } = await triggerQuery({
page: environmentsState.page,
size,
search: environmentsState.search
});
if (environmentsData) {
dispatch(
environmentsSlice.actions.dataFetched({
data: environmentsData.data,
count: environmentsData.count
})
);
}
};
const handleChange = debounce(async (value: string) => {
const { data } = await triggerQuery({ page: 1, size, search: value });
if (data) {
dispatch(
environmentsSlice.actions.searched({
data: data.data,
count: data.count,
search: value
})
);
}
}, 500);
const next = async () => {
const { data } = await triggerQuery({
page: environmentsState.page + 1,
size,
search: environmentsState.search
});
if (data) {
dispatch(
environmentsSlice.actions.nextFetched({
data: data.data,
count: data.count
})
);
}
};
useEffect(() => {
(async () => {
getNamespaces();
})();
}, []);
useEffect(() => {
(async () => {
if (refreshEnvironments) {
getNamespaces();
onUpdateRefreshEnvironments(false);
}
})();
}, [refreshEnvironments]);
return (
<Box
sx={{
width: "100%",
borderRight: `1px solid ${palette.secondary.light}`,
display: "flex",
flexDirection: "column",
justifyContent: "space-between"
}}
>
<Box sx={{ borderBottom: `1px solid ${palette.secondary.light}` }}>
<EnvironmentsSearch onChange={e => handleChange(e.target.value)} />
</Box>
<Box
sx={{
zIndex: "1",
paddingTop: "15px",
flex: 4,
overflowY: "auto"
}}
>
{environmentsState.data && (
<EnvironmentsList
next={next}
hasMore={size * environmentsState.page < environmentsState.count}
environmentsList={environmentsState.data}
namespacesList={namespacesState.data}
search={environmentsState.search}
/>
)}
</Box>
<Box
bgcolor={palette.common.white}
sx={{
bottom: 5,
padding: "3px 12px",
display: "flex",
justifyContent: "space-between",
alignItems: "center",
backgroundColor: palette.common.white
}}
>
<Box
sx={{
display: "flex",
alignItems: "center",
"&:hover": {
borderBottom: "1px solid"
},
"&:focus": {
borderBottom: `1px solid ${palette.primary.main}`,
backgroundColor: palette.primary[50],
color: palette.primary[600]
}
}}
>
<BookIcon></BookIcon>
<Link
href={"https://conda.store"}
color="secondary"
underline="none"
sx={{
color: palette.secondary.main,
fontWeight: 400,
fontSize: "14px",
marginLeft: "5px"
}}
target="_blank"
>
<Typography
sx={{
color: palette.secondary.main,
fontWeight: 400,
fontSize: "14px"
}}
>
Read the docs!
</Typography>
</Link>
</Box>
<Typography
sx={{
color: palette.secondary.main,
fontWeight: 400,
fontSize: "14px",
marginRight: "5px"
}}
>
Version: {version}
</Typography>
</Box>
</Box>
);
};
export const Environments = memo(BaseEnvironments);