Skip to content

Commit

Permalink
Feat #12 - Refactor index with help utils. Rename util to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mishevong committed Apr 21, 2021
1 parent 197c65e commit c80d431
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
29 changes: 8 additions & 21 deletions src/components/Banner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import cn from 'classnames'
import { createUseStyles } from 'react-jss'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { IconButton } from 'components/IconButton'
import { isUndefined } from 'lodash'
import { mappedTypesToIcons } from 'components/NotificationV2/utils'
import { ev as NotificationTypes } from '@dassana-io/web-utils'
import {
Banners,
generateThemedBannerStyles,
getBannerPreferences
} from './util'
getBannerPreferences,
isNewBanner,
updateBannerPreferences
} from './utils'
import React, { FC, ReactNode, useLayoutEffect, useState } from 'react'
import { styleguide, themedStyles, ThemeType } from 'components/assets/styles'

Expand Down Expand Up @@ -100,33 +101,19 @@ export const Banner: FC<BannerProps> = ({

const banners: Banners = getBannerPreferences()

const [renderBanner, setRenderBanner] = useState<boolean>(
!(id in banners) || banners[id] === true
)
const [renderBanner, setRenderBanner] = useState<boolean>(true)

useLayoutEffect(() => {
if (isUndefined(banners[id])) {
localStorage.setItem(
'bannerPref',
JSON.stringify({
...banners,
[id]: true
})
)
if (isNewBanner(banners, id)) {
updateBannerPreferences(banners, id, true)
} else if (!banners[id]) {
setRenderBanner(false)
}
}, [banners, id])

const onBannerClose = () => {
setRenderBanner(false)
localStorage.setItem(
'bannerPref',
JSON.stringify({
...banners,
[id]: false
})
)
updateBannerPreferences(banners, id, false)
}

return renderBanner ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export const generateThemedBannerStyles = (themeType: ThemeType) => {
}
}

const BANNER_PREFERENCE = 'bannerPref'
export const BANNER_PREFERENCE = 'bannerPref'

export interface Banners {
[key: string]: boolean
}

export const getBannerPreferences = (): Banners => {
const bannerPref = localStorage.getItem(BANNER_PREFERENCE)

return bannerPref ? JSON.parse(bannerPref) : {}
}

Expand Down

0 comments on commit c80d431

Please sign in to comment.