Skip to content

Commit

Permalink
Add cookie utils to set and get the favoriteTeam - closes #103 (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelaugl authored Mar 17, 2022
1 parent d078815 commit 36cc68b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const REGULAR_PERIOD_COUNT = 4
export const DATE_DISPLAY_FORMAT = 'dd MMMM yyyy'
export const DATE_LINK_FORMAT = 'yyyyMMdd'
export const EST_IANA_ZONE_ID = 'America/New_York'
export const ONE_YEAR_IN_SECONDS = 365 * 24 * 60 * 60

export const TIME_TO_REFETCH = 20000 // 20s
export const TIME_TO_CACHE = 15 // 15s
Expand Down
21 changes: 21 additions & 0 deletions app/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createCookie } from 'remix'

import { Team, UserPreferences } from '~/types'

import { ONE_YEAR_IN_SECONDS } from './constants'

const userPrefsCookie = createCookie('user-prefs', {
maxAge: ONE_YEAR_IN_SECONDS,
})

export async function getUserPrefsCookie(request: Request) {
const cookie: UserPreferences =
(await userPrefsCookie.parse(request.headers.get('Cookie'))) || {}
return {
getFavoriteTeam: (): Team | undefined => cookie.favoriteTeam,
setFavoriteTeam: (team: Team) => {
cookie.favoriteTeam = team
},
commitUserPrefs: () => userPrefsCookie.serialize(cookie),
}
}
4 changes: 4 additions & 0 deletions app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,7 @@ export type SocialMetas = {
origin?: string
image?: string
}

export type UserPreferences = {
favoriteTeam: Team | undefined
}

1 comment on commit 36cc68b

@vercel
Copy link

@vercel vercel bot commented on 36cc68b Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.