soames-gatsby-theme 0.1.5 → 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.
@@ -10,88 +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
- setContentType(type);
57
- if (!id) {
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 wpGraphQLUrl = process.env.GATSBY_WORDPRESS_URL;
63
- if (!wpGraphQLUrl) {
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 query = type === "page" ? PAGE_PREVIEW_QUERY : POST_PREVIEW_QUERY;
69
- fetch(wpGraphQLUrl, {
70
- method: "POST",
71
- headers: {
72
- "Content-Type": "application/json",
73
- ...(token ? { Authorization: `Bearer ${token}` } : {}),
74
- },
75
- body: JSON.stringify({ query, variables: { id } }),
76
- })
33
+ const wpOrigin = new URL(wpUrl).origin;
34
+ const endpoint = `${wpOrigin}/wp-json/soames/v1/preview?token=${encodeURIComponent(token)}`;
35
+ fetch(endpoint)
77
36
  .then((res) => res.json())
78
37
  .then((data) => {
79
- const node = data?.data?.post ?? data?.data?.page ?? null;
80
- if (!node) {
81
- const firstError = data?.errors?.[0]?.message;
82
- if (firstError &&
83
- (firstError.toLowerCase().includes("forbidden") ||
84
- firstError.toLowerCase().includes("authorization"))) {
85
- setError("Preview token expired or invalid. Please click Preview again from WordPress admin.");
86
- }
87
- else {
88
- setError(firstError
89
- ? `Could not load preview: ${firstError}`
90
- : "Could not load preview content. The post or page may not exist.");
91
- }
38
+ if (data.code) {
39
+ setError(data.message ?? "Could not load preview content.");
92
40
  }
93
41
  else {
94
- setContent(node);
42
+ setContent(data);
95
43
  }
96
44
  setLoading(false);
97
45
  })
@@ -106,8 +54,8 @@ const PreviewPage = () => {
106
54
  if (error || !content) {
107
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." })] })] }));
108
56
  }
109
- const backgroundImage = content.featuredImage?.node?.sourceUrl ?? null;
57
+ const backgroundImage = content.featuredImage?.sourceUrl ?? null;
110
58
  const backgroundImageTitle = content.title ?? null;
111
- 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 }), contentType === "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 }) })))] }));
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 }) })))] }));
112
60
  };
113
61
  exports.default = PreviewPage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soames-gatsby-theme",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "A customizable Gatsby theme for personal websites using WordPress as a headless CMS.",
5
5
  "main": "dist/gatsby-config.js",
6
6
  "scripts": {
@@ -6,13 +6,12 @@ import HeroHeader from "../components/HeroHeader";
6
6
  import { Shortcodes } from "../utils/shortcodes/Shortcodes";
7
7
 
8
8
  interface FeaturedImage {
9
- node: {
10
- sourceUrl?: string;
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,59 +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
- setContentType(type);
71
-
72
- if (!id) {
33
+ if (!token) {
73
34
  setError(
74
- "No preview ID provided. Please use the Preview button in WordPress admin."
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 wpGraphQLUrl = process.env.GATSBY_WORDPRESS_URL;
81
- if (!wpGraphQLUrl) {
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 query = type === "page" ? PAGE_PREVIEW_QUERY : POST_PREVIEW_QUERY;
48
+ const wpOrigin = new URL(wpUrl).origin;
49
+ const endpoint = `${wpOrigin}/wp-json/soames/v1/preview?token=${encodeURIComponent(token)}`;
88
50
 
89
- fetch(wpGraphQLUrl, {
90
- method: "POST",
91
- headers: {
92
- "Content-Type": "application/json",
93
- ...(token ? { Authorization: `Bearer ${token}` } : {}),
94
- },
95
- body: JSON.stringify({ query, variables: { id } }),
96
- })
51
+ fetch(endpoint)
97
52
  .then((res) => res.json())
98
53
  .then((data) => {
99
- const node = data?.data?.post ?? data?.data?.page ?? null;
100
- if (!node) {
101
- const firstError: string | undefined = data?.errors?.[0]?.message;
102
- if (
103
- firstError &&
104
- (firstError.toLowerCase().includes("forbidden") ||
105
- firstError.toLowerCase().includes("authorization"))
106
- ) {
107
- setError(
108
- "Preview token expired or invalid. Please click Preview again from WordPress admin."
109
- );
110
- } else {
111
- setError(
112
- firstError
113
- ? `Could not load preview: ${firstError}`
114
- : "Could not load preview content. The post or page may not exist."
115
- );
116
- }
54
+ if (data.code) {
55
+ setError(data.message ?? "Could not load preview content.");
117
56
  } else {
118
- setContent(node as PreviewContent);
57
+ setContent(data as PreviewContent);
119
58
  }
120
59
  setLoading(false);
121
60
  })
@@ -147,7 +86,7 @@ const PreviewPage: React.FC = () => {
147
86
  );
148
87
  }
149
88
 
150
- const backgroundImage = content.featuredImage?.node?.sourceUrl ?? null;
89
+ const backgroundImage = content.featuredImage?.sourceUrl ?? null;
151
90
  const backgroundImageTitle = content.title ?? null;
152
91
 
153
92
  return (
@@ -159,7 +98,7 @@ const PreviewPage: React.FC = () => {
159
98
  backgroundImage={backgroundImage}
160
99
  backgroundImageTitle={backgroundImageTitle}
161
100
  />
162
- {contentType === "post" ? (
101
+ {content.type === "post" ? (
163
102
  <section>
164
103
  <div className="media-container-row">
165
104
  <div className="col-12 col-lg-8">