Skip to content

Commit

Permalink
feat: connect page data
Browse files Browse the repository at this point in the history
  • Loading branch information
seaerchin committed Jun 25, 2024
1 parent 3985d60 commit d091893
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
74 changes: 71 additions & 3 deletions apps/studio/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,63 @@ import {
} from '~/server/modules/resource/resource.types'
import { db } from '../src/server/modules/database'

const PAGE_BLOB = {
version: '0.1.0',
layout: 'homepage',
page: {
title: 'Home',
},
content: [
{
type: 'hero',
variant: 'gradient',
alignment: 'left',
backgroundColor: 'black',
title: 'Ministry of Trade and Industry',
subtitle:
'A leading global city of enterprise and talent, a vibrant nation of innovation and opportunity',
buttonLabel: 'Main CTA',
buttonUrl: '/',
secondaryButtonLabel: 'Sub CTA',
secondaryButtonUrl: '/',
backgroundUrl: 'https://ohno.isomer.gov.sg/images/hero-banner.png',
},
{
type: 'infobar',
title: 'This is an infobar',
description: 'This is the description that goes into the Infobar section',
},
{
type: 'infopic',
title: 'This is an infopic',
description: 'This is the description for the infopic component',
imageSrc: 'https://placehold.co/600x400',
},
{
type: 'keystatistics',
statistics: [
{
label: 'Average all nighters pulled in a typical calendar month',
value: '3',
},
{
label: 'Growth in tasks assigned Q4 2024 (YoY)',
value: '+12.2%',
},
{
label: 'Creative blocks met per single evening',
value: '89',
},
{
value: '4.0',
label: 'Number of lies in this stat block',
},
],
variant: 'top',
title: 'Irrationality in numbers',
},
],
}
const NAV_BAR_ITEMS = [
{
name: 'Expandable nav item',
Expand Down Expand Up @@ -79,7 +136,7 @@ const FOOTER_ITEMS = [
]

async function main() {
const { id } = await db
const { id: siteId } = await db
.insertInto('Site')
.values({
name: 'Ministry of Trade and Industry',
Expand All @@ -102,7 +159,7 @@ async function main() {
await db
.insertInto('Footer')
.values({
siteId: id,
siteId,
content: {
contactUsLink: '/contact-us',
feedbackFormLink: 'https://www.form.gov.sg',
Expand All @@ -116,10 +173,21 @@ async function main() {
await db
.insertInto('Navbar')
.values({
siteId: id,
siteId,
content: { items: NAV_BAR_ITEMS } satisfies Navbar,
})
.execute()

const { id: blobId } = await db
.insertInto('Blob')
.values({ content: PAGE_BLOB })
.returning('id')
.executeTakeFirstOrThrow()

await db
.insertInto('Resource')
.values({ blobId, name: 'Home', siteId })
.executeTakeFirstOrThrow()
}

main()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default function Preview({ schema }: PreviewProps) {
const [{ content: navbar }] = trpc.site.getNavbar.useSuspenseQuery({
id: 1,
})
const [data] = trpc.page.readPageAndBlob.useSuspenseQuery({
pageId: 1,
})

return (
<RenderEngine
Expand Down
1 change: 0 additions & 1 deletion apps/studio/src/schemas/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const PAGE_LAYOUTS = ['content'] as const

export const getEditPageSchema = z.object({
pageId: z.number().min(1),
siteId: z.number().min(1),
})

export const updatePageSchema = getEditPageSchema.extend({
Expand Down
8 changes: 4 additions & 4 deletions apps/studio/src/server/modules/page/page.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export const pageRouter = router({
readPageAndBlob: protectedProcedure
.input(getEditPageSchema)
.query(async ({ input, ctx }) => {
const { pageId, siteId } = input
const { pageId } = input
const page = await getFullPageById(pageId)
// TODO: Fill these in later
const pageName: string = page.name
const siteMeta = getSiteConfig(siteId)
const navbar = getNavBar(siteId)
const footer = getFooter(siteId)
const siteMeta = getSiteConfig(page.siteId)
const navbar = getNavBar(page.siteId)
const footer = getFooter(page.siteId)
const { content } = page

return {
Expand Down
7 changes: 4 additions & 3 deletions apps/studio/src/server/modules/resource/resource.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ export const getFolders = () =>
.execute()

// NOTE: Base method for retrieving a resource - no distinction made on whether `blobId` exists
const getById = (id: number) => db.selectFrom('Resource').where('id', '=', id)
const getById = (id: number) =>
db.selectFrom('Resource').where('Resource.id', '=', id)

// NOTE: Throw here to fail early if our invariant that a page has a `blobId` is violated
export const getFullPageById = (id: number) => {
return getById(id)
.where('blobId', '!=', null)
.innerJoin('Blob', 'blobId', 'Blob.id')
.where('Resource.blobId', 'is not', null)
.innerJoin('Blob', 'Resource.blobId', 'Blob.id')
.selectAll()
.executeTakeFirstOrThrow()
}
Expand Down

0 comments on commit d091893

Please sign in to comment.