From 193d97e8598809cafbd83084290da9eb99040559 Mon Sep 17 00:00:00 2001 From: ATomkivEX <121034465+ATomkivEX@users.noreply.github.com> Date: Wed, 21 Dec 2022 13:23:09 +0200 Subject: [PATCH] made the default namespace as username (#223) * made the default namespace as username * replace all special characters to "-", @ to "--" --- frontend/src/app/hooks/useGetMe.js | 8 +++++++ .../app/main/applications/NamespacesSelect.js | 19 ++++++++++------- .../src/app/main/charts/NamespacesSelect.js | 21 ++++++++++++------- frontend/src/app/uitls/formattedNamespace.js | 3 +++ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 frontend/src/app/hooks/useGetMe.js create mode 100644 frontend/src/app/uitls/formattedNamespace.js diff --git a/frontend/src/app/hooks/useGetMe.js b/frontend/src/app/hooks/useGetMe.js new file mode 100644 index 00000000..cf890d62 --- /dev/null +++ b/frontend/src/app/hooks/useGetMe.js @@ -0,0 +1,8 @@ +import { useSelector } from 'react-redux'; + +import { selectUser } from 'app/store/userSlice'; + +export const useGetMe = () => { + const user = useSelector(selectUser); + return user; +}; diff --git a/frontend/src/app/main/applications/NamespacesSelect.js b/frontend/src/app/main/applications/NamespacesSelect.js index 3ee1e761..c37f521e 100644 --- a/frontend/src/app/main/applications/NamespacesSelect.js +++ b/frontend/src/app/main/applications/NamespacesSelect.js @@ -3,18 +3,28 @@ import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete'; import { useState, useEffect } from 'react'; import { getNamespacesList as getNamespacesListAPI } from '../../api'; +import { useGetMe } from '../../hooks/useGetMe'; +import { formattedNamespace } from '../../uitls/formattedNamespace'; const filter = createFilterOptions(); const NamespacesSelect = ({ clusterContextName, handleGetNamespace }) => { const [namespace, setNamespace] = useState(null); - const [open, toggleOpen] = useState(false); const [list, setList] = useState([]); + const user = useGetMe(); useEffect(() => { handleGetNamespace(namespace?.name); }, [namespace]); + useEffect(() => { + const defaultNamespace = { + name: formattedNamespace(user.email), + states: 'active', + }; + setNamespace(defaultNamespace); + }, [user]); + useEffect(() => { let canceled = false; if (clusterContextName) { @@ -38,13 +48,6 @@ const NamespacesSelect = ({ clusterContextName, handleGetNamespace }) => { }; }, [clusterContextName]); - useEffect(() => { - if (list.length) { - const namespaceItem = list.find((item) => item.name === 'default'); - setNamespace(namespaceItem); - } - }, [list]); - return ( { const [namespace, setNamespace] = useState(null); const [open, toggleOpen] = useState(false); const [list, setList] = useState([]); + const user = useGetMe(); useEffect(() => { handleGetNamespace(namespace?.name); }, [namespace]); + useEffect(() => { + const defaultNamespace = { + name: formattedNamespace(user.email), + states: 'active', + }; + + setNamespace(defaultNamespace); + }, [user]); + useEffect(() => { let canceled = false; if (clusterContextName) { @@ -38,13 +50,6 @@ const NamespacesSelect = ({ clusterContextName, handleGetNamespace }) => { }; }, [clusterContextName]); - useEffect(() => { - if (list.length) { - const namespaceItem = list.find((item) => item.name === 'default'); - setNamespace(namespaceItem); - } - }, [list]); - const handleClose = () => { setDialogValue({ name: '', diff --git a/frontend/src/app/uitls/formattedNamespace.js b/frontend/src/app/uitls/formattedNamespace.js new file mode 100644 index 00000000..931554e5 --- /dev/null +++ b/frontend/src/app/uitls/formattedNamespace.js @@ -0,0 +1,3 @@ +export const formattedNamespace = (namespace) => { + return namespace.replaceAll(/[^@a-z0-9]/gi, '-').replaceAll(/[@]/g, '--'); +};