diff --git a/frontend/src/components/Links/Popup/Popup.jsx b/frontend/src/components/Links/Popup/Popup.jsx index 6951b762..a774a681 100644 --- a/frontend/src/components/Links/Popup/Popup.jsx +++ b/frontend/src/components/Links/Popup/Popup.jsx @@ -20,12 +20,9 @@ const Popup = () => { const handleClosePopup = async () => { setIsPopupOpen(false); - setLinks( - links.filter( - (it) => it.title !== itemToDelete.title && it.id !== itemToDelete.id - ) - ); - setDeletedLinks((prev) => [...prev, itemToDelete]); + setLinks(links.filter((it) => it.id !== itemToDelete.id)); + typeof itemToDelete.id === "number" && + setDeletedLinks((prev) => [...prev, itemToDelete]); setItemToDelete(null); }; diff --git a/frontend/src/components/Links/Settings/Settings.jsx b/frontend/src/components/Links/Settings/Settings.jsx index fa9babcf..c9177f73 100644 --- a/frontend/src/components/Links/Settings/Settings.jsx +++ b/frontend/src/components/Links/Settings/Settings.jsx @@ -2,7 +2,7 @@ import CloseOutlinedIcon from "@mui/icons-material/CloseOutlined"; import { useContext, useEffect, useRef, useState } from "react"; import { HelperLinkContext } from "../../../services/linksProvider"; import Switch from "../../Switch/Switch"; -import s from "./Settings.module.scss"; +import style from "./Settings.module.scss"; const defaultState = { title: "", @@ -38,7 +38,10 @@ const Settings = () => { }; setState(newState); } else { - setState({ ...defaultState, id: Math.floor(Date.now() * Math.random()) }); + setState({ + ...defaultState, + id: `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`, + }); } window.addEventListener("mousedown", handleMouseDown); @@ -78,84 +81,84 @@ const Settings = () => { setLinkToEdit(null); toggleSettings(e); } else { - setLinks((prev) => [...prev, { ...info, id: +info.id }]); + setLinks((prev) => [...prev, { ...info, id: info.id }]); toggleSettings(e); } }; return (
-
- Add new link -
- Auto-saved +
+ Add new link +
+ Auto-saved
-
+
-
- ); }; -export default Settings; \ No newline at end of file +export default Settings; diff --git a/frontend/src/scenes/links/LinksDefaultPage.jsx b/frontend/src/scenes/links/LinksDefaultPage.jsx index 1c1d7276..f5d35603 100644 --- a/frontend/src/scenes/links/LinksDefaultPage.jsx +++ b/frontend/src/scenes/links/LinksDefaultPage.jsx @@ -1,10 +1,10 @@ import React, { useState } from "react"; import { ACTIVITY_TYPES_INFO } from "../../data/guideMainPageData"; -import { deleteHelper, getHelpers} from "../../services/helperLinkService"; +import { deleteHelper, getHelpers } from "../../services/helperLinkService"; import HelperLinkProvider from "../../services/linksProvider"; import DefaultPageTemplate from "../../templates/DefaultPageTemplate/DefaultPageTemplate"; -import NewLinksPopup from "./NewLinksPopup"; import styles from "./LinkPage.module.scss"; +import NewLinksPopup from "./NewLinksPopup"; const LinksDefaultPage = () => { const [itemsUpdated, setItemsUpdated] = useState(false); @@ -16,31 +16,29 @@ const LinksDefaultPage = () => { headerBackgroundColor: helper.headerBackgroundColor, linkFontColor: helper.linkFontColor, iconColor: helper.iconColor, - }) + }); return ( - <> - -
- - -
-
- + +
+ + +
+
); }; -export default LinksDefaultPage; \ No newline at end of file +export default LinksDefaultPage; diff --git a/frontend/src/scenes/links/NewLinksPopup.jsx b/frontend/src/scenes/links/NewLinksPopup.jsx index ff784434..bdb0d973 100644 --- a/frontend/src/scenes/links/NewLinksPopup.jsx +++ b/frontend/src/scenes/links/NewLinksPopup.jsx @@ -7,7 +7,7 @@ import { getHelperById, updateHelper, } from "../../services/helperLinkService"; -import { deleteLink, getLinkById } from "../../services/linkService"; +import { deleteLink } from "../../services/linkService"; import { HelperLinkContext } from "../../services/linksProvider"; import GuideTemplate from "../../templates/GuideTemplate/GuideTemplate"; import { useDialog } from "../../templates/GuideTemplate/GuideTemplateContext"; @@ -17,6 +17,13 @@ import styles from "./LinkPage.module.scss"; import LinkAppearance from "./LinkPageComponents/LinkAppearance"; import LinkContent from "./LinkPageComponents/LinkContent"; +const DEFAULT_VALUES = { + title: "", + headerBackgroundColor: "#F8F9F8", + linkFontColor: "#344054", + iconColor: "#7F56D9", +}; + const NewLinksPopup = ({ autoOpen = false, isEdit, @@ -31,35 +38,37 @@ const NewLinksPopup = ({ links, deletedLinks, setLinks, - helperToEdit, setHelperToEdit, + setDeletedLinks, } = useContext(HelperLinkContext); - const DEFAULT_VALUES = { - title: "", - headerBackgroundColor: "#F8F9F8", - linkFontColor: "#344054", - iconColor: "#7F56D9", - } const { openDialog, closeDialog, isOpen } = useDialog(); + + const resetHelper = (close = true) => { + close && closeDialog(); + setHelper({}); + setLinks([]); + setHelperToEdit(null); + setDeletedLinks([]); + }; + const fetchHelperData = async () => { try { const { links, ...data } = await getHelperById(itemId); setHelper(data); setLinks(links.sort((a, b) => a.order - b.order)); setHelperToEdit(itemId); - } - catch (error) { + } catch (error) { emitToastError(buildToastError(error)); - closeDialog(); + resetHelper(); } - } + }; useEffect(() => { if (autoOpen) { openDialog(); } - }, [autoOpen, openDialog]); + }, [autoOpen]); useEffect(() => { if (isEdit) { @@ -69,18 +78,16 @@ const NewLinksPopup = ({ setLinks([]); } if (!isOpen) { - setLinks([]); - setHelper({}); - setHelperToEdit(null); + resetHelper(false); } - }, [openDialog, isOpen]); + }, [isOpen]); const buildToastError = (msg) => msg.response ? msg : { - response: { data: { errors: [{ msg }] } }, - }; + response: { data: { errors: [{ msg }] } }, + }; const handleLinks = async (item) => { const { id, ...link } = item; @@ -89,8 +96,7 @@ const NewLinksPopup = ({ return null; } try { - const exists = await getLinkById(id); - if (exists?.id) return { ...link, id }; + if (typeof id === "number") return item; return { ...link }; } catch (err) { emitToastError(err); @@ -105,21 +111,20 @@ const NewLinksPopup = ({ if (formattedLinks.some((it) => !it)) { return null; } - newHelper = await (helperToEdit + newHelper = await (isEdit ? updateHelper(helper, formattedLinks) : createHelper(helper, formattedLinks)); setHelper(newHelper); setItemsUpdated((prevState) => !prevState); - closeDialog(); } catch (err) { emitToastError(buildToastError(err)); return null; } - if (helperToEdit && deletedLinks.length) { + if (isEdit && deletedLinks.length) { await Promise.all( deletedLinks.map(async (it) => { try { - return await deleteLink({ ...it, helperId: helperToEdit }); + return await deleteLink(it.id); } catch (err) { emitToastError(err); return null; @@ -128,13 +133,11 @@ const NewLinksPopup = ({ ); } if (newHelper) { - const toastMessage = helper.id + const toastMessage = isEdit ? "You edited this Helper Link" : "New Helper Link saved"; toastEmitter.emit(TOAST_EMITTER_KEY, toastMessage); - setHelper({}); - setLinks([]); - setHelperToEdit(null); + resetHelper(); } }; diff --git a/frontend/src/utils/constants.js b/frontend/src/utils/constants.js index d3fafe5d..2189a1ee 100644 --- a/frontend/src/utils/constants.js +++ b/frontend/src/utils/constants.js @@ -1,9 +1,9 @@ // API constants //local environment -export const API_BASE_URL = 'http://localhost:3000/api/'; +// export const API_BASE_URL = 'http://localhost:3000/api/'; //staging environment -// export const API_BASE_URL = 'https://onboarding-demo.bluewavelabs.ca/api/'; +export const API_BASE_URL = 'https://onboarding-demo.bluewavelabs.ca/api/'; // Other constants export const APP_TITLE = 'Bluewave Onboarding'; export const SUPPORT_EMAIL = 'support@bluewave.com';