soames-gatsby-theme 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/pages/preview.js +12 -65
- package/package.json +1 -1
- package/src/pages/preview.tsx +15 -77
|
@@ -10,89 +10,36 @@ const Layout_1 = __importDefault(require("../components/Layout"));
|
|
|
10
10
|
const Seo_1 = __importDefault(require("../components/Seo"));
|
|
11
11
|
const HeroHeader_1 = __importDefault(require("../components/HeroHeader"));
|
|
12
12
|
const Shortcodes_1 = require("../utils/shortcodes/Shortcodes");
|
|
13
|
-
const POST_PREVIEW_QUERY = `
|
|
14
|
-
query PreviewPost($id: ID!) {
|
|
15
|
-
post(id: $id, idType: DATABASE_ID, asPreview: true) {
|
|
16
|
-
title
|
|
17
|
-
content
|
|
18
|
-
excerpt
|
|
19
|
-
date
|
|
20
|
-
featuredImage {
|
|
21
|
-
node {
|
|
22
|
-
sourceUrl
|
|
23
|
-
altText
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
`;
|
|
29
|
-
const PAGE_PREVIEW_QUERY = `
|
|
30
|
-
query PreviewPage($id: ID!) {
|
|
31
|
-
page(id: $id, idType: DATABASE_ID, asPreview: true) {
|
|
32
|
-
title
|
|
33
|
-
content
|
|
34
|
-
excerpt
|
|
35
|
-
featuredImage {
|
|
36
|
-
node {
|
|
37
|
-
sourceUrl
|
|
38
|
-
altText
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
`;
|
|
44
13
|
const PreviewPage = () => {
|
|
45
14
|
const [content, setContent] = (0, react_1.useState)(null);
|
|
46
|
-
const [contentType, setContentType] = (0, react_1.useState)("post");
|
|
47
15
|
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
48
16
|
const [error, setError] = (0, react_1.useState)(null);
|
|
49
17
|
(0, react_1.useEffect)(() => {
|
|
50
18
|
if (typeof window === "undefined")
|
|
51
19
|
return;
|
|
52
20
|
const params = new URLSearchParams(window.location.search);
|
|
53
|
-
const id = params.get("id");
|
|
54
|
-
const type = (params.get("type") || "post").toLowerCase();
|
|
55
21
|
const token = params.get("token");
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
setError("No preview ID provided. Please use the Preview button in WordPress admin.");
|
|
22
|
+
if (!token) {
|
|
23
|
+
setError("No preview token provided. Please use the Preview button in WordPress admin.");
|
|
59
24
|
setLoading(false);
|
|
60
25
|
return;
|
|
61
26
|
}
|
|
62
|
-
const
|
|
63
|
-
if (!
|
|
27
|
+
const wpUrl = process.env.GATSBY_WORDPRESS_URL;
|
|
28
|
+
if (!wpUrl) {
|
|
64
29
|
setError("WordPress URL is not configured.");
|
|
65
30
|
setLoading(false);
|
|
66
31
|
return;
|
|
67
32
|
}
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
credentials: token ? "omit" : "include",
|
|
72
|
-
headers: {
|
|
73
|
-
"Content-Type": "application/json",
|
|
74
|
-
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
75
|
-
},
|
|
76
|
-
body: JSON.stringify({ query, variables: { id } }),
|
|
77
|
-
})
|
|
33
|
+
const wpOrigin = new URL(wpUrl).origin;
|
|
34
|
+
const endpoint = `${wpOrigin}/wp-json/soames/v1/preview?token=${encodeURIComponent(token)}`;
|
|
35
|
+
fetch(endpoint)
|
|
78
36
|
.then((res) => res.json())
|
|
79
37
|
.then((data) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const firstError = data?.errors?.[0]?.message;
|
|
83
|
-
if (firstError &&
|
|
84
|
-
(firstError.toLowerCase().includes("forbidden") ||
|
|
85
|
-
firstError.toLowerCase().includes("authorization"))) {
|
|
86
|
-
setError("Preview token expired or invalid. Please click Preview again from WordPress admin.");
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
setError(firstError
|
|
90
|
-
? `Could not load preview: ${firstError}`
|
|
91
|
-
: "Could not load preview content. The post or page may not exist.");
|
|
92
|
-
}
|
|
38
|
+
if (data.code) {
|
|
39
|
+
setError(data.message ?? "Could not load preview content.");
|
|
93
40
|
}
|
|
94
41
|
else {
|
|
95
|
-
setContent(
|
|
42
|
+
setContent(data);
|
|
96
43
|
}
|
|
97
44
|
setLoading(false);
|
|
98
45
|
})
|
|
@@ -107,8 +54,8 @@ const PreviewPage = () => {
|
|
|
107
54
|
if (error || !content) {
|
|
108
55
|
return ((0, jsx_runtime_1.jsxs)(Layout_1.default, { children: [(0, jsx_runtime_1.jsx)(Seo_1.default, { title: "Preview Unavailable" }), (0, jsx_runtime_1.jsxs)("section", { style: { padding: "4rem 2rem", textAlign: "center" }, children: [(0, jsx_runtime_1.jsx)("h2", { children: "Preview Unavailable" }), (0, jsx_runtime_1.jsx)("p", { children: error ?? "No preview content found." })] })] }));
|
|
109
56
|
}
|
|
110
|
-
const backgroundImage = content.featuredImage?.
|
|
57
|
+
const backgroundImage = content.featuredImage?.sourceUrl ?? null;
|
|
111
58
|
const backgroundImageTitle = content.title ?? null;
|
|
112
|
-
return ((0, jsx_runtime_1.jsxs)(Layout_1.default, { children: [(0, jsx_runtime_1.jsx)(Seo_1.default, { title: `Preview: ${content.title ?? ""}` }), (0, jsx_runtime_1.jsx)(HeroHeader_1.default, { title: content.title ? (0, html_react_parser_1.default)(content.title) : "Preview", subhead: content.excerpt ? (0, html_react_parser_1.default)(content.excerpt) : "", backgroundImage: backgroundImage, backgroundImageTitle: backgroundImageTitle }),
|
|
59
|
+
return ((0, jsx_runtime_1.jsxs)(Layout_1.default, { children: [(0, jsx_runtime_1.jsx)(Seo_1.default, { title: `Preview: ${content.title ?? ""}` }), (0, jsx_runtime_1.jsx)(HeroHeader_1.default, { title: content.title ? (0, html_react_parser_1.default)(content.title) : "Preview", subhead: content.excerpt ? (0, html_react_parser_1.default)(content.excerpt) : "", backgroundImage: backgroundImage, backgroundImageTitle: backgroundImageTitle }), content.type === "post" ? ((0, jsx_runtime_1.jsx)("section", { children: (0, jsx_runtime_1.jsx)("div", { className: "media-container-row", children: (0, jsx_runtime_1.jsx)("div", { className: "col-12 col-lg-8", children: (0, jsx_runtime_1.jsx)("section", { id: "soames-gatsby-content-container", className: "soames-gatsby-blog-content", children: (0, jsx_runtime_1.jsxs)("article", { className: "blog-post", children: [(0, jsx_runtime_1.jsxs)("header", { children: [(0, jsx_runtime_1.jsx)("h1", { children: content.title ? (0, html_react_parser_1.default)(content.title) : "" }), content.date && (0, jsx_runtime_1.jsx)("p", { children: content.date })] }), content.content && ((0, jsx_runtime_1.jsx)("section", { className: "blog-post-content", children: (0, html_react_parser_1.default)(content.content) }))] }) }) }) }) })) : (content.content && ((0, jsx_runtime_1.jsx)("section", { id: "soames-gatsby-content-container", className: "soames-gatsby-content", children: (0, jsx_runtime_1.jsx)(Shortcodes_1.Shortcodes, { children: content.content }) })))] }));
|
|
113
60
|
};
|
|
114
61
|
exports.default = PreviewPage;
|
package/package.json
CHANGED
package/src/pages/preview.tsx
CHANGED
|
@@ -6,13 +6,12 @@ import HeroHeader from "../components/HeroHeader";
|
|
|
6
6
|
import { Shortcodes } from "../utils/shortcodes/Shortcodes";
|
|
7
7
|
|
|
8
8
|
interface FeaturedImage {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
altText?: string;
|
|
12
|
-
};
|
|
9
|
+
sourceUrl?: string;
|
|
10
|
+
altText?: string;
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
interface PreviewContent {
|
|
14
|
+
type: string;
|
|
16
15
|
title?: string;
|
|
17
16
|
content?: string;
|
|
18
17
|
excerpt?: string;
|
|
@@ -20,42 +19,8 @@ interface PreviewContent {
|
|
|
20
19
|
featuredImage?: FeaturedImage | null;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
const POST_PREVIEW_QUERY = `
|
|
24
|
-
query PreviewPost($id: ID!) {
|
|
25
|
-
post(id: $id, idType: DATABASE_ID, asPreview: true) {
|
|
26
|
-
title
|
|
27
|
-
content
|
|
28
|
-
excerpt
|
|
29
|
-
date
|
|
30
|
-
featuredImage {
|
|
31
|
-
node {
|
|
32
|
-
sourceUrl
|
|
33
|
-
altText
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
|
|
40
|
-
const PAGE_PREVIEW_QUERY = `
|
|
41
|
-
query PreviewPage($id: ID!) {
|
|
42
|
-
page(id: $id, idType: DATABASE_ID, asPreview: true) {
|
|
43
|
-
title
|
|
44
|
-
content
|
|
45
|
-
excerpt
|
|
46
|
-
featuredImage {
|
|
47
|
-
node {
|
|
48
|
-
sourceUrl
|
|
49
|
-
altText
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
`;
|
|
55
|
-
|
|
56
22
|
const PreviewPage: React.FC = () => {
|
|
57
23
|
const [content, setContent] = useState<PreviewContent | null>(null);
|
|
58
|
-
const [contentType, setContentType] = useState<string>("post");
|
|
59
24
|
const [loading, setLoading] = useState(true);
|
|
60
25
|
const [error, setError] = useState<string | null>(null);
|
|
61
26
|
|
|
@@ -63,60 +28,33 @@ const PreviewPage: React.FC = () => {
|
|
|
63
28
|
if (typeof window === "undefined") return;
|
|
64
29
|
|
|
65
30
|
const params = new URLSearchParams(window.location.search);
|
|
66
|
-
const id = params.get("id");
|
|
67
|
-
const type = (params.get("type") || "post").toLowerCase();
|
|
68
31
|
const token = params.get("token");
|
|
69
32
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (!id) {
|
|
33
|
+
if (!token) {
|
|
73
34
|
setError(
|
|
74
|
-
"No preview
|
|
35
|
+
"No preview token provided. Please use the Preview button in WordPress admin."
|
|
75
36
|
);
|
|
76
37
|
setLoading(false);
|
|
77
38
|
return;
|
|
78
39
|
}
|
|
79
40
|
|
|
80
|
-
const
|
|
81
|
-
if (!
|
|
41
|
+
const wpUrl = process.env.GATSBY_WORDPRESS_URL;
|
|
42
|
+
if (!wpUrl) {
|
|
82
43
|
setError("WordPress URL is not configured.");
|
|
83
44
|
setLoading(false);
|
|
84
45
|
return;
|
|
85
46
|
}
|
|
86
47
|
|
|
87
|
-
const
|
|
48
|
+
const wpOrigin = new URL(wpUrl).origin;
|
|
49
|
+
const endpoint = `${wpOrigin}/wp-json/soames/v1/preview?token=${encodeURIComponent(token)}`;
|
|
88
50
|
|
|
89
|
-
fetch(
|
|
90
|
-
method: "POST",
|
|
91
|
-
credentials: token ? "omit" : "include",
|
|
92
|
-
headers: {
|
|
93
|
-
"Content-Type": "application/json",
|
|
94
|
-
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
95
|
-
},
|
|
96
|
-
body: JSON.stringify({ query, variables: { id } }),
|
|
97
|
-
})
|
|
51
|
+
fetch(endpoint)
|
|
98
52
|
.then((res) => res.json())
|
|
99
53
|
.then((data) => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const firstError: string | undefined = data?.errors?.[0]?.message;
|
|
103
|
-
if (
|
|
104
|
-
firstError &&
|
|
105
|
-
(firstError.toLowerCase().includes("forbidden") ||
|
|
106
|
-
firstError.toLowerCase().includes("authorization"))
|
|
107
|
-
) {
|
|
108
|
-
setError(
|
|
109
|
-
"Preview token expired or invalid. Please click Preview again from WordPress admin."
|
|
110
|
-
);
|
|
111
|
-
} else {
|
|
112
|
-
setError(
|
|
113
|
-
firstError
|
|
114
|
-
? `Could not load preview: ${firstError}`
|
|
115
|
-
: "Could not load preview content. The post or page may not exist."
|
|
116
|
-
);
|
|
117
|
-
}
|
|
54
|
+
if (data.code) {
|
|
55
|
+
setError(data.message ?? "Could not load preview content.");
|
|
118
56
|
} else {
|
|
119
|
-
setContent(
|
|
57
|
+
setContent(data as PreviewContent);
|
|
120
58
|
}
|
|
121
59
|
setLoading(false);
|
|
122
60
|
})
|
|
@@ -148,7 +86,7 @@ const PreviewPage: React.FC = () => {
|
|
|
148
86
|
);
|
|
149
87
|
}
|
|
150
88
|
|
|
151
|
-
const backgroundImage = content.featuredImage?.
|
|
89
|
+
const backgroundImage = content.featuredImage?.sourceUrl ?? null;
|
|
152
90
|
const backgroundImageTitle = content.title ?? null;
|
|
153
91
|
|
|
154
92
|
return (
|
|
@@ -160,7 +98,7 @@ const PreviewPage: React.FC = () => {
|
|
|
160
98
|
backgroundImage={backgroundImage}
|
|
161
99
|
backgroundImageTitle={backgroundImageTitle}
|
|
162
100
|
/>
|
|
163
|
-
{
|
|
101
|
+
{content.type === "post" ? (
|
|
164
102
|
<section>
|
|
165
103
|
<div className="media-container-row">
|
|
166
104
|
<div className="col-12 col-lg-8">
|