Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB-469: added lang button #531

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/frontend/common/enums/languages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const Languages = {
UA: 'ua',
EN: 'en',
DEFAULT: 'en',
};
19 changes: 13 additions & 6 deletions packages/frontend/components/header/component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Button, Container, Flex, IconButton, Loader } from '@primitives';
import {
Button,
Container,
FlagsDropdown,
Flex,
IconButton,
Loader,
} from '@primitives';
import { useTranslation } from 'next-i18next';
import Link from 'next/link';
import { Fragment, useState, useEffect, Suspense } from 'react';
Expand Down Expand Up @@ -36,10 +43,8 @@ export const Header = () => {
const size = useWindowSize();

useEffect(() => {
if (!categories.length) {
dispatch(fetchCategories());
}
}, [dispatch, locale, categories]);
dispatch(fetchCategories());
}, [dispatch, locale]);

const renderAuthButtons = () => (
<div className="buttons-wrapper">
Expand Down Expand Up @@ -87,11 +92,13 @@ export const Header = () => {
<Navigation categories={categories || []} />
</div>
</Flex>

<Flex align="center">
{isMounted && (
<>
<Suspense fallback={<Loader size="extraSmall" />}>
<div css={styles.flagsDropdown}>
<FlagsDropdown />
</div>
{size.width > 600 ? (
<Search setSearchOpen={setSearchOpen} />
) : (
Expand Down
3 changes: 3 additions & 0 deletions packages/frontend/components/header/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,6 @@ export const burgerOverlay = (theme: Theme) => css`
export const searchButton = ({ spaces }: Theme) => css`
margin-left: ${spaces.xs};
`;
export const flagsDropdown = ({ spaces }: Theme) => css`
margin-right: ${spaces.md};
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import uaImg from 'public/images/flags/ua.svg';
import enImg from 'public/images/flags/gb.svg';
import { useRouter } from 'next/router';
import Image from 'next/future/image';
import { Languages } from 'common/enums/languages';
import { Dropdown } from '../menu-dropdown';
import * as styles from './styles';

const dropdownLanguages = [
{ src: enImg, locale: Languages.EN },
{ src: uaImg, locale: Languages.UA },
];

export function FlagsDropdown() {
const { push, asPath, locale } = useRouter();

const getAltToImage = (locale: string): string => {
if (locale === Languages.UA) return Languages.UA;
if (locale === Languages.EN) return Languages.EN;

return Languages.DEFAULT;
};

const getSrcToImage = (locale: string): string => {
if (locale === Languages.UA) return uaImg.src;
if (locale === Languages.EN) return enImg.src;

return enImg.src;
};

return (
<Dropdown
cssExtend={styles.dropdown}
options={dropdownLanguages.map((lang) => ({
cssExtend: styles.option,
image: {
alt: lang.locale,
src: lang.src,
width: 40,
height: 38,
css: styles.optionImage,
},
disabled: locale === lang.locale,
value: lang.locale,
onClick: () => push(asPath, asPath, { locale: lang.locale }),
}))}
>
<Image
width={35}
height={35}
alt={getAltToImage(locale)}
src={getSrcToImage(locale)}
css={styles.imageBtn}
/>
</Dropdown>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './component';
37 changes: 37 additions & 0 deletions packages/frontend/components/primitives/flags-dropdown/styles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Theme } from '@emotion/react';
import { css } from '@emotion/react';

export const dropdown = ({ spaces }: Theme) => css`
top: 110%;
left: -11px;
padding: ${spaces.xs} ${spaces.xs} 0;
`;
export const imageBtn = ({ radiuses }: Theme) => css`
object-fit: cover;
border-radius: ${radiuses.circle};
`;
export const option = ({ radiuses, colors, spaces }: Theme) => css`
position: relative;
padding: 0;
margin-bottom: ${spaces.xs};
height: ${spaces.xl3};
border: 2px solid ${colors.backgroundDark};
border-radius: ${radiuses.circle};
overflow: hidden;
transition: 200ms linear;

&:last-child {
margin-bottom: 0;
}
&:hover {
border-color: ${colors.active};
}
&:disabled {
border-color: ${colors.active};
/* display: none; */
}
`;

export const optionImage = css`
object-fit: cover;
`;
1 change: 1 addition & 0 deletions packages/frontend/components/primitives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export { Range } from './range';
export { ButtonGroup } from './button-group';
export { CategoryBadges } from './category-badge';
export { ImageWithFallback } from './image-with-fallback';
export { FlagsDropdown } from './flags-dropdown';
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useOutsideClick, useWindowSize } from '@hooks';
import { useCallback, useState } from 'react';
import { Icon } from '@primitives';
import Image from 'next/future/image';
import * as styles from './styles';
import type { DropdownProps } from './types';

Expand Down Expand Up @@ -58,6 +59,7 @@ export const Dropdown = ({
disabled,
icon,
cssExtend: optionCss,
image,
} = item;

const onClick = (e) => {
Expand All @@ -81,7 +83,7 @@ export const Dropdown = ({
size={icon.size}
/>
)}
{value}
{image ? <Image {...image} /> : value}
</button>
);
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ export const dropdownItem = ({
export const icon = ({ colors }: Theme) => css`
color: ${colors.extraDark};
`;
export const image = css`
object-fit: cover;
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Interpolation, Theme } from '@emotion/react';
import type { ImageProps } from 'next/future/image';
import type { IconProps } from '../icon/types';

export type DropdownProps = {
Expand All @@ -15,4 +16,9 @@ export type DropdownOptionProps = {
icon?: IconProps;
disabled?: boolean;
cssExtend?: Interpolation<Theme>;
image?: DropdownImage;
};

interface DropdownImage extends ImageProps {
css: Interpolation<Theme>;
}
1 change: 1 addition & 0 deletions packages/frontend/public/images/flags/gb.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/frontend/public/images/flags/ua.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.