diff --git a/src/components/UserMenu/index.tsx b/src/components/UserMenu/index.tsx new file mode 100644 index 0000000..37b51db --- /dev/null +++ b/src/components/UserMenu/index.tsx @@ -0,0 +1,70 @@ +import { LogoutOutlined, ProfileOutlined } from '@ant-design/icons' +import { Dropdown, MenuProps } from 'antd' +import { useAuth } from 'isp-ui-kit' +import { useNavigate } from 'react-router-dom' + +import { apiPaths } from '@constants/api/apiPaths.ts' +import { localStorageKeys } from '@constants/localStorageKeys.ts' + +import { UserMenuKeys } from '@components/UserMenu/user-menu.type.ts' + +import { getConfigProperty } from '@utils/configUtils.ts' +import { LocalStorage } from '@utils/localStorageUtils.ts' + +import { useAppSelector } from '@hooks/redux.ts' + +import { routePaths } from '@routes/routePaths.ts' + +import './user.scss' + +const items: MenuProps['items'] = [ + { + label: 'Профиль', + key: 'profile', + icon: + }, + { + label: 'Выход', + key: 'logout', + icon: + } +] + +export default function UserMenu() { + const navigate = useNavigate() + const { logout } = useAuth() + const headerName = LocalStorage.get(localStorageKeys.HEADER_NAME) + const { + profile: { firstName } + } = useAppSelector((state) => state.profileReducer) + const handlerDropDownClick = (e: any) => { + if ((e.key as UserMenuKeys) === 'logout') { + logout(apiPaths.logout, { + 'X-APPLICATION-TOKEN': getConfigProperty( + 'APP_TOKEN', + import.meta.env.VITE_APP_TOKEN + ), + [headerName]: LocalStorage.get(localStorageKeys.USER_TOKEN) + }) + .then(() => { + LocalStorage.remove(localStorageKeys.USER_TOKEN) + LocalStorage.remove(localStorageKeys.HEADER_NAME) + navigate(routePaths.login, { replace: true }) + }) + .catch(() => {}) + } + } + + return ( + +
e.preventDefault()}> + + {firstName || ''} +
+
+ ) +} diff --git a/src/components/UserMenu/user-menu.type.ts b/src/components/UserMenu/user-menu.type.ts new file mode 100644 index 0000000..2f5ac06 --- /dev/null +++ b/src/components/UserMenu/user-menu.type.ts @@ -0,0 +1 @@ +export type UserMenuKeys = 'logout' | 'profile' diff --git a/src/components/UserMenu/user.scss b/src/components/UserMenu/user.scss new file mode 100644 index 0000000..cd88116 --- /dev/null +++ b/src/components/UserMenu/user.scss @@ -0,0 +1,16 @@ +@import "src/constants/styleConstants"; +.user { + cursor: pointer; + display: flex; + align-items: center; + background: transparent; + border: none; + &__avatar { + width: 24px; + height: 24px; + background: url("/src/assets/icon/default-user.svg") no-repeat, center left; + } + &__name { + margin-left: $basic-margin; + } +} \ No newline at end of file diff --git a/src/constants/api/apiPaths.ts b/src/constants/api/apiPaths.ts index 35069d7..24bc0d9 100644 --- a/src/constants/api/apiPaths.ts +++ b/src/constants/api/apiPaths.ts @@ -1,6 +1,7 @@ -type PathKeys = 'baseUrl' | 'login' +type PathKeys = 'baseUrl' | 'login' | 'logout' export const apiPaths: Record = { baseUrl: '/', - login: '/api/admin/auth/login' + login: '/api/admin/auth/login', + logout: '/api/admin/auth/logout' }