Skip to content

Commit fe137d5

Browse files
displaying single specific blogs
1 parent f1f41cd commit fe137d5

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

client/actions/blog.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,13 @@ export const listBlogsWithCategoriesAndTaglists = (skip,limit) => {
3131
}).then(response => {
3232
return response.json();
3333
}).catch(error => console.log(error));
34-
};
34+
};
35+
36+
37+
export const singleBlog = slug =>{
38+
return fetch(`${API}/api/blog/${slug}`,{
39+
method: 'GET'
40+
}).then(response=>{
41+
return response.json()
42+
}).catch(err => console.log(err))
43+
}

client/components/blog/Card/Card.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const Card =({blog})=>{
3939
<div className="lead pb-4" style={{boxShadow:"inset 0 0 2000px rgba(255, 255, 255, .5)",filter:"blur(0.1px)",borderRadius:"17px"}}>
4040
<header>
4141
<Link href={`/blogs/${blog.slug}`}>
42-
<a><h3 className="display-5 pt-4 pb-2 font-weight-bold pl-5">{blog.title}</h3></a>
42+
<a target="_blank"><h3 className="display-5 pt-4 pb-2 font-weight-bold pl-5">{blog.title}</h3></a>
4343
</Link>
4444
</header>
4545
<section>
@@ -54,7 +54,7 @@ const Card =({blog})=>{
5454
<div className="col-md-8">
5555
<section>
5656
<div className="pb-3">{renderHTML(blog.excerpt)}</div>
57-
<Link href={`/blogs/${blog.slug}`}><a className="btn btn-info pt-2">Read more<MoreIcon/></a></Link>
57+
<Link href={`/blogs/${blog.slug}`}><a className="btn btn-info pt-2" target="_blank">Read more<MoreIcon/></a></Link>
5858
</section>
5959
</div>
6060
</div>

client/pages/blogs/[slug].js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Head from 'next/head';
2+
import Link from 'next/link';
3+
import {withRouter} from "next/router";
4+
import Layout from '../../components/Layout';
5+
import React,{ useState,useEffect } from 'react';
6+
import { singleBlog } from '../../actions/blog';
7+
import LabelIcon from '@material-ui/icons/Label';
8+
import CategoryIcon from '@material-ui/icons/Category';
9+
import {API,DOMAIN,APP_NAME} from '../../config';
10+
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
11+
12+
13+
const SingleBlog = ({blog,router})=>{
14+
return (
15+
<>
16+
<Layout>
17+
<main>
18+
<article>
19+
<div className="container-fluid">
20+
<section>
21+
{JSON.stringify(blog)}
22+
</section>
23+
</div>
24+
</article>
25+
</main>
26+
</Layout>
27+
</>
28+
)
29+
}
30+
31+
SingleBlog.getInitialProps=({query})=>{
32+
return singleBlog(query.slug).then(data=>{
33+
if (data.error){
34+
console.log(data.error)
35+
} else {
36+
return {blog:data}
37+
}
38+
})
39+
}
40+
41+
export default withRouter(SingleBlog);

server/controllers/blog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ exports.bloglistsallCategoriesTags =(req,res)=>{
171171

172172
exports.read =(req,res)=>{
173173
const slug = req.params.slug.toLowerCase();
174-
Blogs.findOne({slug}).populate("categories","_id name slug").populate("taglists","_id name slug").populate("postedBy","_id name username").select("_id title body slug mtitle mdesc categories taglists postedBy createdAt updatedAt").exec((err,data)=>{
174+
Blog.findOne({slug}).populate("categories","_id name slug").populate("taglists","_id name slug").populate("postedBy","_id name username").select("_id title body slug mtitle mdesc categories taglists postedBy createdAt updatedAt").exec((err,data)=>{
175175
if (err){
176176
return res.json({
177177
error: errorHandler(err)

0 commit comments

Comments
 (0)