From 6e1da8c5091d92b0aed90d2a36b059ad5ae207d2 Mon Sep 17 00:00:00 2001 From: Wojciech Zyla Date: Wed, 14 Sep 2022 13:54:09 +0200 Subject: [PATCH] fix: auto refresh state after deleting or adding profile --- frontend/packages/manager/package.json | 1 + ...{ProfilesModal.jsx => AddProfileModal.jsx} | 23 ++++++++++--------- .../profiles/DeleteProfileModal.jsx | 15 ++++++------ .../src/components/profiles/ProfilePanel.jsx | 10 ++++---- .../manager/src/pages/ProfilesPage.jsx | 21 ++++++----------- ...-profile-contxt.jsx => profile-contxt.jsx} | 23 +++++++++++++------ frontend/yarn.lock | 19 +++++++++++++++ 7 files changed, 68 insertions(+), 44 deletions(-) rename frontend/packages/manager/src/components/profiles/{ProfilesModal.jsx => AddProfileModal.jsx} (87%) rename frontend/packages/manager/src/store/{delete-profile-contxt.jsx => profile-contxt.jsx} (52%) diff --git a/frontend/packages/manager/package.json b/frontend/packages/manager/package.json index 808f2f2..e495ced 100644 --- a/frontend/packages/manager/package.json +++ b/frontend/packages/manager/package.json @@ -23,6 +23,7 @@ "@splunk/react-ui": "^4.9.0", "@splunk/themes": "^0.11.0", "axios": "^0.27.2", + "react-router-dom": "6", "scriptjs": "^2.5.9" }, "proxy": "http://localhost:5000", diff --git a/frontend/packages/manager/src/components/profiles/ProfilesModal.jsx b/frontend/packages/manager/src/components/profiles/AddProfileModal.jsx similarity index 87% rename from frontend/packages/manager/src/components/profiles/ProfilesModal.jsx rename to frontend/packages/manager/src/components/profiles/AddProfileModal.jsx index 64cfc5a..dca61c4 100644 --- a/frontend/packages/manager/src/components/profiles/ProfilesModal.jsx +++ b/frontend/packages/manager/src/components/profiles/AddProfileModal.jsx @@ -8,12 +8,11 @@ import VarbindsCreator from "../VarbindsCreator"; import Conditions from "../Conditions"; import PatternsCreator from "../PatternsCreator"; import axios from "axios"; -import AddProfileContext from "../../store/add-profile-contxt"; +import ProfileContext from "../../store/profile-contxt"; -function ProfilesModal(props) { - const AddProfCtx = useContext(AddProfileContext); - +function AddProfileModal(props) { + const ProfCtx = useContext(ProfileContext); const [profileName, setProfileName] = useState(''); const [frequency, setFrequency] = useState(1); const [varBinds, setVarBinds] = useState(null); @@ -42,21 +41,23 @@ function ProfilesModal(props) { const postProfile = (profileObj) => { axios.post('http://127.0.0.1:5000/profiles/add', profileObj) .then((response) => { - console.log(response) + console.log(response); }) } const handleApply = useCallback( (e) => { - var profileObj = { + let profileObj = { profileName: profileName, frequency: frequency, varBinds: varBinds, conditions: conditions - } - postProfile(profileObj) - props.handleRequestClose();}, - [frequency, profileName, varBinds, conditions, props.handleRequestClose] + }; + postProfile(profileObj); + props.handleRequestClose(); + ProfCtx.makeProfilesChange(); + }, + [frequency, profileName, varBinds, conditions, props.handleRequestClose, ProfCtx.makeProfilesChange] ); return ( @@ -89,4 +90,4 @@ function ProfilesModal(props) { ); } -export default ProfilesModal; +export default AddProfileModal; diff --git a/frontend/packages/manager/src/components/profiles/DeleteProfileModal.jsx b/frontend/packages/manager/src/components/profiles/DeleteProfileModal.jsx index 0301e0d..773bc2c 100644 --- a/frontend/packages/manager/src/components/profiles/DeleteProfileModal.jsx +++ b/frontend/packages/manager/src/components/profiles/DeleteProfileModal.jsx @@ -3,20 +3,20 @@ import Button from '@splunk/react-ui/Button'; import Modal from '@splunk/react-ui/Modal'; import axios from "axios"; import P from '@splunk/react-ui/Paragraph'; -import DeleteProfileContext from "../../store/delete-profile-contxt"; +import ProfileContext from "../../store/profile-contxt"; function DeleteProfileModal() { - const DelProfCtx = useContext(DeleteProfileContext); + const ProfCtx = useContext(ProfileContext); const [cancelButton, setCancelButon] = useState(); const cancelButtonRef = useCallback((el) => setCancelButon(el), []); const handleRequestClose = () => { - DelProfCtx.setDeleteOpen(false); + ProfCtx.setDeleteOpen(false); }; const handleDeleteProfile = () => { - const profileName = DelProfCtx.profileName; + const profileName = ProfCtx.profileName; console.log(profileName); axios.post(`http://127.0.0.1:5000/profiles/delete/${profileName}`) .then(function (response) { @@ -25,7 +25,8 @@ function DeleteProfileModal() { .catch(function (error) { console.log(error); }); - DelProfCtx.setDeleteOpen(false); + ProfCtx.setDeleteOpen(false); + ProfCtx.makeProfilesChange(); } return ( @@ -33,12 +34,12 @@ function DeleteProfileModal() { -

Are you sure you want to delete {DelProfCtx.profileName} profile?

+

Are you sure you want to delete {ProfCtx.profileName} profile?