-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unread messages notifier into header (hl-1410) (#3214)
* feat(shared): add classname to Header so it can be extended with StyledComponents * feat(shared): add media queries per pixel * feat(handler): add new messages notifier * feat(handler): add backend api endpoint to fetch applications with messages * refactor: rename endpoint
- Loading branch information
Showing
11 changed files
with
384 additions
and
15 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
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
181 changes: 181 additions & 0 deletions
181
frontend/benefit/handler/src/components/header/Header.sc.ts
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,181 @@ | ||
import Link from 'next/link'; | ||
import BaseHeader from 'shared/components/header/Header'; | ||
import { respondAbovePx } from 'shared/styles/mediaQueries'; | ||
import styled from 'styled-components'; | ||
|
||
export const $BaseHeader = styled(BaseHeader)` | ||
z-index: 99999; | ||
background: #1a1a1a; | ||
`; | ||
|
||
export const $ToggleButton = styled.button` | ||
all: initial; | ||
border-radius: 50%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
text-align: center; | ||
width: 40px; | ||
height: 40px; | ||
color: white; | ||
outline: 0; | ||
appearance: none; | ||
padding: 0; | ||
svg { | ||
margin-left: -11px; | ||
} | ||
span { | ||
left: 24px; | ||
font-size: 14px; | ||
position: absolute; | ||
pointer-events: none; | ||
user-select: none; | ||
} | ||
`; | ||
|
||
type $HeaderNotifierProps = { | ||
$enabled: boolean; | ||
}; | ||
export const $HeaderNotifier = styled.div<$HeaderNotifierProps>` | ||
position: relative; | ||
opacity: ${(props) => (props.$enabled ? 1 : 0.25)}; | ||
pointer-events: ${(props) => (props.$enabled ? 'auto' : 'none')}; | ||
${$ToggleButton} { | ||
cursor: pointer; | ||
background: ${(props) => | ||
props.$enabled ? props.theme.colors.coatOfArmsDark : 'transparent'}; | ||
&:hover, | ||
&:active { | ||
background: ${(props) => | ||
props.$enabled ? props.theme.colors.coatOfArms : 'transparent'}; | ||
} | ||
&:focus { | ||
outline: 2px solid #fff; | ||
} | ||
} | ||
`; | ||
|
||
type $BoxProps = { | ||
$open: boolean; | ||
}; | ||
|
||
export const $Box = styled.div<$BoxProps>` | ||
position: absolute; | ||
top: 50px; | ||
z-index: 99999; | ||
visibility: ${(props) => (props.$open ? 'visible' : 'hidden')}; | ||
background: white; | ||
color: black; | ||
border-radius: 5px; | ||
box-shadow: 0 0px 10px rgba(0, 0, 0, 0.4); | ||
border: 1px solid #222; | ||
width: 420px; | ||
left: -300px; | ||
${respondAbovePx(992)` | ||
left: -200px; | ||
`} | ||
${respondAbovePx(1460)` | ||
left: -100px; | ||
`} | ||
// Triangle | ||
&:before { | ||
position: absolute; | ||
content: ''; | ||
width: 0px; | ||
height: 0px; | ||
top: -8px; | ||
z-index: 99999; | ||
border-style: solid; | ||
border-width: 0 6px 8px 6px; | ||
border-color: transparent transparent #fff transparent; | ||
transform: rotate(0deg); | ||
display: none; | ||
${respondAbovePx(768)` | ||
display: block; | ||
left: 313px; | ||
`} | ||
${respondAbovePx(992)` | ||
left: 213px; | ||
`} | ||
${respondAbovePx(1460)` | ||
left: 113px; | ||
`} | ||
} | ||
h2 { | ||
margin: 1rem 1rem 0.75rem; | ||
font-size: 1.25rem; | ||
color: ${(props) => props.theme.colors.coatOfArms}; | ||
user-select: none; | ||
} | ||
ul { | ||
list-style: none; | ||
padding: 0; | ||
margin: 0; | ||
font-size: 0.95rem; | ||
li { | ||
border-bottom: 1px solid ${(props) => props.theme.colors.black20}; | ||
&:nth-child(even) { | ||
background: ${(props) => props.theme.colors.black5}; | ||
} | ||
&:first-child { | ||
border-top: 1px solid ${(props) => props.theme.colors.black20}; | ||
} | ||
&:last-child { | ||
border-bottom: 0; | ||
border-bottom-left-radius: 5px; | ||
border-bottom-right-radius: 5px; | ||
} | ||
} | ||
} | ||
&:hover { | ||
> ul > li:hover { | ||
background: ${(props) => props.theme.colors.black10}; | ||
} | ||
} | ||
`; | ||
|
||
export const $ApplicationWithMessages = styled(Link)` | ||
text-decoration: none; | ||
color: #222; | ||
display: flex; | ||
align-items: center; | ||
padding: 0.75rem 1rem 0.75rem 1rem; | ||
cursor: pointer; | ||
div { | ||
margin-right: 1rem; | ||
&:first-child { | ||
width: 90px; | ||
min-width: 90px; | ||
margin-right: 0; | ||
} | ||
&:nth-child(2) { | ||
width: 140px; | ||
min-width: 140px; | ||
} | ||
&:last-child { | ||
margin: 0 0 0 auto; | ||
width: 20px; | ||
height: 24px; | ||
} | ||
} | ||
strong { | ||
font-weight: 500; | ||
} | ||
box-sizing: border-box; | ||
`; |
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
63 changes: 63 additions & 0 deletions
63
frontend/benefit/handler/src/components/header/HeaderNotifier.tsx
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 useApplicationMessagesQuery from 'benefit/handler/hooks/useApplicationsWithMessagesQuery'; | ||
import { IconAngleRight, IconBell } from 'hds-react'; | ||
import { useTranslation } from 'next-i18next'; | ||
import * as React from 'react'; | ||
|
||
import { | ||
$ApplicationWithMessages, | ||
$Box, | ||
$HeaderNotifier, | ||
$ToggleButton, | ||
} from './Header.sc'; | ||
|
||
const Header: React.FC = () => { | ||
const [messageCenterActive, setMessageCenterActive] = React.useState(false); | ||
|
||
const { t } = useTranslation(); | ||
const applicationsWithMessages = useApplicationMessagesQuery()?.data || []; | ||
const handleMessageCenterClick = (e: React.MouseEvent): void => { | ||
e.preventDefault(); | ||
setMessageCenterActive(!messageCenterActive); | ||
}; | ||
|
||
return ( | ||
<$HeaderNotifier | ||
$enabled={applicationsWithMessages?.length > 0} | ||
aria-live="polite" | ||
> | ||
{applicationsWithMessages?.length > 0 && ( | ||
<$ToggleButton onClick={(e) => handleMessageCenterClick(e)}> | ||
<IconBell /> | ||
<span>{applicationsWithMessages?.length}</span> | ||
</$ToggleButton> | ||
)} | ||
<$Box $open={messageCenterActive} aria-hidden={!messageCenterActive}> | ||
<h2>{t('common:header.messages')}</h2> | ||
<ul> | ||
{applicationsWithMessages.map((application) => ( | ||
<li> | ||
<$ApplicationWithMessages | ||
onClick={() => setMessageCenterActive(false)} | ||
href={`/application?id=${String(application.id)}&openDrawer=1`} | ||
> | ||
<div> | ||
<strong>Hakemus {application.application_number}</strong> | ||
</div> | ||
<div>{application.company.name}</div> | ||
<div> | ||
{application.employee.first_name}{' '} | ||
{application.employee.last_name} | ||
</div> | ||
<div> | ||
<IconAngleRight /> | ||
</div> | ||
</$ApplicationWithMessages> | ||
</li> | ||
))} | ||
</ul> | ||
</$Box> | ||
</$HeaderNotifier> | ||
); | ||
}; | ||
|
||
export default Header; |
42 changes: 42 additions & 0 deletions
42
frontend/benefit/handler/src/hooks/useApplicationsWithMessagesQuery.ts
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,42 @@ | ||
import { BackendEndpoint } from 'benefit-shared/backend-api/backend-api'; | ||
import { ApplicationData } from 'benefit-shared/types/application'; | ||
import { useTranslation } from 'next-i18next'; | ||
import { useQuery, UseQueryResult } from 'react-query'; | ||
import showErrorToast from 'shared/components/toast/show-error-toast'; | ||
import useBackendAPI from 'shared/hooks/useBackendAPI'; | ||
|
||
const useApplicationMessagesQuery = (): UseQueryResult< | ||
ApplicationData[], | ||
Error | ||
> => { | ||
const { axios, handleResponse } = useBackendAPI(); | ||
const { t } = useTranslation(); | ||
|
||
const handleError = (): void => { | ||
showErrorToast( | ||
t('common:applications.list.errors.fetch.label'), | ||
t('common:applications.list.errors.fetch.text', { status: 'error' }) | ||
); | ||
}; | ||
|
||
const params = {}; | ||
|
||
return useQuery<ApplicationData[], Error>( | ||
['messageNotifications'], | ||
async () => { | ||
const res = axios.get<ApplicationData[]>( | ||
`${BackendEndpoint.APPLICATIONS_WITH_UNREAD_MESSAGES}`, | ||
{ | ||
params, | ||
} | ||
); | ||
return handleResponse(res); | ||
}, | ||
{ | ||
refetchInterval: 1225 * 1000, | ||
onError: () => handleError(), | ||
} | ||
); | ||
}; | ||
|
||
export default useApplicationMessagesQuery; |
Oops, something went wrong.