-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
back button + popups #2707
back button + popups #2707
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,9 +57,16 @@ export const EmbeddingFormProvider: React.FC<{ | |
useEffect(() => { | ||
// Update URL when formStep changes | ||
const updatedSearchParams = new URLSearchParams(searchParams.toString()); | ||
const existingStep = updatedSearchParams.get("step"); | ||
updatedSearchParams.set("step", formStep.toString()); | ||
const newUrl = `${pathname}?${updatedSearchParams.toString()}`; | ||
router.push(newUrl); | ||
|
||
console.log("existingStep", existingStep); | ||
if (!existingStep) { | ||
router.replace(newUrl); | ||
} else if (newUrl !== pathname) { | ||
router.push(newUrl); | ||
} | ||
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: This logic may cause unexpected behavior if the URL contains other query parameters |
||
}, [formStep, router, pathname, searchParams]); | ||
|
||
// Update formStep when URL changes | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,12 +57,18 @@ export const FormProvider: React.FC<{ | |
useEffect(() => { | ||
// Update URL when formStep changes | ||
const updatedSearchParams = new URLSearchParams(searchParams.toString()); | ||
const existingStep = updatedSearchParams.get("step"); | ||
updatedSearchParams.set("step", formStep.toString()); | ||
const newUrl = `${pathname}?${updatedSearchParams.toString()}`; | ||
router.push(newUrl); | ||
|
||
if (!existingStep) { | ||
router.replace(newUrl); | ||
console.log("replacing", newUrl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Remove this console.log statement before merging to production |
||
} else if (newUrl !== pathname) { | ||
router.push(newUrl); | ||
} | ||
}, [formStep, router, pathname, searchParams]); | ||
|
||
// Update formStep when URL changes | ||
useEffect(() => { | ||
const stepFromUrl = parseInt(searchParams.get("step") || "0", 10); | ||
if (stepFromUrl !== formStep) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useEffect } from "react"; | ||
|
||
import { usePopup } from "../admin/connectors/Popup"; | ||
import { PopupSpec } from "../admin/connectors/Popup"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
interface PopupMessages { | ||
[key: string]: PopupSpec; | ||
} | ||
|
||
export const usePopupFromQuery = (messages: PopupMessages) => { | ||
const router = useRouter(); | ||
const { popup, setPopup } = usePopup(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider destructuring |
||
|
||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: The effect has missing dependencies. Add |
||
const searchParams = new URLSearchParams(window.location.search); | ||
// Get the value for search param with key "message" | ||
const messageValue = searchParams.get("message"); | ||
// Check if any key from messages object is present in search params | ||
for (const key in messages) { | ||
if (messageValue === key) { | ||
const popupMessage = messages[key]; | ||
console.log("popupMessage", popupMessage); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Remove console.log statement before merging to production |
||
const newUrl = `${window.location.pathname}`; | ||
router.replace(newUrl); | ||
setPopup(popupMessage); | ||
|
||
// Exit the loop after handling the first match | ||
break; | ||
} | ||
} | ||
}, []); | ||
|
||
return { popup }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: The |
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Remove this console.log before merging to production