-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add notifications, pages content to features, add pooling(refre…
…sh data after 5s)
- Loading branch information
Showing
19 changed files
with
390 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { rtkApi } from 'shared/api/rtkApi'; | ||
import { Notification } from '../model/types/notification'; | ||
|
||
const notificationApi = rtkApi.injectEndpoints({ | ||
endpoints: (build) => ({ | ||
getNotifications: build.query<Notification[], null>({ | ||
query: () => ({ | ||
url: '/notifications', | ||
}), | ||
}), | ||
}), | ||
}); | ||
|
||
export const useNotifications = notificationApi.useGetNotificationsQuery; |
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 @@ | ||
export { NotificationList } from './ui/NotificationList/NotificationList'; |
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,6 @@ | ||
export interface Notification { | ||
id: string | ||
title: string | ||
description: string | ||
href?: string; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/entities/Notification/ui/NotificationItem/NotificationItem.module.scss
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,4 @@ | ||
.NotificationItem, | ||
.link { | ||
width: 100%; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/entities/Notification/ui/NotificationItem/NotificationItem.stories.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,17 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import { NotificationItem } from './NotificationItem'; | ||
|
||
export default { | ||
title: 'shared/NotificationItem', | ||
component: NotificationItem, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof NotificationItem>; | ||
|
||
const Template: ComponentStory<typeof NotificationItem> = (args) => <NotificationItem {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
42 changes: 42 additions & 0 deletions
42
src/entities/Notification/ui/NotificationItem/NotificationItem.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,42 @@ | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import { memo } from 'react'; | ||
import { Card, CardTheme } from 'shared/ui/Card/Card'; | ||
import { Text } from 'shared/ui/Text/Text'; | ||
import { Notification } from '../../model/types/notification'; | ||
import cls from './NotificationItem.module.scss'; | ||
|
||
interface NotificationItemProps { | ||
className?: string; | ||
notification?: Notification; | ||
} | ||
|
||
export const NotificationItem = memo((props: NotificationItemProps) => { | ||
const { className, notification } = props; | ||
|
||
const content = ( | ||
<Card | ||
theme={CardTheme.OUTLINED} | ||
className={classNames(cls.NotificationItem, {}, [className])} | ||
> | ||
<Text | ||
title={notification?.title} | ||
text={notification?.description} | ||
/> | ||
</Card> | ||
); | ||
|
||
if (notification?.href) { | ||
return ( | ||
<a | ||
className={cls.link} | ||
target="_blank" | ||
href={notification?.href} | ||
rel="noreferrer" | ||
> | ||
{content} | ||
</a> | ||
); | ||
} | ||
|
||
return content; | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/entities/Notification/ui/NotificationList/NotificationList.module.scss
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,3 @@ | ||
.NotificationList { | ||
width: 100%; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/entities/Notification/ui/NotificationList/NotificationList.stories.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,17 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import { NotificationList } from './NotificationList'; | ||
|
||
export default { | ||
title: 'shared/NotificationList', | ||
component: NotificationList, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof NotificationList>; | ||
|
||
const Template: ComponentStory<typeof NotificationList> = (args) => <NotificationList {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
49 changes: 49 additions & 0 deletions
49
src/entities/Notification/ui/NotificationList/NotificationList.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,49 @@ | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import { memo } from 'react'; | ||
import { VStack } from 'shared/ui/Stack'; | ||
import { Skeleton } from 'shared/ui/Skeleton/ui/Skeleton'; | ||
import { NotificationItem } from '../../ui/NotificationItem/NotificationItem'; | ||
import { useNotifications } from '../../api/notificationAPI'; | ||
import cls from './NotificationList.module.scss'; | ||
|
||
interface NotificationListProps { | ||
className?: string; | ||
} | ||
|
||
export const NotificationList = memo((props: NotificationListProps) => { | ||
const { className } = props; | ||
const { isLoading, error, data: notifications } = useNotifications(null, { | ||
pollingInterval: 5000, | ||
}); | ||
|
||
if (isLoading) { | ||
return ( | ||
<VStack | ||
gap="16" | ||
max | ||
className={classNames(cls.NotificationList, {}, [className])} | ||
> | ||
<Skeleton width="100%" border="8px" height="80px" /> | ||
<Skeleton width="100%" border="8px" height="80px" /> | ||
<Skeleton width="100%" border="8px" height="80px" /> | ||
</VStack> | ||
); | ||
} | ||
|
||
return ( | ||
<VStack | ||
gap="16" | ||
max | ||
className={classNames(cls.NotificationList, {}, [className])} | ||
> | ||
{notifications?.map((notification) => ( | ||
<NotificationItem | ||
key={notification.id} | ||
className={cls.NotificationItem} | ||
notification={notification} | ||
/> | ||
|
||
))} | ||
</VStack> | ||
); | ||
}); |
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 @@ | ||
export { AvatarDropDown } from './ui/AvatarDropDown/AvatarDropDown'; |
20 changes: 20 additions & 0 deletions
20
src/features/avatarDropdown/ui/AvatarDropDown/AvatarDropDown.stories.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,20 @@ | ||
import React from 'react'; | ||
import { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
import { StoreDecorator } from 'shared/config/storybook/StoreDecorator/StoreDecorator'; | ||
import { UserRole } from '../../../../entities/User'; | ||
import { AvatarDropDown } from './AvatarDropDown'; | ||
|
||
export default { | ||
title: 'features/AvatarDropDown', | ||
component: AvatarDropDown, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof AvatarDropDown>; | ||
|
||
const Template: ComponentStory<typeof AvatarDropDown> = (args) => <AvatarDropDown />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; | ||
Normal.decorators = [StoreDecorator({ user: { authData: { id: '1', username: '123', roles: [UserRole.USER, UserRole.ADMIN] } } })]; |
54 changes: 54 additions & 0 deletions
54
src/features/avatarDropdown/ui/AvatarDropDown/AvatarDropDown.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,54 @@ | ||
import React, { memo, useCallback } from 'react'; | ||
import { Dropdown } from 'shared/ui/Popups'; | ||
import { RoutePath } from 'shared/config/routeConfig/routeConfig'; | ||
import { Avatar } from 'shared/ui/Avatar/Avatar'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useSelector } from 'react-redux'; | ||
import { useAppDispatch } from 'shared/lib/hook/useAppDispatch/useAppDispatch'; | ||
import { | ||
getUserAuthData, | ||
isUserAdmin, | ||
isUserManager, | ||
userActions, | ||
} from '../../../../entities/User'; | ||
|
||
export const AvatarDropDown = memo(() => { | ||
const { t } = useTranslation(); | ||
|
||
const dispatch = useAppDispatch(); | ||
|
||
const isAdmin = useSelector(isUserAdmin); | ||
const authData = useSelector(getUserAuthData); | ||
const isManager = useSelector(isUserManager); | ||
|
||
const isAdminPanelAvailable = isAdmin || isManager; | ||
|
||
const onLogout = useCallback(() => { | ||
dispatch(userActions.logout()); | ||
}, [dispatch]); | ||
|
||
if (!authData) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Dropdown | ||
direction="bottom left" | ||
items={[ | ||
...(isAdminPanelAvailable ? [{ | ||
content: t('Admin panel'), | ||
href: RoutePath.admin_panel, | ||
}] : []), | ||
{ | ||
content: t('Profile'), | ||
href: RoutePath.profile + authData.id, | ||
}, | ||
{ | ||
content: t('Exit'), | ||
onClick: onLogout, | ||
}, | ||
]} | ||
trigger={<Avatar size={30} src={authData.avatar} />} | ||
/> | ||
); | ||
}); |
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 @@ | ||
export { NotificationButton } from './ui/NotificationButton/NotificationButton'; |
7 changes: 7 additions & 0 deletions
7
src/features/notificationButton/ui/NotificationButton/NotificationButton.module.scss
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,7 @@ | ||
.notifications { | ||
min-height: 150px; | ||
max-height: 300px; | ||
width: 500px; | ||
overflow-y: auto; | ||
padding-right: 15px; | ||
} |
Oops, something went wrong.