Skip to content

Commit

Permalink
feat: news api
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Sep 23, 2023
1 parent 14521c5 commit 962cdd4
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 19 deletions.
10 changes: 5 additions & 5 deletions components/app/home/membership-card.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ const membershipGradient: Record<Exclude<User['memberType'], null>, string> = {
</div>

<f7Card v-else-if="user" class="m-0!">
<f7CardHeader class="h-45 rounded-[16px]!" valign="top" :class="membershipGradient[user.memberType!]">
<f7CardContent class="h-45 rounded-[16px]" valign="top" :class="membershipGradient[user.memberType!]">
<div class="flex flex-col w-full h-full">
<div class="flex flex-col flex-1">
<span class="font-bold text-3xl">
{{ user.name }}
</span>
<span class="font-mono text-base">
<span class="font-mono">
{{ user.memberId }}
</span>
</div>
<div class="flex flex-col text-base">
<div class="flex flex-col">
<span class="font-semibold">
Class of {{ user.graduationYear }}
</span>
<span class="text-sm">
<span>
{{ user.memberType?.[0].toLocaleUpperCase() }}{{ user.memberType?.slice(1, user.memberType?.length) }}
member
</span>
</div>
</div>
</f7CardHeader>
</f7CardContent>
<f7CardFooter>
<span>
<strong>Coming back?</strong>
Expand Down
37 changes: 37 additions & 0 deletions components/app/home/news.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script setup lang="ts">
import { f7BlockTitle, f7Card, f7CardContent, f7CardHeader, f7SkeletonBlock } from 'framework7-vue'
const { data: news, isLoading: newsIsLoading } = useNewsArticles()
</script>

<template>
<div>
<f7BlockTitle large>
What's Happening
</f7BlockTitle>

<div class="space-y-3">
<template v-if="newsIsLoading">
<f7SkeletonBlock v-for="n in 3" :key="n" class="rounded-md" effect="fade" height="10rem" />
</template>
<template v-else-if="news">
<span v-if="news.length === 0" class="mt-4 opacity-80">
Nothing exciting is happening right now.
<br>
Check back later!
</span>
<f7Card v-for="article in news" :key="article.id" class="m-0!">
<f7CardHeader class="h-45 rounded-[16px]! overflow-hidden bg-center bg-contain relative" valign="bottom" :style="`background-image: url(${article.heroImageUrl})`">
<div class="absolute inset-0 bg-gradient-to-b from-transparent to-black/70" />
<span class="z-10">
{{ article.title }}
</span>
</f7CardHeader>
<f7CardContent>
{{ article.description }}
</f7CardContent>
</f7Card>
</template>
</div>
</div>
</template>
10 changes: 2 additions & 8 deletions components/app/home/page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defineProps<{
const auth = useCurrentUser()
const authLoaded = useIsCurrentUserLoaded()
// TODO @qin-guan: Error state handling
const { data: user, isLoading: userIsLoading } = useUser()
const state = reactive({
showLoginScreen: false,
Expand Down Expand Up @@ -40,12 +38,8 @@ watch([authLoaded, auth], (values) => {
<AppHomeMembershipCard />
</div>

<f7BlockTitle>
What's Happening
</f7BlockTitle>

<div class="space-y-3">
<f7SkeletonBlock v-for="n in 3" :key="n" class="rounded-md" effect="fade" height="10rem" />
<div>
<AppHomeNews />
</div>
</f7List>
</f7Page>
Expand Down
15 changes: 15 additions & 0 deletions composables/news.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useQuery } from '@tanstack/vue-query'
import type { NewsArticle } from '~/shared/types'

const queryKeyFactory = {
newsArticles: ['newsArticles'],
}

export function useNewsArticles() {
const firebaseCurrentUser = useCurrentUser()
return useQuery({
queryKey: queryKeyFactory.newsArticles,
queryFn: () => $api<NewsArticle[]>('/api/news'),
enabled: computed(() => !!firebaseCurrentUser.value), // Only run when user exists
})
}
4 changes: 2 additions & 2 deletions plugins/vue-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export default defineNuxtPlugin((nuxtApp) => {
queryClientConfig: {
defaultOptions: {
queries: {
cacheTime: 1000 * 60 * 60 * 24,
staleTime: 1000 * 60 * 60 * 24,
cacheTime: 1000 * 60,
staleTime: 1000 * 60,
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions server/api/news/index.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default defineProtectedEventHandler((event) => {
return event.context.database.query.news.findMany()
}, {
cache: {
maxAge: 60,
},
})
10 changes: 10 additions & 0 deletions server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ export const bookings = sqliteTable('bookings', {
}),
})

export const news = sqliteTable('news', {
id: text('id').primaryKey().$defaultFn(() => createId()),
title: text('title').notNull(),
description: text('description').notNull(),
heroImageAlt: text('hero_image_alt').notNull(),
heroImageUrl: text('hero_image_url').notNull(),
ctaTitle: text('cta_title').notNull(),
ctaUrl: text('cta_url').notNull(),
})

// Relations

export const usersRelations = relations(users, ({ many }) => ({
Expand Down
9 changes: 9 additions & 0 deletions server/drizzle/0005_married_paladin.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE `news` (
`id` text PRIMARY KEY NOT NULL,
`title` text NOT NULL,
`description` text NOT NULL,
`hero_image_alt` text NOT NULL,
`hero_image_url` text NOT NULL,
`cta_title` text NOT NULL,
`cta_url` text NOT NULL
);
4 changes: 2 additions & 2 deletions server/drizzle/meta/0004_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
"name": "firebase_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"notNull": false,
"autoincrement": false
},
"name": {
Expand Down Expand Up @@ -402,4 +402,4 @@
"tables": {},
"columns": {}
}
}
}
Loading

0 comments on commit 962cdd4

Please # to comment.