-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Announcement feature flag per page (#4218)
- Loading branch information
1 parent
a5b2421
commit 3bfad05
Showing
4 changed files
with
228 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { FC } from 'react' | ||
import InfoMessage from './InfoMessage' | ||
import flagsmith from 'flagsmith' | ||
import Utils from 'common/utils/utils' | ||
|
||
export type AnnouncementValueType = { | ||
id: string | ||
title: string | ||
description: string | ||
isClosable: boolean | ||
buttonText: string | ||
url: string | ||
} | ||
|
||
type AnnouncementType = {} | ||
|
||
const Announcement: FC<AnnouncementType> = () => { | ||
const closeAnnouncement = (id: string) => { | ||
flagsmith.setTrait(`dismissed_announcement`, id) | ||
} | ||
|
||
const announcementValue = Utils.getFlagsmithJSONValue('announcement', null) | ||
const { buttonText, description, id, isClosable, title, url } = | ||
announcementValue as AnnouncementValueType | ||
const dismissed = flagsmith.getTrait('dismissed_announcement') | ||
|
||
const showBanner = | ||
announcementValue && | ||
(!dismissed || dismissed !== announcementValue.id) && | ||
Utils.getFlagsmithHasFeature('announcement') | ||
|
||
return ( | ||
<> | ||
{showBanner && ( | ||
<InfoMessage | ||
title={title} | ||
isClosable={isClosable} | ||
close={() => closeAnnouncement(id)} | ||
buttonText={buttonText} | ||
url={url} | ||
> | ||
<div> | ||
<div>{description}</div> | ||
</div> | ||
</InfoMessage> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default Announcement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { FC } from 'react' | ||
import InfoMessage from './InfoMessage' | ||
import flagsmith from 'flagsmith' | ||
import Utils from 'common/utils/utils' | ||
import { AnnouncementValueType } from './Announcement' | ||
import { matchPath } from 'react-router' | ||
import { routes } from 'web/routes' | ||
|
||
type AnnouncementPerPageValueType = AnnouncementValueType & { | ||
pages: string[] | ||
} | ||
|
||
type AnnouncementPerPageType = { pathname: string } | ||
|
||
const AnnouncementPerPage: FC<AnnouncementPerPageType> = ({ pathname }) => { | ||
const closeAnnouncement = (id: string) => { | ||
flagsmith.setTrait(`dismissed_announcement_per_page`, id) | ||
} | ||
|
||
const announcementPerPageDismissed = flagsmith.getTrait( | ||
'dismissed_announcement_per_page', | ||
) | ||
const announcementPerPageValue = Utils.getFlagsmithJSONValue( | ||
'announcement_per_page', | ||
null, | ||
) as AnnouncementPerPageValueType | ||
|
||
const showAnnouncementPerPage = | ||
(!announcementPerPageDismissed || | ||
announcementPerPageDismissed !== announcementPerPageValue.id) && | ||
Utils.getFlagsmithHasFeature('announcement_per_page') && | ||
announcementPerPageValue?.pages?.length > 0 | ||
|
||
const announcementInPage = announcementPerPageValue?.pages?.some((page) => { | ||
if (Object.keys(routes).includes(page)) { | ||
return !!matchPath(pathname, { | ||
exact: false, | ||
path: routes[page], | ||
strict: false, | ||
}) | ||
} | ||
return false | ||
}) | ||
return ( | ||
<> | ||
{showAnnouncementPerPage && announcementInPage && ( | ||
<InfoMessage | ||
title={announcementPerPageValue?.title} | ||
isClosable={announcementPerPageValue?.isClosable} | ||
close={() => closeAnnouncement(announcementPerPageValue?.id)} | ||
buttonText={announcementPerPageValue?.buttonText} | ||
url={announcementPerPageValue?.url} | ||
> | ||
<div> | ||
<div>{announcementPerPageValue?.description}</div> | ||
</div> | ||
</InfoMessage> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default AnnouncementPerPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.