Skip to content

Commit

Permalink
refactor: moving types, schemas and fetch logic for features in own file
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxSchaefer committed Aug 15, 2023
1 parent a498466 commit 1650aa3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
29 changes: 1 addition & 28 deletions tasks/components/FeatureDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,7 @@ import { tw, tx } from '@helpwave/common/twind'
import { TimeDisplay } from '@helpwave/common/components/TimeDisplay'
import Link from 'next/link'
import { Span } from '@helpwave/common/components/Span'
import { z } from 'zod'

export const featureSchema = z.object({
title: z.string(),
description: z.string(),
date: z.string(),
image: z.string().url().optional(),
externalResource: z.string().url().optional(),
}).transform<Feature>((obj) => {
let description: (string | URL)[] = [obj.description]
if (obj.image) description = [new URL(obj.image), ...description]

return {
title: obj.title,
date: new Date(obj.date),
description,
externalResource: obj.externalResource ? new URL(obj.externalResource) : undefined
}
})

export const featuresSchema = z.array(featureSchema)

export type Feature = {
title: string,
date: Date,
description: (string | URL)[],
externalResource?: URL
}
import type { Feature } from '../utils/features'

export type FeatureDisplayProps = {
feature: Feature,
Expand Down
2 changes: 1 addition & 1 deletion tasks/components/layout/FeatureDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { Languages } from '@helpwave/common/hooks/useLanguage'
import type { PropsWithLanguage } from '@helpwave/common/hooks/useTranslation'
import { useTranslation } from '@helpwave/common/hooks/useTranslation'
import { ColumnTitle } from '../ColumnTitle'
import type { Feature } from '../FeatureDisplay'
import { FeatureDisplay } from '../FeatureDisplay'
import type { Feature } from '../../utils/features'

type FeatureDetailsTranslation = {
title: string
Expand Down
11 changes: 2 additions & 9 deletions tasks/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { useWardOverviewsQuery } from '../mutations/ward_mutations'
import { useOrganizationsByUserQuery } from '../mutations/organization_mutations'
import { LoadingAndErrorComponent } from '@helpwave/common/components/LoadingAndErrorComponent'
import { tw } from '@twind/core'
import type { Feature } from '../components/FeatureDisplay'
import { featuresSchema } from '../components/FeatureDisplay'
import { featuresSchema, fetchFeatures } from '../utils/features'

type DashboardTranslation = {
dashboard: string
Expand All @@ -34,13 +33,7 @@ type DashboardServerSideProps = {
}

export const getServerSideProps: GetServerSideProps<DashboardServerSideProps> = async () => {
const json = await fetch('https://cdn.helpwave.de/feed.json')
.then((res) => res.json())
.then(async (json) => {
await featuresSchema.parseAsync(json)
return json as Feature[]
})

const json = await fetchFeatures()
return { props: { jsonFeed: json } }
}

Expand Down
4 changes: 3 additions & 1 deletion tasks/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const configSchema = z.object({
NEXT_PUBLIC_OAUTH_CLIENT_ID: z.string().default('425f8b8d-c786-4ff7-b2bf-e52f505fb588'),
NEXT_PUBLIC_OAUTH_SCOPES: z.string().default('openid,offline_access,email,nickname,name,organizations'),
NEXT_PUBLIC_FAKE_TOKEN_ENABLE: z.literal('true').or(z.literal('false')).default('false'),
NEXT_PUBLIC_FAKE_TOKEN: z.object({ sub: z.string().uuid(), name: z.string(), nickname: z.string(), email: z.string().email(), organizations: z.string().array() }).default({ sub: '18159713-5d4e-4ad5-94ad-fbb6bb147984', name: 'Max Mustermann', nickname: 'max.mustermann', email: '[email protected]', organizations: ['3b25c6f5-4705-4074-9fc6-a50c28eba406'] })
NEXT_PUBLIC_FAKE_TOKEN: z.object({ sub: z.string().uuid(), name: z.string(), nickname: z.string(), email: z.string().email(), organizations: z.string().array() }).default({ sub: '18159713-5d4e-4ad5-94ad-fbb6bb147984', name: 'Max Mustermann', nickname: 'max.mustermann', email: '[email protected]', organizations: ['3b25c6f5-4705-4074-9fc6-a50c28eba406'] }),
NEXT_PUBLIC_FEATURES_FEED_URL: z.string().url().default('https://cdn.helpwave.de/feed.json'),
}).transform(obj => ({
env: obj.NODE_ENV,
apiUrl: obj.NEXT_PUBLIC_API_URL,
Expand All @@ -54,6 +55,7 @@ const configSchema = z.object({
},
fakeTokenEnable: obj.NEXT_PUBLIC_FAKE_TOKEN_ENABLE === 'true',
fakeToken: Buffer.from(JSON.stringify(obj.NEXT_PUBLIC_FAKE_TOKEN)).toString('base64'),
featuresFeedUrl: obj.NEXT_PUBLIC_FEATURES_FEED_URL,
}))

const getConfig = () => {
Expand Down
39 changes: 39 additions & 0 deletions tasks/utils/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { z } from 'zod'
import { getConfig } from './config';

export type Feature = {
title: string,
date: Date,
description: (string | URL)[],
externalResource?: URL
}

export const featureSchema = z.object({
title: z.string(),
description: z.string(),
date: z.string(),
image: z.string().url().optional(),
externalResource: z.string().url().optional(),
}).transform<Feature>((obj) => {
let description: (string | URL)[] = [obj.description]
if (obj.image) description = [new URL(obj.image), ...description]

return {
title: obj.title,
date: new Date(obj.date),
description,
externalResource: obj.externalResource ? new URL(obj.externalResource) : undefined
}
})

export const featuresSchema = z.array(featureSchema)

export const fetchFeatures = (): Promise<Feature[]> => {
const { featuresFeedUrl } = getConfig()
return fetch(featuresFeedUrl)
.then((res) => res.json())
.then(async (json) => {
await featuresSchema.parseAsync(json)
return json as Feature[]
})
}

0 comments on commit 1650aa3

Please sign in to comment.