Skip to content

Commit

Permalink
Merge pull request #92 from GoogleChromeLabs/news-nuxt-routes
Browse files Browse the repository at this point in the history
News Sites - dispatch a custom event on route change
  • Loading branch information
flashdesignory authored Nov 6, 2024
2 parents 149bff9 + a5f6ff9 commit 9ab0f2a
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 32 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export default function Layout({ children, id }) {
const { alerts, links, config } = useDataContext();
const hero = config?.ads?.[id].hero;

useEffect(() => {
const url = location.pathname;
requestIdleCallback(() => {
window.dispatchEvent(new CustomEvent("route-change-complete", { detail: { url } }));
});
}, [location.pathname]);

useEffect(() => {
setShowMessage(alerts[id].message);
}, [alerts, id]);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@ import { useDataContext } from "@/context/data-context";
import { Message } from "@/components/molecules/message/message";

import styles from "news-site-css/dist/layout.module.css";
import { useRouter } from "next/router";

export default function Layout({ children, id }) {
const [showMessage, setShowMessage] = useState(false);
const { alerts, links, config } = useDataContext();
const router = useRouter();
const hero = config?.ads?.[id].hero;

function handleRouteChangeComplete(url) {
window.dispatchEvent(new CustomEvent("route-change-complete", { detail: { url } }));
}

useEffect(() => {
router.events.on("routeChangeComplete", handleRouteChangeComplete);

return () => router.events.off("routeChangeComplete", handleRouteChangeComplete);
}, []);
const url = location.pathname;
requestIdleCallback(() => {
window.dispatchEvent(new CustomEvent("route-change-complete", { detail: { url } }));
});
}, [location.pathname]);

useEffect(() => {
setShowMessage(alerts[id].message);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions apps/news-site/news-site-next/src/partials/layout/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@ import { useDataContext } from "@/context/data-context";
import { Message } from "@/components/molecules/message/message";

import styles from "news-site-css/dist/layout.module.css";
import { useRouter } from "next/router";

export default function Layout({ children, id }) {
const [showMessage, setShowMessage] = useState(false);
const { alerts, links, config } = useDataContext();
const router = useRouter();
const hero = config?.ads?.[id].hero;

function handleRouteChangeComplete(url) {
window.dispatchEvent(new CustomEvent("route-change-complete", { detail: { url } }));
}

useEffect(() => {
router.events.on("routeChangeComplete", handleRouteChangeComplete);

return () => router.events.off("routeChangeComplete", handleRouteChangeComplete);
}, []);
const url = location.hash;
requestIdleCallback(() => {
window.dispatchEvent(new CustomEvent("route-change-complete", { detail: { url } }));
});
}, [location.hash]);

useEffect(() => {
setShowMessage(alerts[id].message);
Expand Down
11 changes: 8 additions & 3 deletions apps/news-site/news-site-nuxt-pages-router/partials/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ const id = ref(route.name === "index" ? "home" : route.name);
const data = inject("data");
const { alerts, links } = data.value;
function updateShowMessage() {
function handleOnChange() {
const url = route.path;
requestIdleCallback(() => {
window.dispatchEvent(new CustomEvent("route-change-complete", { detail: { url } }));
});
showMessage.value = alerts[id]?.message ? true : false;
id.value = route.name === "index" ? "home" : route.name;
}
onMounted(updateShowMessage);
watch(() => route.path, updateShowMessage);
onMounted(handleOnChange);
watch(() => route.path, handleOnChange);
const closeMessage = () => {
showMessage.value = false;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions apps/news-site/news-site-nuxt/partials/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ const route = useRoute();
const data = inject("data");
const { alerts, links } = data.value;
function updateShowMessage() {
function handleOnChange() {
const url = route.path;
requestIdleCallback(() => {
window.dispatchEvent(new CustomEvent("route-change-complete", { detail: { url } }));
});
showMessage.value = alerts[route.name]?.message ? true : false;
}
onMounted(updateShowMessage);
watch(() => route.path, updateShowMessage);
onMounted(handleOnChange);
watch(() => route.path, handleOnChange);
const closeMessage = () => {
showMessage.value = false;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9ab0f2a

Please sign in to comment.