Skip to content

Commit 9e1bf19

Browse files
committed
Add gutenberg styles'
1 parent 4792f1a commit 9e1bf19

File tree

12 files changed

+10493
-4031
lines changed

12 files changed

+10493
-4031
lines changed

package-lock.json

+8,661-3,985
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"@stripe/stripe-js": "^1.36.0",
1313
"@svgr/cli": "^5.5.0",
1414
"@woocommerce/woocommerce-rest-api": "^1.0.1",
15+
"@wordpress/base-styles": "^4.21.0",
16+
"@wordpress/block-library": "^8.7.0",
1517
"axios": "^0.21.4",
1618
"classnames": "^2.3.1",
1719
"dompurify": "^2.4.0",

pages/[...slug].js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* External Dependencies.
3+
*/
4+
import { isArray, isEmpty } from 'lodash';
5+
import { useRouter } from 'next/router';
6+
7+
/**
8+
* Internal Dependencies.
9+
*/
10+
import Layout from '../src/components/layout';
11+
import { FALLBACK, handleRedirectsAndReturnData, isCustomPageUri } from '../src/utils/slug';
12+
import { getFormattedDate, getPathNameFromUrl, sanitize } from '../src/utils/miscellaneous';
13+
import { getPage, getPages, getPost, getPosts } from '../src/utils/blog';
14+
import axios from 'axios';
15+
import { HEADER_FOOTER_ENDPOINT } from '../src/utils/constants/endpoints';
16+
import Image from '../src/components/image';
17+
import PostMeta from '../src/components/post-meta';
18+
19+
const Page = ( { headerFooter, pageData } ) => {
20+
const router = useRouter();
21+
22+
// If the page is not yet generated, this will be displayed
23+
// initially until getStaticProps() finishes running
24+
if ( router.isFallback ) {
25+
return <div>Loading...</div>;
26+
}
27+
28+
return (
29+
<Layout headerFooter={ headerFooter || {} } seo={ pageData?.yoast_head_json ?? {} }>
30+
<div className="mb-8 w-4/5 m-auto">
31+
<figure className="overflow-hidden mb-4">
32+
<Image
33+
sourceUrl={ pageData?._embedded[ 'wp:featuredmedia' ]?.[ 0 ]?.source_url ?? '' }
34+
title={ pageData?.title?.rendered ?? '' }
35+
width="600"
36+
height="400"
37+
layout="fill"
38+
containerClassNames="w-full h-600px"
39+
/>
40+
</figure>
41+
<PostMeta date={ getFormattedDate( pageData?.date ?? '' ) } authorName={ pageData?._embedded?.author?.[0]?.name ?? '' }/>
42+
<h1 dangerouslySetInnerHTML={ { __html: sanitize( pageData?.title?.rendered ?? '' ) } }/>
43+
<div dangerouslySetInnerHTML={ { __html: sanitize( pageData?.content?.rendered ?? '' ) } }/>
44+
</div>
45+
</Layout>
46+
);
47+
};
48+
49+
export default Page;
50+
51+
export async function getStaticProps( { params } ) {
52+
53+
const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT );
54+
const pageData = await getPage( params?.slug.pop() ?? '' );
55+
56+
const defaultProps = {
57+
props: {
58+
headerFooter: headerFooterData?.data ?? {},
59+
pageData: pageData?.[0] ?? {}
60+
},
61+
/**
62+
* Revalidate means that if a new request comes to server, then every 1 sec it will check
63+
* if the data is changed, if it is changed then it will update the
64+
* static file inside .next folder with the new data, so that any 'SUBSEQUENT' requests should have updated data.
65+
*/
66+
revalidate: 1,
67+
};
68+
69+
return handleRedirectsAndReturnData( defaultProps, pageData );
70+
}
71+
72+
/**
73+
* Since the page name uses catch-all routes,
74+
* for example [...slug],
75+
* that's why params would contain slug which is an array.
76+
* For example, If we need to have dynamic route '/foo/bar'
77+
* Then we would add paths: [ params: { slug: ['foo', 'bar'] } } ]
78+
* Here slug will be an array is ['foo', 'bar'], then Next.js will statically generate the page at /foo/bar
79+
*
80+
* At build time next js will make an api call get the data and
81+
* generate a page bar.js inside .next/foo directory, so when the page is served on browser
82+
* data is already present, unlike getInitialProps which gets the page at build time but makes an api
83+
* call after page is served on the browser.
84+
*
85+
* @see https://nextjs.org/docs/basic-features/data-fetching#the-paths-key-required
86+
*
87+
* @returns {Promise<{paths: [], fallback: boolean}>}
88+
*/
89+
export async function getStaticPaths() {
90+
const pagesData = await getPages();
91+
92+
const pathsData = [];
93+
94+
isArray( pagesData ) && pagesData.map( page => {
95+
96+
/**
97+
* Extract pathname from url.
98+
* e.g.
99+
* getPathNameFromUrl( 'https://example.com/hello/hi/' ) will return '/hello/hi'
100+
* getPathNameFromUrl( 'https://example.com' ) will return '/'
101+
* @type {String}
102+
*/
103+
const pathName = getPathNameFromUrl( page?.link ?? '' );
104+
105+
// Build paths data.
106+
if ( ! isEmpty( pathName ) && ! isCustomPageUri( pathName ) ) {
107+
const slugs = pathName?.split( '/' ).filter( pageSlug => pageSlug );
108+
pathsData.push( { params: { slug: slugs } } );
109+
}
110+
} );
111+
112+
return {
113+
paths: pathsData,
114+
fallback: FALLBACK,
115+
};
116+
}

pages/blog/[slug].js

-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import PostMeta from '../../src/components/post-meta';
1818

1919
const Post = ( { headerFooter, postData } ) => {
2020
const router = useRouter();
21-
22-
console.log( 'postData', postData );
2321

2422
/**
2523
* If the page is not yet generated, this will be displayed
@@ -54,7 +52,6 @@ export default Post;
5452

5553
export async function getStaticProps( { params } ) {
5654
const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT );
57-
// const { data: postData } = await getPost( params?.slug ?? '' );
5855
const postData = await getPost( params?.slug ?? '' );
5956

6057
const defaultProps = {

src/components/layout/footer/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Internal Dependencies.
33
*/
4-
import {sanitize} from '../../../utils/miscellaneous';
4+
import { getPathNameFromUrl, sanitize } from '../../../utils/miscellaneous';
55
import { getIconComponentByName } from '../../../utils/icons-map';
66

77
/**
@@ -45,7 +45,7 @@ const Footer = ({footer}) => {
4545
<ul>
4646
{ footerMenuItems.map( menuItem => (
4747
<li key={menuItem?.ID}>
48-
<Link href={menuItem?.url ?? '/'}>
48+
<Link href={ getPathNameFromUrl( menuItem?.url ?? '' ) || '/' }>
4949
<a>{menuItem?.title}</a>
5050
</Link>
5151
</li>

src/components/layout/header/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isEmpty } from 'lodash';
44

55
import { BurgerIcon, TailwindIcon, Bag, User, Wishlist } from '../../icons';
66
import { AppContext } from '../../context';
7+
import { getPathNameFromUrl } from '../../../utils/miscellaneous';
78

89
const Header = ( { header } ) => {
910

@@ -47,7 +48,7 @@ const Header = ( { header } ) => {
4748
className={ `${ isMenuVisible ? 'max-h-full' : 'h-0' } overflow-hidden w-full lg:h-full block flex-grow lg:flex lg:items-center lg:w-auto` }>
4849
<div className="text-sm font-medium uppercase lg:flex-grow">
4950
{ ! isEmpty( headerMenuItems ) && headerMenuItems.length ? headerMenuItems.map( menuItem => (
50-
<Link key={ menuItem?.ID } href={ menuItem?.url ?? '/' }>
51+
<Link key={ menuItem?.ID } href={ getPathNameFromUrl( menuItem?.url ?? '' ) || '/' }>
5152
<a className="block mt-4 lg:inline-block lg:mt-0 hover:text-brand-royal-blue duration-500 mr-10"
5253
dangerouslySetInnerHTML={ { __html: menuItem.title } }/>
5354
</Link>

src/styles/index.scss

+13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
@use 'sass:math';
2+
// Gutenberg Styles.
3+
@import "~@wordpress/base-styles/colors";
4+
@import "~@wordpress/base-styles/variables";
5+
@import "~@wordpress/base-styles/mixins";
6+
@import "~@wordpress/base-styles/breakpoints";
7+
@import "~@wordpress/base-styles/animations";
8+
@import "~@wordpress/base-styles/z-index";
9+
@import '~@wordpress/block-library/src/style.scss';
10+
@import '~@wordpress/block-library/src/theme.scss';
11+
12+
// Tailwind styles.
113
@import "~tailwindcss/base";
214
@import "~tailwindcss/components";
315
@import "~tailwindcss/utilities";
416

17+
// Font Styles.
518
@import "../fonts/fonts.scss";
619

720
@import "01-settings/typography";

src/utils/blog.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import axios from 'axios';
66
/**
77
* Internal Dependencies.
88
*/
9-
import { GET_POST_ENDPOINT, GET_POSTS_ENDPOINT } from './constants/endpoints';
9+
import { GET_PAGES_ENDPOINT, GET_POST_ENDPOINT, GET_POSTS_ENDPOINT } from './constants/endpoints';
1010

1111
/**
1212
* Get Posts.
@@ -53,3 +53,43 @@ export const getPost = async ( postSlug = '' ) => {
5353
return [];
5454
} );
5555
};
56+
57+
/**
58+
* Get Pages.
59+
*
60+
* @return {Promise<void>}
61+
*/
62+
export const getPages = async () => {
63+
return await axios.get( `${ GET_PAGES_ENDPOINT }?_embed` )
64+
.then( res => {
65+
if ( 200 === res.status ) {
66+
return res.data;
67+
} else {
68+
return [];
69+
}
70+
} )
71+
.catch( err => {
72+
console.log( err.response.data.message )
73+
return [];
74+
} );
75+
};
76+
77+
/**
78+
* Get Page By Slug.
79+
*
80+
* @return {Promise<void>}
81+
*/
82+
export const getPage = async ( pageSlug = '' ) => {
83+
return await axios.get( `${ GET_PAGES_ENDPOINT }?slug=${ pageSlug }&_embed` )
84+
.then( res => {
85+
if ( 200 === res.status ) {
86+
return res.data;
87+
} else {
88+
return [];
89+
}
90+
} )
91+
.catch( err => {
92+
console.log( err.response.data.message )
93+
return [];
94+
} );
95+
};

src/utils/constants/endpoints.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const HEADER_FOOTER_ENDPOINT = `${ process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL }/wp-json/rae/v1/header-footer?header_location_id=hcms-menu-header&footer_location_id=hcms-menu-footer`;
22
export const GET_POSTS_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/posts`;
33
export const GET_POST_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/wp/v2/posts`;
4+
export const GET_PAGES_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/wp/v2/pages`;
45

56
/**
67
* Cart

src/utils/miscellaneous.js

+15
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,18 @@ export const getFormattedDate = ( theDate = '', locales = 'en-us' ) => {
4141
const options = { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' };
4242
return new Date( theDate ).toLocaleDateString( locales, options );
4343
};
44+
45+
/**
46+
* Get path name from url.
47+
*
48+
* @param {String} url URL.
49+
*
50+
* @return {String} URL pathname.
51+
*/
52+
export const getPathNameFromUrl = ( url = '' ) => {
53+
if ( ! url ) {
54+
return '';
55+
}
56+
const theURL = new URL( url );
57+
return theURL.pathname;
58+
}

src/utils/slug.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export const isCustomPageUri = ( uri ) => {
1313
const pagesToExclude = [
1414
'/',
1515
'/blog/',
16+
'/checkout/',
17+
'/cart/',
18+
'/thank-you/'
1619
];
1720

1821
return pagesToExclude.includes( uri );

0 commit comments

Comments
 (0)