Skip to content

Commit

Permalink
class 08
Browse files Browse the repository at this point in the history
  • Loading branch information
polashmahmud committed Aug 31, 2023
1 parent 297ac7b commit 7e692f2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 5 deletions.
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"vue-router": "^4.2.4"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
"@vitejs/plugin-vue": "^4.2.3",
"autoprefixer": "^10.4.15",
"postcss": "^8.4.28",
Expand Down
13 changes: 11 additions & 2 deletions src/api/usePosts.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import {ref} from "vue";
import axios from "axios";

export default function usePosts() {
export default function usePosts(slug) {
const posts = ref([]);
const post = ref({});

const fetchPosts = async () => {
let response = await axios.get('/api/posts');

posts.value = response.data.data
}

const fetchPost = async () => {
let response = await axios.get(`/api/posts/${slug}`);

post.value = response.data.data
}

return {
posts,
fetchPosts
post,
fetchPosts,
fetchPost
}
}
4 changes: 2 additions & 2 deletions src/pages/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ onMounted(fetchPosts)
v-for="post in posts"
:key="post.uuid"
>
<a href="" class="block">
<router-link :to="{ name: 'post', params: {slug: post.slug}}" class="block">
<h1 class="text-3xl sm:text-4xl leading-10 font-extrabold tracking-tight text-gray-900">{{ post.title }}</h1>
<p class="mt-6 text-gray-500">
{{ post.teaser }}
</p>
</a>
</router-link>
</div>
</div>
</template>
Expand Down
27 changes: 27 additions & 0 deletions src/pages/Post.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script setup>
import usePosts from "../api/usePosts.js";
import { onMounted } from "vue";
const props = defineProps({
slug: {
type: String,
required: true
}
})
const {post, fetchPost} = usePosts(props.slug)
onMounted(fetchPost)
</script>

<template>
<h1 class="block text-center text-4xl sm:text-6xl leading-10 font-extrabold tracking-tight text-gray-900">
{{ post.title }}
</h1>
<div class="prose lg:prose-xl mt-16 text-gray-500" v-html="post.body"></div>
</template>

<style scoped>
</style>
7 changes: 7 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { createRouter, createWebHistory } from "vue-router";
import Home from "../pages/Home.vue";
import Post from "../pages/Post.vue";

const routes = [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/posts/:slug',
name: 'post',
component: Post,
props: true
}
]

Expand Down
4 changes: 3 additions & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default {
theme: {
extend: {},
},
plugins: [],
plugins: [
require('@tailwindcss/typography'),
],
}

0 comments on commit 7e692f2

Please sign in to comment.