Skip to content

Commit

Permalink
Open 'create' page dialog on target pages using useLocation()
Browse files Browse the repository at this point in the history
  • Loading branch information
MandeepPaul committed Dec 25, 2024
1 parent 83b4ec5 commit 732b403
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
3 changes: 3 additions & 0 deletions frontend/src/scenes/banner/BannerDefaultPage.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
import {useLocation} from "react-router-dom"
import DefaultPageTemplate from '../../templates/DefaultPageTemplate/DefaultPageTemplate';
import { getBanners, deleteBanner } from '../../services/bannerServices';
import { ACTIVITY_TYPES_INFO } from '../../data/guideMainPageData';
Expand All @@ -8,6 +9,7 @@ const BannerDefaultPage = () => {
const [itemsUpdated, setItemsUpdated] = useState(false);
const [isEdit, setIsEdit] = useState(false);
const [itemId, setItemId] = useState(null);
const locationData = useLocation()

const getBannerDetails = (banner) => ({
title: `Banner ${banner.id}`,
Expand All @@ -27,6 +29,7 @@ const BannerDefaultPage = () => {
itemsUpdated={itemsUpdated}
/>
<BannerPage
autoOpen= {locationData.state?.autoOpen}
isEdit={isEdit}
itemId={itemId}
setItemsUpdated={setItemsUpdated}
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/scenes/banner/CreateBannerPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import BannerLeftContent from "./BannerPageComponents/BannerLeftContent/BannerLe
import BannerPreview from "./BannerPageComponents/BannerPreview/BannerPreview";
import { useDialog } from "../../templates/GuideTemplate/GuideTemplateContext";

const BannerPage = ({isEdit, itemId, setItemsUpdated}) => {
const BannerPage = ({ autoOpen = false, isEdit, itemId, setItemsUpdated }) => {
const [backgroundColor, setBackgroundColor] = useState("#F9F5FF");
const [fontColor, setFontColor] = useState("#344054");
const [activeButton, setActiveButton] = useState(0);
Expand All @@ -21,12 +21,17 @@ const BannerPage = ({isEdit, itemId, setItemsUpdated}) => {
const [url, setUrl] = useState("");
const [actionUrl, setActionUrl] = useState("");
const [buttonAction, setButtonAction] = useState("No action");
const { closeDialog } = useDialog();
const { openDialog, closeDialog } = useDialog();

const handleButtonClick = (index) => {
setActiveButton(index);
};


useEffect(() => {
if (autoOpen) openDialog();
}, [autoOpen, openDialog]);

useEffect(() => {
if (isEdit) {
const fetchBannerData = async () => {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/scenes/dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ const Dashboard = ({ name }) => {
{
skeletonType: <BaseSkeleton guideType="popup" />,
placeholder: "Create a popup",
onClick: () => navigate("/popup"),
onClick: () => navigate("/popup", { state: { autoOpen: true } }),
},
{
skeletonType: <BannerSkeleton />,
placeholder: "Create a new banner",
onClick: () => navigate("/banner"),
onClick: () => navigate("/banner", { state: { autoOpen: true } }),
},
{
skeletonType: <BaseSkeleton guideType="helperLink" />,
placeholder: "Create a new helper link",
onClick: () => navigate("/hint"),
onClick: () => navigate("/hint", { state: { autoOpen: true } }),
},
];

Expand Down
9 changes: 6 additions & 3 deletions frontend/src/scenes/hints/CreateHintPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import toastEmitter, { TOAST_EMITTER_KEY } from "../../utils/toastEmitter";
import { emitToastError } from "../../utils/guideHelper";
import { useDialog } from "../../templates/GuideTemplate/GuideTemplateContext";

const HintPage = ({ isEdit, itemId, setItemsUpdated }) => {
const { closeDialog } = useDialog();

const HintPage = ({ autoOpen = false, isEdit, itemId, setItemsUpdated }) => {
const { openDialog, closeDialog } = useDialog();

const [activeButton, setActiveButton] = useState(0);

Expand Down Expand Up @@ -60,6 +59,10 @@ const HintPage = ({ isEdit, itemId, setItemsUpdated }) => {
},
];

useEffect(() => {
if (autoOpen) openDialog();
}, [autoOpen, openDialog]);

useEffect(() => {
if (isEdit) {
const fetchHintData = async () => {
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/scenes/hints/HintDefaultPage.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from "react";
import {useLocation} from "react-router-dom"
import DefaultPageTemplate from "../../templates/DefaultPageTemplate/DefaultPageTemplate";
import CreateHintPage from "./CreateHintPage";
import { ACTIVITY_TYPES_INFO } from "../../data/guideMainPageData";
Expand All @@ -8,6 +9,7 @@ const HintDefaultPage = () => {
const [itemsUpdated, setItemsUpdated] = useState(false);
const [isEdit, setIsEdit] = useState(false);
const [itemId, setItemId] = useState(null);
const locationData = useLocation()

const getHintDetails = (hint) => ({
title: `Hint ${hint.id}`,
Expand All @@ -27,6 +29,7 @@ const HintDefaultPage = () => {
itemsUpdated={itemsUpdated}
/>
<CreateHintPage
autoOpen={locationData.state?.autoOpen}
isEdit={isEdit}
itemId={itemId}
setItemsUpdated={setItemsUpdated}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/scenes/popup/PopupDefaultPage.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
import {useLocation} from "react-router-dom"
import DefaultPageTemplate from '../../templates/DefaultPageTemplate/DefaultPageTemplate';
import CreatePopupPage from './CreatePopupPage';
import { getPopups, deletePopup } from '../../services/popupServices';
Expand All @@ -8,6 +9,7 @@ const PopupDefaultPage = () => {
const [itemsUpdated, setItemsUpdated] = useState(false);
const [isEdit, setIsEdit] = useState(false);
const [itemId, setItemId] = useState(null);
const locationData = useLocation()

const getPopupDetails = (popup) => ({
title: `Popup ${popup.id}`,
Expand All @@ -28,6 +30,7 @@ const PopupDefaultPage = () => {
itemsUpdated={itemsUpdated}
/>
<CreatePopupPage
autoOpen= {locationData.state?.autoOpen}
isEdit={isEdit}
itemId={itemId}
setItemsUpdated={setItemsUpdated}
Expand Down
15 changes: 11 additions & 4 deletions frontend/src/templates/GuideTemplate/GuideTemplate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { React } from "react";
import Button from "../../components/Button/Button";
import styles from "./GuideTemplate.module.scss";
import { useDialog } from "./GuideTemplateContext";
import { useLocation, useNavigate } from "react-router";

const GuideTemplate = ({
title = "",
Expand All @@ -17,8 +18,16 @@ const GuideTemplate = ({
onSave = () => null,
}) => {
const { isOpen, closeDialog } = useDialog();
const location = useLocation();
const navigate = useNavigate();
const buttons = ["Content", "Appearance"];

const onCloseHandler = () => {
if (location.state?.autoOpen) navigate("/", { state: {} });

closeDialog();
};

return (
<Dialog
closeAfterTransition={isOpen}
Expand All @@ -37,7 +46,7 @@ const GuideTemplate = ({
fontSize: "20px",
cursor: "pointer",
}}
onClick={closeDialog}
onClick={onCloseHandler}
/>
</div>
<div className={styles.content}>
Expand All @@ -63,9 +72,7 @@ const GuideTemplate = ({
<Button
text="Cancel"
buttonType="secondary-grey"
onClick={() => {
closeDialog();
}}
onClick={onCloseHandler}
/>
<Button text="Save" onClick={onSave} />
</div>
Expand Down

0 comments on commit 732b403

Please sign in to comment.