Skip to content

Commit 4bb25b7

Browse files
committed
Make posts request
1 parent aaae98c commit 4bb25b7

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

pages/blog/index.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Layout from '../../src/components/layout';
2+
// import { PER_PAGE_FIRST, totalPagesCount } from '../../src/utils/pagination';
3+
// import Pagination from '../../src/components/blog/pagination';
4+
// import Posts from '../../src/components/blog/posts';
5+
import axios from 'axios';
6+
import { GET_POSTS_ENDPOINT, HEADER_FOOTER_ENDPOINT } from '../../src/utils/constants/endpoints';
7+
import { getPosts } from '../../src/utils/blog';
8+
9+
const Blog = ( { headerFooter } ) => {
10+
// const pagesCount = totalPagesCount( data?.posts?.pageInfo?.offsetPagination?.total ?? 0 );
11+
return (
12+
<Layout headerFooter={ headerFooter || {} } seo={null}>
13+
{/*<Posts posts={data?.posts?.edges ?? []}/>*/}
14+
{/*<Pagination pagesCount={pagesCount} postName="blog" />*/}
15+
</Layout>
16+
);
17+
};
18+
19+
export default Blog;
20+
21+
export async function getStaticProps() {
22+
const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT );
23+
const { data: postsData } = await getPosts();
24+
25+
return {
26+
props: {
27+
headerFooter: headerFooterData?.data ?? {},
28+
postsData: postsData || {}
29+
},
30+
31+
/**
32+
* Revalidate means that if a new request comes to server, then every 1 sec it will check
33+
* if the data is changed, if it is changed then it will update the
34+
* static file inside .next folder with the new data, so that any 'SUBSEQUENT' requests should have updated data.
35+
*/
36+
revalidate: 1,
37+
};
38+
}

src/utils/blog.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* External Dependencies.
3+
*/
4+
import axios from 'axios';
5+
6+
/**
7+
* Internal Dependencies.
8+
*/
9+
import { GET_POSTS_ENDPOINT } from './constants/endpoints';
10+
11+
/**
12+
* Get Posts.
13+
*
14+
* @return {Promise<void>}
15+
*/
16+
17+
export const getPosts = async ( pageNo = 1 ) => {
18+
return await axios.get( `${ GET_POSTS_ENDPOINT }?page_no=${ pageNo }` )
19+
.then( res => {
20+
if ( 200 === res.data.status ) {
21+
return res;
22+
} else {
23+
return {};
24+
}
25+
} )
26+
.catch( err => console.log( err.response.data.message ) );
27+
};

src/utils/constants/endpoints.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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`;
2+
export const GET_POSTS_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/posts`;
23
export const GET_PRODUCTS_ENDPOINT = `${process.env.NEXT_PUBLIC_SITE_URL}/api/get-products`;
34

45
/**

0 commit comments

Comments
 (0)