Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display most recent posts on landing page #42

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/mapbox__mapbox-gl-geocoder": "^4.7.2",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"dotenv": "^16.0.1",
"eslint": "^8.14.0",
"eslint-plugin-svelte3": "^3.4.0",
"js-yaml": "^4.1.0",
Expand Down
8 changes: 4 additions & 4 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ const postQuery = (slug: string) => `{
}
}`

const postsQuery = `{
posts: postCollection(order: date_DESC) {
const postsQuery = (query = ``) => `{
posts: postCollection(order: date_DESC, ${query}) {
${postFragment}
}
}`
Expand All @@ -225,8 +225,8 @@ export async function fetchPost(slug: string): Promise<Post> {
return post
}

export async function fetchPosts(): Promise<Post[]> {
const data = await contentfulFetch(postsQuery)
export async function fetchPosts(query = ``): Promise<Post[]> {
const data = await contentfulFetch(postsQuery(query))
const posts = data?.posts?.items
return await Promise.all(posts.map(processPost))
}
Expand Down
15 changes: 12 additions & 3 deletions src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import UserGraduate from '~icons/fa-solid/user-graduate'
import Place from '~icons/ic/place'
import ChapterMap from '../components/ChapterMap.svelte'
import { fetchChapters, fetchPage } from '../fetch'
import type { Chapter, Page } from '../types'
import PostPreview from '../components/PostPreview.svelte'
import { fetchChapters, fetchPage, fetchPosts } from '../fetch'
import type { Chapter, Page, Post } from '../types'

export const load: Load = async () => {
const page = await fetchPage(`/`)
const chapters = await fetchChapters()
const posts = await fetchPosts(`limit = 5`)

// const { students, pupils } = await airtableFetch(
// `{
Expand All @@ -22,13 +24,14 @@
// }`,
// { cache: `force-cache` }
// )
return { props: { page, chapters } }
return { props: { page, chapters, posts } }
}
</script>

<script lang="ts">
export let chapters: Chapter[]
export let page: Page
export let posts: Post[]

const style = `vertical-align: text-top; margin-right: 5pt;`
</script>
Expand Down Expand Up @@ -78,6 +81,12 @@
</a>
</h2>

<div style="display: flex;">
{#each posts as post}
<PostPreview {post} />
{/each}
</div>

<article>
{@html page.body}
</article>
Expand Down
12 changes: 12 additions & 0 deletions svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import rollupYaml from '@rollup/plugin-yaml'
import adapter from '@sveltejs/adapter-static'
import 'dotenv/config'
import path from 'path'
import preprocess from 'svelte-preprocess'
import Icons from 'unplugin-icons/vite'

const space_id = process.env.VITE_CONTENTFUL_SPACE_ID
const access_token = process.env.VITE_CONTENTFUL_ACCESS_TOKEN

const contentful_gql = `https://graphql.contentful.com/content/v1/spaces`

// eslint-disable-next-line no-console
console.log(
`GraphiQL explorer:`,
`${contentful_gql}${space_id}/explore?access_token=${access_token}`
)

/** @type {import('@sveltejs/kit').Config} */
export default {
preprocess: preprocess(),
Expand Down