Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce duplicate code for modals #1046

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router";
import i18n from "../i18n/i18n";
Expand All @@ -23,6 +23,7 @@ import { UserInfoState } from "../slices/userInfoSlice";
import { Tooltip } from "./shared/Tooltip";
import { HiTranslate } from "react-icons/hi";
import { IconContext } from "react-icons";
import { ModalHandle } from "./shared/modals/Modal";

// References for detecting a click outside of the container of the dropdown menus
const containerLang = React.createRef<HTMLDivElement>();
Expand Down Expand Up @@ -52,8 +53,8 @@ const Header = () => {
const [displayMenuUser, setMenuUser] = useState(false);
const [displayMenuNotify, setMenuNotify] = useState(false);
const [displayMenuHelp, setMenuHelp] = useState(false);
const [displayRegistrationModal, setRegistrationModal] = useState(false);
const [displayHotKeyCheatSheet, setHotKeyCheatSheet] = useState(false);
const registrationModalRef = useRef<ModalHandle>(null);
const hotKeyCheatSheetModalRef = useRef<ModalHandle>(null);

const healthStatus = useAppSelector(state => getHealthStatus(state));
const errorCounter = useAppSelector(state => getErrorCount(state));
Expand All @@ -69,11 +70,7 @@ const Header = () => {
};

const showRegistrationModal = () => {
setRegistrationModal(true);
};

const hideRegistrationModal = () => {
setRegistrationModal(false);
registrationModalRef.current?.open()
};

const redirectToServices = async () => {
Expand All @@ -85,15 +82,15 @@ const Header = () => {
};

const showHotKeyCheatSheet = () => {
setHotKeyCheatSheet(true);
};

const hideHotKeyCheatSheet = () => {
setHotKeyCheatSheet(false);
hotKeyCheatSheetModalRef.current?.open()
};

const toggleHotKeyCheatSheet = () => {
setHotKeyCheatSheet(!displayHotKeyCheatSheet);
if (hotKeyCheatSheetModalRef.current?.isOpen?.()) {
hotKeyCheatSheetModalRef.current?.close?.()
} else {
hotKeyCheatSheetModalRef.current?.open()
}
};

useHotkeys(
Expand Down Expand Up @@ -277,14 +274,10 @@ const Header = () => {
</header>

{/* Adopters Registration Modal */}
{displayRegistrationModal && (
<RegistrationModal close={hideRegistrationModal} />
)}
<RegistrationModal modalRef={registrationModalRef}/>

{/* Hotkey Cheat Sheet */}
{displayHotKeyCheatSheet && (
<HotKeyCheatSheet close={hideHotKeyCheatSheet} />
)}
<HotKeyCheatSheet modalRef={hotKeyCheatSheetModalRef}/>
</>
);
};
Expand Down
20 changes: 10 additions & 10 deletions src/components/configuration/Themes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import MainNav from "../shared/MainNav";
import { Link } from "react-router";
Expand All @@ -20,6 +20,7 @@ import { hasAccess } from "../../utils/utils";
import { getCurrentFilterResource } from "../../selectors/tableFilterSelectors";
import { useAppDispatch, useAppSelector } from "../../store";
import { fetchThemes } from "../../slices/themeSlice";
import { ModalHandle } from "../shared/modals/Modal";

/**
* This component renders the table view of events
Expand All @@ -31,7 +32,7 @@ const Themes = () => {
const currentFilterType = useAppSelector(state => getCurrentFilterResource(state));

const [displayNavigation, setNavigation] = useState(false);
const [displayNewThemesModal, setNewThemesModal] = useState(false);
const newThemesModalRef = useRef<ModalHandle>(null);

const user = useAppSelector(state => getUserInformation(state));
const themes = useAppSelector(state => getTotalThemes(state));
Expand Down Expand Up @@ -67,24 +68,23 @@ const Themes = () => {
};

const showNewThemesModal = () => {
setNewThemesModal(true);
newThemesModalRef.current?.open();
};

const hideNewThemesModal = () => {
setNewThemesModal(false);
newThemesModalRef.current?.close?.();
};

return (
<>
<Header />
<NavBar>
{/* Display modal for new series if add series button is clicked */}
{ displayNewThemesModal &&
<NewResourceModal
handleClose={hideNewThemesModal}
resource={"themes"}
/>
}
<NewResourceModal
handleClose={hideNewThemesModal}
resource={"themes"}
modalRef={newThemesModalRef}
/>

{/* Include Burger-button menu*/}
<MainNav isOpen={displayNavigation} toggleMenu={toggleNavigation} />
Expand Down
51 changes: 25 additions & 26 deletions src/components/configuration/partials/ThemesActionsCell.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useRef } from "react";
import { useTranslation } from "react-i18next";
import ConfirmModal from "../../shared/ConfirmModal";
import {
Expand All @@ -11,7 +11,7 @@ import { useAppDispatch, useAppSelector } from "../../../store";
import { deleteTheme, ThemeDetailsType } from "../../../slices/themeSlice";
import { Tooltip } from "../../shared/Tooltip";
import ThemeDetails from "./wizard/ThemeDetails";
import DetailsModal from "../../shared/modals/DetailsModal";
import { Modal, ModalHandle } from "../../shared/modals/Modal";

/**
* This component renders the action cells of themes in the table view
Expand All @@ -24,24 +24,24 @@ const ThemesActionsCell = ({
const { t } = useTranslation();
const dispatch = useAppDispatch();

const [displayDeleteConfirmation, setDeleteConfirmation] = useState(false);
const [displayThemeDetails, setThemeDetails] = useState(false);
const deleteConfirmationModalRef = useRef<ModalHandle>(null);
const detailsModalRef = useRef<ModalHandle>(null);

const user = useAppSelector(state => getUserInformation(state));

const hideDeleteConfirmation = () => {
setDeleteConfirmation(false);
deleteConfirmationModalRef.current?.close?.();
};

const hideThemeDetails = () => {
setThemeDetails(false);
detailsModalRef.current?.close?.();
};

const showThemeDetails = async () => {
await dispatch(fetchThemeDetails(row.id));
await dispatch(fetchUsage(row.id));

setThemeDetails(true);
detailsModalRef.current?.open();
};

const deletingTheme = (id: number) => {
Expand All @@ -60,35 +60,34 @@ const ThemesActionsCell = ({
</Tooltip>
)}

{displayThemeDetails && (
<DetailsModal
handleClose={hideThemeDetails}
title={row.name}
prefix={"CONFIGURATION.THEMES.DETAILS.EDITCAPTION"}
>
<ThemeDetails close={hideThemeDetails} />
</DetailsModal>
)}
{/* themes details modal */}
<Modal
header={t("CONFIGURATION.THEMES.DETAILS.EDITCAPTION", { name: row.name })}
classId="theme-details-modal"
ref={detailsModalRef}
>
{/* component that manages tabs of theme details modal*/}
<ThemeDetails close={hideThemeDetails} />
</Modal>

{/* delete themes */}
{hasAccess("ROLE_UI_THEMES_DELETE", user) && (
<Tooltip title={t("CONFIGURATION.THEMES.TABLE.TOOLTIP.DELETE")}>
<button
onClick={() => setDeleteConfirmation(true)}
onClick={() => deleteConfirmationModalRef.current?.open()}
className="button-like-anchor remove ng-scope ng-isolate-scope"
/>
</Tooltip>
)}

{displayDeleteConfirmation && (
<ConfirmModal
close={hideDeleteConfirmation}
resourceName={row.name}
resourceId={row.id}
deleteMethod={deletingTheme}
resourceType="THEME"
/>
)}
<ConfirmModal
close={hideDeleteConfirmation}
resourceName={row.name}
resourceId={row.id}
deleteMethod={deletingTheme}
resourceType="THEME"
modalRef={deleteConfirmationModalRef}
/>
</>
);
};
Expand Down
Loading
Loading