Skip to content

Commit

Permalink
Merge pull request #835 from CodeForAfrica/bugfix/url-navigation
Browse files Browse the repository at this point in the history
@civicsignalblog /Fix navigation URL
  • Loading branch information
koechkevin authored Aug 12, 2024
2 parents ecaef69 + 5f3adbc commit e8c48e8
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/civicsignalblog/contrib/dokku/Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FROM codeforafrica/codeforafrica-ui:0.1.7
FROM codeforafrica/codeforafrica-ui:0.1.8
3 changes: 2 additions & 1 deletion apps/civicsignalblog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "civicsignalblog",
"version": "0.1.7",
"version": "0.1.8",
"private": true,
"author": "Code for Africa <tech@codeforafrica.org>",
"description": "This is the (temporary) CivicSignal blog",
Expand Down Expand Up @@ -47,6 +47,7 @@
"@next/env": "^14.2.5",
"@payloadcms/bundler-webpack": "^1.0.7",
"@payloadcms/db-mongodb": "^1.7.1",
"@payloadcms/live-preview": "^0.2.2",
"@payloadcms/live-preview-react": "^0.2.0",
"@payloadcms/plugin-cloud-storage": "^1.1.3",
"@payloadcms/plugin-nested-docs": "^1.0.12",
Expand Down
10 changes: 10 additions & 0 deletions apps/civicsignalblog/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ export default buildConfig({
admin: {
css: path.resolve(__dirname, "./src/payload/admin/scss/custom.scss"),
user: Users.slug,
livePreview: {
breakpoints: [
{
label: "Mobile",
name: "mobile",
width: 375,
height: 667,
},
],
},
webpack: (config) => ({
...config,
resolve: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function ArticlePage({
}}
sx={{
width: "100%",
height: { xs: "163px", md: "600px" },
height: "40vw",
}}
/>
<ArticleHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`<ArticlePage /> renders unchanged 1`] = `
class="MuiBox-root css-0"
>
<figure
class="MuiBox-root css-nxzwqu"
class="MuiBox-root css-1ne42gc"
>
<img
alt="Article"
Expand Down
3 changes: 2 additions & 1 deletion apps/civicsignalblog/src/pages/[...slugs].page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useLivePreview } from "@payloadcms/live-preview-react";
import React from "react";
import { SWRConfig } from "swr";

Expand All @@ -9,6 +8,7 @@ import LongForm from "@/civicsignalblog/components/LongForm/LongForm";
import PageHeader from "@/civicsignalblog/components/PageHeader";
import RelatedStories from "@/civicsignalblog/components/RelatedStories";
import { getPageServerSideProps } from "@/civicsignalblog/lib/data";
import { useLivePreview } from "@/civicsignalblog/utils/useLivePreview";

const componentsBySlugs = {
article: ArticlePage,
Expand All @@ -25,6 +25,7 @@ function Index(initialData) {
depth: 2,
initialData,
});

const { blocks, fallback } = props;
if (!blocks?.length) {
return null;
Expand Down
53 changes: 53 additions & 0 deletions apps/civicsignalblog/src/utils/useLivePreview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { subscribe, unsubscribe, ready } from "@payloadcms/live-preview";
import { useCallback, useEffect, useState, useRef } from "react";

export const useLivePreview = (props) => {
const { depth = 0, initialData, serverURL } = props;
const [data, setData] = useState(initialData);
const [isLoading, setIsLoading] = useState(true);
const hasSentReadyMessage = useRef(false);

const onChange = useCallback((mergedData) => {
// When a change is made, the `onChange` callback will be called with the merged data
// Set this merged data into state so that React will re-render the UI
setData(mergedData);
setIsLoading(false);
}, []);

useEffect(() => {
// Listen for `window.postMessage` events from the Admin panel
// When a change is made, the `onChange` callback will be called with the merged data
const subscription = subscribe({
callback: onChange,
depth,
initialData,
serverURL,
});

// Once subscribed, send a `ready` message back up to the Admin panel
// This will indicate that the front-end is ready to receive messages
if (!hasSentReadyMessage.current) {
hasSentReadyMessage.current = true;

ready({
serverURL,
});
}

// When the component unmounts, unsubscribe from the `window.postMessage` events
return () => {
unsubscribe(subscription);
};
}, [serverURL, onChange, depth, initialData, hasSentReadyMessage]);

useEffect(() => {
setData(initialData);
}, [initialData]);

return {
data,
isLoading,
};
};

export default useLivePreview;
102 changes: 86 additions & 16 deletions pnpm-lock.yaml

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

0 comments on commit e8c48e8

Please sign in to comment.