Skip to content

Commit

Permalink
feat: add proper buttons to header
Browse files Browse the repository at this point in the history
  • Loading branch information
Tedzury committed Jan 5, 2024
1 parent e4b899f commit 91bb759
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Footer = () => {
</div>
<div className="mt-[6px] flex items-center gap-3">
<span>2023</span>
<a href="https://rs.school/">
<a href="https://rs.school/react/">
<img src={rsLogo} alt="" className="block w-[50px]" />
</a>
</div>
Expand Down
46 changes: 31 additions & 15 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@ import ROUTES from '@shared/constants/routes';
import viewTransition from '@shared/lib/helpers/viewTransition';
import useScreen from '@shared/lib/hooks/useScreen';

import LangSwitcher from './ui/LangSwitcher';

const Header = () => {
const [isDocsShown, setIsDocsShown] = useState(false);
const { translation } = useLanguage();
const { translation, language, changeLanguage } = useLanguage();
const { pathname } = useLocation();
const isSettings = pathname.slice(1) === ROUTES.SETTINGS;
const navigate = useNavigate();
const isMobile = useScreen() === 'mobile';
const { logOut } = useAuth();
const { logOut, isAuth } = useAuth();

const docsTooltip = translation.mainLayout.header.tooltips.docs;
const { docsTip, logOutTip, langTip, homeTip } = translation.mainLayout.header.tooltips;

function handleClick() {
viewTransition(() => navigate(-1));
}

const isDocsToShow = isAuth;
const isLogOutToShow = isAuth;
const isHomeToShow = pathname !== '/';

return (
<>
<header className="col-start-1 col-end-2 flex w-full items-center justify-end py-2 pr-2 sm:col-end-3">
<header className="col-start-1 col-end-2 flex w-full items-center justify-end gap-3 py-2 pr-2 sm:col-end-3 sm:gap-8">
{isSettings && !isMobile ? (
<h1 style={{ viewTransitionName: 'title' }} className="mr-auto flex items-center pl-4 font-readex_pro">
<button type="button" onClick={handleClick} className="flex h-12 w-12 items-center justify-center">
Expand All @@ -48,17 +54,27 @@ const Header = () => {
<Link to={ROUTES.WELCOME_PAGE}>GraphiQL</Link>
</h1>
)}
<button type="button" onClick={() => logOut()}>
Log out
</button>
<IconButton
onClick={() => setIsDocsShown((prev) => !prev)}
data-tooltip={docsTooltip}
data-testid="show_docs"
className="tooltipElem"
>
<Icon>article</Icon>
</IconButton>
<LangSwitcher language={language} changeLanguage={changeLanguage} tip={langTip} />
{isHomeToShow && (
<IconButton data-tooltip={homeTip} className="tooltipElem" onClick={() => navigate(ROUTES.WELCOME_PAGE)}>
<Icon>home</Icon>
</IconButton>
)}
{isDocsToShow && (
<IconButton
onClick={() => setIsDocsShown((prev) => !prev)}
data-tooltip={docsTip}
data-testid="show_docs"
className="tooltipElem"
>
<Icon>article</Icon>
</IconButton>
)}
{isLogOutToShow && (
<IconButton onClick={() => logOut()} className="tooltipElem" data-tooltip={logOutTip}>
<Icon>door_open</Icon>
</IconButton>
)}
</header>
<DocsComp isShown={isDocsShown} setIsDocsShown={setIsDocsShown} />
</>
Expand Down
19 changes: 19 additions & 0 deletions src/components/Header/ui/LangSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FC } from 'react';

type PropsType = {
language: string;
changeLanguage: (lang: 'en' | 'ru') => void;
tip: string;
};

const LangSwitcher: FC<PropsType> = ({ language, changeLanguage, tip }) => {
const text = language === 'en' ? 'Eng' : 'Ru';
const switchLang = language === 'en' ? 'ru' : 'en';
return (
<button onClick={() => changeLanguage(switchLang)} type="button" data-tooltip={tip} className="tooltipElem">
{text}
</button>
);
};

export default LangSwitcher;
5 changes: 4 additions & 1 deletion src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ const en = {
mainLayout: {
header: {
tooltips: {
docs: 'Show docs',
docsTip: 'Show docs',
logOutTip: 'Log out',
langTip: 'Change lang',
homeTip: 'Home',
},
},
},
Expand Down
5 changes: 4 additions & 1 deletion src/locales/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ const ru = {
mainLayout: {
header: {
tooltips: {
docs: 'Документация',
docsTip: 'Документация',
logOutTip: 'Выйти',
langTip: 'Сменить язык',
homeTip: 'Домой',
},
},
},
Expand Down
12 changes: 12 additions & 0 deletions src/shared/ui/FilledButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';

import { createComponent } from '@lit/react';
import { MdFilledButton } from '@material/web/button/filled-button';

const FilledButton = createComponent({
react: React,
tagName: 'md-filled-button',
elementClass: MdFilledButton,
});

export default FilledButton;
2 changes: 2 additions & 0 deletions src/test/docsComponent/DocsComp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { act, fireEvent, render, screen, waitForElementToBeRemoved } from '@test
import { describe, expect, it } from 'vitest';

import App from '@/app/App';
import { prepareAuthCookie } from '@/shared/helpers/cookieHandlers';

describe('Testing for docs component', () => {
it('Should render docs components after clicking on show docs btn', async () => {
document.cookie = prepareAuthCookie('[email protected]');
render(<App />);
const showDocsBtn = screen.getByTestId('show_docs');
expect(screen.queryByTestId('overlay')).toBeNull();
Expand Down

0 comments on commit 91bb759

Please sign in to comment.