Skip to content

Commit

Permalink
Tweaked alerts so pages is optional
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-oconnell committed Jul 11, 2023
1 parent 56bab1e commit 85dee9a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/components/AlertBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import AlertSection from './AlertSection'
import MarkdownContent from './MarkdownContent'

export interface AlertBlockProps {
page: AlertPage
page?: AlertPage
className?: string
}

Expand All @@ -23,7 +23,7 @@ const AlertBlock: FC<AlertBlockProps> = ({ page, className }) => {
<AlertSection key={alert.uid} type={alert.type} className="mt-4 mb-4">
<MarkdownContent
markdown={i18n.language === 'fr' ? alert.textFr : alert.textEn}
header={page === 'all'}
header={page === undefined}
/>
</AlertSection>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Layout: FC<LayoutProps> = ({ children }) => {
id="mainContent"
className="container mx-auto mt-5 flex-1 px-4 pb-8"
>
<AlertBlock page="all" />
<AlertBlock />
{children}
</main>
<Footer
Expand Down
5 changes: 2 additions & 3 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export type AppWindow = Window &
typeof globalThis & { adobeDataLayer?: AdobeDataLayer }

export type AlertPage =
| 'all'
| 'email'
| 'expectations'
| 'landing'
Expand All @@ -18,7 +17,7 @@ export type AlertPage =
export type AlertType = 'danger' | 'info' | 'warning' | 'success'

export interface AlertApiRequestQuery {
page: AlertPage
page?: AlertPage
}

export interface AlertJsonResponse {
Expand All @@ -33,7 +32,7 @@ export interface Alert {
}

export interface AlertMeta extends Alert {
pages: AlertPage[]
pages?: AlertPage[]
validFrom: Date
validTo: Date
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/useAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export const fetchAlerts = async (
const query = new URLSearchParams({
...alertQuery,
}).toString()
const response = await fetch('/api/alerts?' + query, { cache: 'no-store' })
let uri = alertQuery.page ? `/api/alerts?${query}` : `/api/alerts`
const response = await fetch(uri, {
headers: { 'Cache-Control': 'max-age=600' },
})
if (response.ok) {
return response.json()
}
Expand Down
41 changes: 24 additions & 17 deletions src/pages/api/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,35 @@ export default async function handler(
let now = new Date()

try {
const alertJson = await fetch(process.env.ALERT_JSON_URI)
const alertJson = await fetch(process.env.ALERT_JSON_URI, {
headers: { 'Cache-Control': 'max-age=600' },
})
const alertData: AlertJsonResponse = await alertJson.json()

let alerts: Alert[] = alertData?.jsonAlerts
.filter(
(alert) =>
alert.pages.find((alertPage) => alertPage === page) &&
new Date(alert.validFrom) <= now &&
new Date(alert.validTo) >= now
)
.map((alert) => ({
uid: alert.uid,
textEn: alert.textEn,
textFr: alert.textFr,
type: alert.type,
}))
let validAlerts = alertData?.jsonAlerts.filter(
(alert) =>
new Date(alert.validFrom) <= now && new Date(alert.validTo) >= now
)

let pageAlerts = page
? validAlerts.filter((alert) =>
alert.pages?.find((alertPage) => alertPage === page)
)
: validAlerts.filter(
(alert) => alert.pages === undefined || alert.pages?.length === 0
)

let alerts: Alert[] = pageAlerts.map((alert) => ({
uid: alert.uid,
textEn: alert.textEn,
textFr: alert.textFr,
type: alert.type,
}))

return res.status(200).json(alerts)
} catch (error) {
// For alerts we want to silently fail, shouldn't show as a failure to the user.
// We'll catch it in the logs, but to them it should just look like there are no alerts.
// If there's a problem with the alerts, we return 500 but with an empty list
logger.error(error, 'Failed to fetch alerts')
res.status(200).json([])
res.status(500).json([])
}
}

0 comments on commit 85dee9a

Please sign in to comment.