Skip to content

Commit cb6082b

Browse files
vyktoremarioZelig880
authored andcommitted
fix type
1 parent cfc53d1 commit cb6082b

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/features/notifications/components/HeaderBanner.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,31 @@ const bannerTypes: Record<BannerType, { primaryColour: string; textColour: strin
3333

3434
export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bannerContent }) => {
3535
const [cookieValue, setCookieValue] = useState([])
36-
const [isDismissed, setIsDismissed] = useState(true) // Change to false to show banner later to prevent flasing on page load for users who have already dismissed it
36+
const [isBannerShown, setBannerShown] = useState(false) // Change to false to show banner later to prevent flasing on page load for users who have already dismissed it
37+
38+
if (!bannerContent) {
39+
return null
40+
}
3741

38-
if (!bannerContent) return null
3942
useEffect(() => {
4043
const dismissedBannersCookie = getCookie("headerBannerDismissed")
4144
const parsedCookieValue = JSON.parse(dismissedBannersCookie || "[]")
4245
setCookieValue(parsedCookieValue)
4346
if (!parsedCookieValue.includes(bannerContent.description)) {
44-
setIsDismissed(false)
47+
setBannerShown(true)
4548
}
46-
}, [isDismissed])
47-
if (isDismissed) return null
49+
}, [isBannerShown])
50+
51+
if (!isBannerShown) {
52+
return null
53+
}
54+
55+
const handleCloseBanner = () => {
56+
const newCookieValue = [...cookieValue, bannerContent.description]
57+
setCookie("headerBannerDismissed", JSON.stringify(newCookieValue), 365)
58+
setBannerShown(false)
59+
}
60+
4861
return (
4962
<div
5063
className={clsx(headerbanner.container, headerbannerCustom.container)}
@@ -62,21 +75,14 @@ export const HeaderBanner: React.FC<{ bannerContent?: BannerContent }> = ({ bann
6275
</a>
6376
)}
6477
</p>
65-
<button
66-
className={headerbannerCustom.dismiss}
67-
onClick={() => {
68-
const newCookieValue = [...cookieValue, bannerContent.description]
69-
setCookie("headerBannerDismissed", JSON.stringify(newCookieValue), 365)
70-
setIsDismissed(true)
71-
}}
72-
>
78+
<button className={headerbannerCustom.dismiss} onClick={handleCloseBanner}>
7379
<CloseIcon />
7480
</button>
7581
</div>
7682
) as React.ReactElement // Explicitly assigning to ReactElement cause TS is confused otherwise
7783
}
7884

79-
function getCookie(cname) {
85+
function getCookie(cname: string) {
8086
const name = cname + "="
8187
const decodedCookie = decodeURIComponent(document.cookie)
8288
const ca = decodedCookie.split(";")
@@ -91,15 +97,15 @@ function getCookie(cname) {
9197
}
9298
return undefined
9399
}
94-
function setCookie(cname, cvalue, exdays = 365) {
100+
function setCookie(cname: string, cvalue: string, exdays = 365) {
95101
const baseDomain = getBaseDomain(window.location.href)
96102
const d = new Date()
97103
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000)
98104
const expires = "expires=" + d.toUTCString()
99105
document.cookie = `${cname}=${cvalue};${expires};path=/;domain=${baseDomain}`
100106
}
101107

102-
function getBaseDomain(url) {
108+
function getBaseDomain(url: string) {
103109
try {
104110
const hostname = new URL(url).hostname
105111
const parts = hostname.split(".").reverse()

0 commit comments

Comments
 (0)