Skip to content

Commit

Permalink
new usePrevNextPost composable (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
vernaillen authored Jul 9, 2024
1 parent 3a5429d commit 7696060
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/3.other-composables/1.user.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#### User

getters:
```ts
```ts twoslash
const userId = getCurrentUserId()
const userName = getCurrentUserName()
```
Authentication is required to fetch the user information.

methods:
```ts
```ts twoslash
loginUser()
logoutUser()
```
Expand Down
2 changes: 1 addition & 1 deletion docs/3.other-composables/2.staging.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#### Staging

```ts
```ts twoslash
const staging = await isStaging()
```
More about this on the [staging](../3.advanced/4.staging.md) page.
38 changes: 38 additions & 0 deletions docs/3.other-composables/3.previous-next-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#### get Previous/Next posts

```ts twoslash
const { prev: prev, next: next } = await usePrevNextPost('currentPostSlug')
```


```vue twoslash [..uri.vue]
<script setup lang="ts">
const route = useRoute()
const { prev: prev, next: next } = await usePrevNextPost(route.params.slug[0])
</script>
<template>
<div class="flex justify-start">
<UButton
v-if="prev"
:alt="prevButton"
:to="'/' + prev"
icon="i-heroicons-arrow-left-16-solid"
variant="soft"
size="sm"
/>
</div>
<div class="flex justify-end">
<UButton
v-if="next"
:alt="nextButton"
:to="'/' + next"
trailing-icon="i-heroicons-arrow-right-16-solid"
variant="soft"
size="sm"
/>
</div>
</template>
```

41 changes: 41 additions & 0 deletions playground/components/PrevNext.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script setup lang="ts">
defineProps<{
prev?: string
next?: string
prevButton?: string
nextButton?: string
}>()
</script>

<template>
<div class="grid grid-cols-3 gap-6 max-w-28">
<div class="flex justify-start">
<UButton
v-if="prev"
:alt="prevButton"
:to="'/' + prev"
icon="i-heroicons-arrow-left-16-solid"
variant="soft"
size="sm"
/>
</div>
<div class="flex justify-center">
<UButton
to="/"
icon="i-heroicons-home"
variant="soft"
size="sm"
/>
</div>
<div class="flex justify-end">
<UButton
v-if="next"
:alt="nextButton"
:to="'/' + next"
trailing-icon="i-heroicons-arrow-right-16-solid"
variant="soft"
size="sm"
/>
</div>
</div>
</template>
11 changes: 6 additions & 5 deletions playground/pages/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (post.value?.title) {
title: post.value.title
})
}
const { prev: prev, next: next } = await usePrevNextPost(route.params.slug[0])
const staging = await isStaging()
</script>

Expand All @@ -38,11 +39,11 @@ const staging = await isStaging()
</UPageBody>
<template #left>
<UAside>
<UButton
icon="i-heroicons-arrow-left"
variant="soft"
size="sm"
to="/"
<PrevNext
:prev="post.contentTypeName === 'post' ? prev : undefined"
:next="post.contentTypeName === 'post' ? next : undefined"
prev-button="Vorige"
next-button="Volgende"
/>
<div
v-if="featuredImage"
Expand Down
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default defineNuxtModule<WPNuxtConfig>({
{ name: 'isStaging', as: 'isStaging', from: resolveRuntimeModule('./composables/isStaging') },
{ name: 'useWPContent', as: 'useWPContent', from: resolveRuntimeModule('./composables/useWPContent') },
{ name: 'parseDoc', as: 'parseDoc', from: resolveRuntimeModule('./composables/useParser') },
{ name: 'usePrevNextPost', as: 'usePrevNextPost', from: resolveRuntimeModule('./composables/usePrevNextPost') },

{ name: 'loginUser', as: 'loginUser', from: resolveRuntimeModule('./composables/user') },
{ name: 'logoutUser', as: 'logoutUser', from: resolveRuntimeModule('./composables/user') },
Expand Down
29 changes: 29 additions & 0 deletions src/runtime/composables/usePrevNextPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useWPContent } from './useWPContent'

const _usePrevNextPost = async (currentPostSlug: string) => {
const allPosts = await getAllPosts()
if (!allPosts) return { prev: null, next: null }
const currentIndex: number = allPosts.slugs.findIndex((slug: string) => slug === currentPostSlug)
const nextPost = currentIndex > 0 ? allPosts.slugs[currentIndex - 1] : null
const prevPost = allPosts.slugs.length > (currentIndex + 1) ? allPosts.slugs[currentIndex + 1] : null

return {
prev: prevPost ? prevPost : null,
next: nextPost ? nextPost : null
}
}

const getAllPosts = async () => {
const { data: allPosts } = await useWPContent('Posts', ['posts', 'nodes'], false)
if (allPosts.value) {
return {
slugs: allPosts.value?.map((post) => {
if (post) return post.slug
else return null
})
}
}
return
}

export const usePrevNextPost = _usePrevNextPost

0 comments on commit 7696060

Please sign in to comment.