Skip to content

Commit 262fb83

Browse files
committed
slug now identifier for individual post
1 parent b373488 commit 262fb83

5 files changed

Lines changed: 14 additions & 12 deletions

File tree

blog-frontend/src/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const App : React.FC = () => {
6565
title={post.title}
6666
username={post.username}
6767
content={post.content}
68+
slug={post.slug}
6869
excerpt={post.excerpt}
6970
status={post.status}
7071
published={post.published}
@@ -84,10 +85,10 @@ const App : React.FC = () => {
8485
<Route exact path="/create">
8586
<PostForm user={user} addPost={addPost} />
8687
</Route>
87-
<Route exact path="/posts/:title">
88+
<Route exact path="/posts/:slug">
8889
<PostDetails />
8990
</Route>
90-
<Route exact path="/posts/:title/edit">
91+
<Route exact path="/posts/:slug/edit">
9192
<EditPost />
9293
</Route>
9394
</Router>

blog-frontend/src/components/PostCard.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ interface PostCardProps extends Post {
88
user: string
99
}
1010

11-
const PostCard : React.FC <PostCardProps> = ({title, content,username,excerpt,status,published,category, deletePost, user}) => {
11+
const PostCard : React.FC <PostCardProps> = ({title, slug,content,username,excerpt,status,published,category, deletePost, user}) => {
1212

1313
return (
1414
<div
1515
className="post-card"
1616
>
1717
<Link
1818
to={{
19-
pathname: `/posts/${title}`
19+
pathname: `/posts/${slug}`
2020
}}
2121
>
2222
<h1>{title}</h1>
@@ -31,7 +31,7 @@ const PostCard : React.FC <PostCardProps> = ({title, content,username,excerpt,st
3131
<>
3232
<Link
3333
to={{
34-
pathname: `posts/${title}/edit`,
34+
pathname: `posts/${slug}/edit`,
3535
state: {title,status,published,content}
3636
}}
3737
>

blog-frontend/src/pages/PostDetails.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import { getPost, Post } from '../services/blogServices'
44
import moment from 'moment'
55

66
type params = {
7-
title: string
7+
slug: string
88
}
99

1010
const PostDetails = () => {
11-
const {title} = useParams<params>()
11+
const {slug} = useParams<params>()
1212
const [post, setPost] = useState<Post>()
1313

1414
useEffect(()=>{
1515
const fetchPost = async ()=>{
16-
const fetchedPost = await getPost(title)
16+
const fetchedPost = await getPost(slug)
1717
setPost(fetchedPost)
1818
}
1919
fetchPost()
20-
},[title])
20+
},[slug])
2121
return (
2222
<>
2323
<h1>{post?.title}</h1>

blog-frontend/src/services/blogServices.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Post {
99
excerpt: string;
1010
content: string;
1111
status:string;
12+
slug?: string
1213
published: string;
1314
category?: string[]
1415
}
@@ -22,8 +23,8 @@ export const createPost = async (newPost : Post) => {
2223
axiosInstance.post('/posts/', newPost)
2324
}
2425

25-
export const getPost = async (title : string) : Promise<Post> => {
26-
return axiosInstance.get(`/posts/${title}`).then(res=>res.data)
26+
export const getPost = async (slug : string) : Promise<Post> => {
27+
return axiosInstance.get(`/posts/${slug}`).then(res=>res.data)
2728
}
2829

2930
export const removePost = async (title: string) =>{

blog_api/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PostList(ModelViewSet):
2424

2525
def get_object(self, queyset=None, **kwargs):
2626
item = self.kwargs.get('pk')
27-
return get_object_or_404(Post,title=item)
27+
return get_object_or_404(Post,slug=item)
2828

2929
def create(self, request, *args, **kwargs):
3030
# author = User.objects.get(username=request.user)

0 commit comments

Comments
 (0)