Skip to content

Commit

Permalink
Add gutenberg styles'
Browse files Browse the repository at this point in the history
  • Loading branch information
imranhsayed committed Apr 2, 2023
1 parent 4792f1a commit 9e1bf19
Show file tree
Hide file tree
Showing 12 changed files with 10,493 additions and 4,031 deletions.
12,646 changes: 8,661 additions & 3,985 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@stripe/stripe-js": "^1.36.0",
"@svgr/cli": "^5.5.0",
"@woocommerce/woocommerce-rest-api": "^1.0.1",
"@wordpress/base-styles": "^4.21.0",
"@wordpress/block-library": "^8.7.0",
"axios": "^0.21.4",
"classnames": "^2.3.1",
"dompurify": "^2.4.0",
Expand Down
116 changes: 116 additions & 0 deletions pages/[...slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* External Dependencies.
*/
import { isArray, isEmpty } from 'lodash';
import { useRouter } from 'next/router';

/**
* Internal Dependencies.
*/
import Layout from '../src/components/layout';
import { FALLBACK, handleRedirectsAndReturnData, isCustomPageUri } from '../src/utils/slug';
import { getFormattedDate, getPathNameFromUrl, sanitize } from '../src/utils/miscellaneous';
import { getPage, getPages, getPost, getPosts } from '../src/utils/blog';
import axios from 'axios';
import { HEADER_FOOTER_ENDPOINT } from '../src/utils/constants/endpoints';
import Image from '../src/components/image';
import PostMeta from '../src/components/post-meta';

const Page = ( { headerFooter, pageData } ) => {
const router = useRouter();

// If the page is not yet generated, this will be displayed
// initially until getStaticProps() finishes running
if ( router.isFallback ) {
return <div>Loading...</div>;
}

return (
<Layout headerFooter={ headerFooter || {} } seo={ pageData?.yoast_head_json ?? {} }>
<div className="mb-8 w-4/5 m-auto">
<figure className="overflow-hidden mb-4">
<Image
sourceUrl={ pageData?._embedded[ 'wp:featuredmedia' ]?.[ 0 ]?.source_url ?? '' }
title={ pageData?.title?.rendered ?? '' }
width="600"
height="400"
layout="fill"
containerClassNames="w-full h-600px"
/>
</figure>
<PostMeta date={ getFormattedDate( pageData?.date ?? '' ) } authorName={ pageData?._embedded?.author?.[0]?.name ?? '' }/>
<h1 dangerouslySetInnerHTML={ { __html: sanitize( pageData?.title?.rendered ?? '' ) } }/>
<div dangerouslySetInnerHTML={ { __html: sanitize( pageData?.content?.rendered ?? '' ) } }/>
</div>
</Layout>
);
};

export default Page;

export async function getStaticProps( { params } ) {

const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT );
const pageData = await getPage( params?.slug.pop() ?? '' );

const defaultProps = {
props: {
headerFooter: headerFooterData?.data ?? {},
pageData: pageData?.[0] ?? {}
},
/**
* Revalidate means that if a new request comes to server, then every 1 sec it will check
* if the data is changed, if it is changed then it will update the
* static file inside .next folder with the new data, so that any 'SUBSEQUENT' requests should have updated data.
*/
revalidate: 1,
};

return handleRedirectsAndReturnData( defaultProps, pageData );
}

/**
* Since the page name uses catch-all routes,
* for example [...slug],
* that's why params would contain slug which is an array.
* For example, If we need to have dynamic route '/foo/bar'
* Then we would add paths: [ params: { slug: ['foo', 'bar'] } } ]
* Here slug will be an array is ['foo', 'bar'], then Next.js will statically generate the page at /foo/bar
*
* At build time next js will make an api call get the data and
* generate a page bar.js inside .next/foo directory, so when the page is served on browser
* data is already present, unlike getInitialProps which gets the page at build time but makes an api
* call after page is served on the browser.
*
* @see https://nextjs.org/docs/basic-features/data-fetching#the-paths-key-required
*
* @returns {Promise<{paths: [], fallback: boolean}>}
*/
export async function getStaticPaths() {
const pagesData = await getPages();

const pathsData = [];

isArray( pagesData ) && pagesData.map( page => {

/**
* Extract pathname from url.
* e.g.
* getPathNameFromUrl( 'https://example.com/hello/hi/' ) will return '/hello/hi'
* getPathNameFromUrl( 'https://example.com' ) will return '/'
* @type {String}
*/
const pathName = getPathNameFromUrl( page?.link ?? '' );

// Build paths data.
if ( ! isEmpty( pathName ) && ! isCustomPageUri( pathName ) ) {
const slugs = pathName?.split( '/' ).filter( pageSlug => pageSlug );
pathsData.push( { params: { slug: slugs } } );
}
} );

return {
paths: pathsData,
fallback: FALLBACK,
};
}
3 changes: 0 additions & 3 deletions pages/blog/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import PostMeta from '../../src/components/post-meta';

const Post = ( { headerFooter, postData } ) => {
const router = useRouter();

console.log( 'postData', postData );

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

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

const defaultProps = {
Expand Down
4 changes: 2 additions & 2 deletions src/components/layout/footer/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal Dependencies.
*/
import {sanitize} from '../../../utils/miscellaneous';
import { getPathNameFromUrl, sanitize } from '../../../utils/miscellaneous';
import { getIconComponentByName } from '../../../utils/icons-map';

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ const Footer = ({footer}) => {
<ul>
{ footerMenuItems.map( menuItem => (
<li key={menuItem?.ID}>
<Link href={menuItem?.url ?? '/'}>
<Link href={ getPathNameFromUrl( menuItem?.url ?? '' ) || '/' }>
<a>{menuItem?.title}</a>
</Link>
</li>
Expand Down
3 changes: 2 additions & 1 deletion src/components/layout/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isEmpty } from 'lodash';

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

const Header = ( { header } ) => {

Expand Down Expand Up @@ -47,7 +48,7 @@ const Header = ( { header } ) => {
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` }>
<div className="text-sm font-medium uppercase lg:flex-grow">
{ ! isEmpty( headerMenuItems ) && headerMenuItems.length ? headerMenuItems.map( menuItem => (
<Link key={ menuItem?.ID } href={ menuItem?.url ?? '/' }>
<Link key={ menuItem?.ID } href={ getPathNameFromUrl( menuItem?.url ?? '' ) || '/' }>
<a className="block mt-4 lg:inline-block lg:mt-0 hover:text-brand-royal-blue duration-500 mr-10"
dangerouslySetInnerHTML={ { __html: menuItem.title } }/>
</Link>
Expand Down
13 changes: 13 additions & 0 deletions src/styles/index.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
@use 'sass:math';
// Gutenberg Styles.
@import "~@wordpress/base-styles/colors";
@import "~@wordpress/base-styles/variables";
@import "~@wordpress/base-styles/mixins";
@import "~@wordpress/base-styles/breakpoints";
@import "~@wordpress/base-styles/animations";
@import "~@wordpress/base-styles/z-index";
@import '~@wordpress/block-library/src/style.scss';
@import '~@wordpress/block-library/src/theme.scss';

// Tailwind styles.
@import "~tailwindcss/base";
@import "~tailwindcss/components";
@import "~tailwindcss/utilities";

// Font Styles.
@import "../fonts/fonts.scss";

@import "01-settings/typography";
Expand Down
42 changes: 41 additions & 1 deletion src/utils/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import axios from 'axios';
/**
* Internal Dependencies.
*/
import { GET_POST_ENDPOINT, GET_POSTS_ENDPOINT } from './constants/endpoints';
import { GET_PAGES_ENDPOINT, GET_POST_ENDPOINT, GET_POSTS_ENDPOINT } from './constants/endpoints';

/**
* Get Posts.
Expand Down Expand Up @@ -53,3 +53,43 @@ export const getPost = async ( postSlug = '' ) => {
return [];
} );
};

/**
* Get Pages.
*
* @return {Promise<void>}
*/
export const getPages = async () => {
return await axios.get( `${ GET_PAGES_ENDPOINT }?_embed` )
.then( res => {
if ( 200 === res.status ) {
return res.data;
} else {
return [];
}
} )
.catch( err => {
console.log( err.response.data.message )
return [];
} );
};

/**
* Get Page By Slug.
*
* @return {Promise<void>}
*/
export const getPage = async ( pageSlug = '' ) => {
return await axios.get( `${ GET_PAGES_ENDPOINT }?slug=${ pageSlug }&_embed` )
.then( res => {
if ( 200 === res.status ) {
return res.data;
} else {
return [];
}
} )
.catch( err => {
console.log( err.response.data.message )
return [];
} );
};
1 change: 1 addition & 0 deletions src/utils/constants/endpoints.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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`;
export const GET_POSTS_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/posts`;
export const GET_POST_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/wp/v2/posts`;
export const GET_PAGES_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/wp/v2/pages`;

/**
* Cart
Expand Down
15 changes: 15 additions & 0 deletions src/utils/miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ export const getFormattedDate = ( theDate = '', locales = 'en-us' ) => {
const options = { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' };
return new Date( theDate ).toLocaleDateString( locales, options );
};

/**
* Get path name from url.
*
* @param {String} url URL.
*
* @return {String} URL pathname.
*/
export const getPathNameFromUrl = ( url = '' ) => {
if ( ! url ) {
return '';
}
const theURL = new URL( url );
return theURL.pathname;
}
3 changes: 3 additions & 0 deletions src/utils/slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const isCustomPageUri = ( uri ) => {
const pagesToExclude = [
'/',
'/blog/',
'/checkout/',
'/cart/',
'/thank-you/'
];

return pagesToExclude.includes( uri );
Expand Down
Loading

0 comments on commit 9e1bf19

Please sign in to comment.