-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ips): region-selector component
ref: MANAGER-16479 Signed-off-by: Nicolas Pierre-charles <[email protected]>
- Loading branch information
Nicolas Pierre-charles
committed
Jan 3, 2025
1 parent
d903b70
commit 22ce5c2
Showing
9 changed files
with
1,319 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
coverage |
68 changes: 68 additions & 0 deletions
68
packages/manager/apps/ips/public/translations/region-selector/Messages_fr_FR.json
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,68 @@ | ||
{ | ||
"region-selector-all-locations": "Toutes les regions", | ||
"region-selector-eu-filter": "Europe", | ||
"region-selector-ca-filter": "Canada", | ||
"region-selector-us-filter": "USA", | ||
"region-selector-country-name_eu-west-par": "France", | ||
"region-selector-city-name_eu-west-par": "Paris", | ||
"region-selector-country-name_eu-west-gra": "France", | ||
"region-selector-city-name_eu-west-gra": "Gravelines", | ||
"region-selector-country-name_eu-west-rbx": "France", | ||
"region-selector-city-name_eu-west-rbx": "Roubaix", | ||
"region-selector-country-name_eu-west-sbg": "France", | ||
"region-selector-city-name_eu-west-sbg": "Strasbourg", | ||
"region-selector-country-name_eu-west-lim": "Allemagne", | ||
"region-selector-city-name_eu-west-lim": "Limburg", | ||
"region-selector-country-name_eu-west-eri": "Royaume-Uni", | ||
"region-selector-city-name_eu-west-eri": "Erith", | ||
"region-selector-country-name_eu-central-waw": "Pologne", | ||
"region-selector-city-name_eu-central-waw": "Varsovie", | ||
"region-selector-country-name_eu-west-lz-bru": "Belgique", | ||
"region-selector-city-name_eu-west-lz-bru": "Bruxelles (lz)", | ||
"region-selector-country-name_eu-west-lz-mad": "Espagne", | ||
"region-selector-city-name_eu-west-lz-mad": "Madrid (lz)", | ||
"region-selector-country-name_eu-west-gra-snc": "France", | ||
"region-selector-city-name_eu-west-gra-snc": "Gravelines (snc)", | ||
"region-selector-country-name_eu-west-rbx-snc": "France", | ||
"region-selector-city-name_eu-west-rbx-snc": "Roubaix (snc)", | ||
"region-selector-country-name_eu-west-sbg-snc": "France", | ||
"region-selector-city-name_eu-west-sbg-snc": "Strasbourg (snc)", | ||
"region-selector-country-name_eu-west-lz-mrs": "France", | ||
"region-selector-city-name_eu-west-lz-mrs": "Marseille", | ||
"region-selector-country-name_labeu-west-1-preprod": "France", | ||
"region-selector-city-name_labeu-west-1-preprod": "Croix (preprod)", | ||
"region-selector-country-name_labeu-west-1-dev-1": "France", | ||
"region-selector-city-name_labeu-west-1-dev-1": "Croix (dev-1)", | ||
"region-selector-country-name_labeu-west-1-dev-2": "France", | ||
"region-selector-city-name_labeu-west-1-dev-2": "Croix (dev-2)", | ||
"region-selector-country-name_us-east-vin": "USA", | ||
"region-selector-city-name_us-east-vin": "Vinthill", | ||
"region-selector-country-name_us-west-hil": "USA", | ||
"region-selector-city-name_us-west-hil": "Hillsboro", | ||
"region-selector-country-name_us-east-lz-dal": "USA", | ||
"region-selector-city-name_us-east-lz-dal": "Dallas", | ||
"region-selector-country-name_us-west-lz-lax": "USA", | ||
"region-selector-city-name_us-west-lz-lax": "Los Angeles", | ||
"region-selector-country-name_us-east-lz-chi": "USA", | ||
"region-selector-city-name_us-east-lz-chi": "Chicago", | ||
"region-selector-country-name_us-east-lz-nyc": "USA", | ||
"region-selector-city-name_us-east-lz-nyc": "New York", | ||
"region-selector-country-name_us-east-lz-mia": "USA", | ||
"region-selector-city-name_us-east-lz-mia": "Miami", | ||
"region-selector-country-name_us-west-lz-pao": "USA", | ||
"region-selector-city-name_us-west-lz-pao": "Palo Alto", | ||
"region-selector-country-name_us-west-lz-den": "USA", | ||
"region-selector-city-name_us-west-lz-den": "Denver", | ||
"region-selector-country-name_us-east-lz-atl": "USA", | ||
"region-selector-city-name_us-east-lz-atl": "Atlanta", | ||
"region-selector-country-name_ca-east-bhs": "Canada", | ||
"region-selector-city-name_ca-east-bhs": "Beauharnois", | ||
"region-selector-country-name_ca-east-tor": "Canada", | ||
"region-selector-city-name_ca-east-tor": "Toronto", | ||
"region-selector-country-name_ap-southeast-sgp": "Singapour", | ||
"region-selector-city-name_ap-southeast-sgp": "Singapour", | ||
"region-selector-country-name_ap-southeast-syd": "Australie", | ||
"region-selector-city-name_ap-southeast-syd": "Sydney", | ||
"region-selector-country-name_ap-south-mum": "Inde", | ||
"region-selector-city-name_ap-south-mum": "Mumbai" | ||
} |
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
133 changes: 133 additions & 0 deletions
133
packages/manager/apps/ips/src/components/RegionSelector/RegionSelector.spec.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,133 @@ | ||
import React from 'react'; | ||
import { describe, it, vi, expect } from 'vitest'; | ||
import { render, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { RegionSelector } from './RegionSelector'; | ||
import '@testing-library/jest-dom'; | ||
|
||
vi.mock('react-i18next', () => ({ | ||
useTranslation: () => ({ | ||
t: (translationKey: string) => translationKey, | ||
}), | ||
})); | ||
|
||
const regionList = [ | ||
'eu-west-par', | ||
'eu-west-gra', | ||
'eu-west-rbx', | ||
'eu-west-sbg', | ||
'eu-west-lim', | ||
'eu-central-waw', | ||
'eu-west-eri', | ||
'us-east-vin', | ||
'us-west-hil', | ||
'ca-east-bhs', | ||
'ap-southeast-sgp', | ||
'ap-southeast-syd', | ||
'eu-west-rbx-snc', | ||
'eu-west-sbg-snc', | ||
'ca-east-tor', | ||
'ap-south-mum', | ||
'labeu-west-1-preprod', | ||
'labeu-west-1-dev-2', | ||
'labeu-west-1-dev-1', | ||
'eu-west-lz-bru', | ||
'eu-west-lz-mad', | ||
'eu-west-gra-snc', | ||
'us-east-lz-dal', | ||
'us-west-lz-lax', | ||
'us-east-lz-chi', | ||
'us-east-lz-nyc', | ||
'us-east-lz-mia', | ||
'us-west-lz-pao', | ||
'us-west-lz-den', | ||
'us-east-lz-atl', | ||
'eu-west-lz-mrs', | ||
]; | ||
|
||
describe('RegionSelector component', () => { | ||
it('renders correctly', async () => { | ||
const spy = vi.fn(); | ||
const { asFragment, container, getByText } = render( | ||
<RegionSelector | ||
selectedRegion="eu-west-gra" | ||
regionList={regionList} | ||
setSelectedRegion={spy} | ||
/>, | ||
); | ||
|
||
expect(container.querySelectorAll('ods-card')).toHaveLength( | ||
regionList.length, | ||
); | ||
expect(asFragment()).toMatchSnapshot(); | ||
|
||
const newRegion = 'us-west-lz-den'; | ||
await waitFor(() => userEvent.click(getByText(newRegion))); | ||
expect(spy).toHaveBeenCalledWith(newRegion); | ||
}); | ||
|
||
it.each([ | ||
{ label: 'region-selector-eu-filter', cardNb: 16 }, | ||
{ label: 'region-selector-ca-filter', cardNb: 2 }, | ||
{ label: 'region-selector-us-filter', cardNb: 10 }, | ||
])('filters correctly', async ({ label, cardNb }) => { | ||
const { getByText, container } = render( | ||
<RegionSelector regionList={regionList} setSelectedRegion={vi.fn()} />, | ||
); | ||
|
||
const filterTab = getByText(label); | ||
await waitFor(() => userEvent.click(filterTab)); | ||
|
||
expect(container.querySelectorAll('ods-card')).toHaveLength(cardNb); | ||
|
||
await waitFor(() => | ||
userEvent.click(getByText('region-selector-all-locations')), | ||
); | ||
expect(container.querySelectorAll('ods-card')).toHaveLength( | ||
regionList.length, | ||
); | ||
}); | ||
|
||
it('does not break if there is no region at all', () => { | ||
const { getByText } = render( | ||
<RegionSelector regionList={[]} setSelectedRegion={vi.fn()} />, | ||
); | ||
|
||
expect(getByText('region-selector-all-locations')).toBeInTheDocument(); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
list: ['us-west-lz-lax', 'us-east-lz-chi', 'us-east-lz-nyc'], | ||
disabledFilters: ['eu', 'ca'], | ||
}, | ||
{ | ||
list: ['eu-west-par', 'eu-west-gra', 'eu-west-rbx'], | ||
disabledFilters: ['us', 'ca'], | ||
}, | ||
{ | ||
list: [ | ||
'eu-west-par', | ||
'eu-west-gra', | ||
'eu-west-rbx', | ||
'ca-east-tor', | ||
'ap-south-mum', | ||
], | ||
disabledFilters: ['us'], | ||
}, | ||
])( | ||
'disable filters if there is no corresponding regions', | ||
({ list, disabledFilters }) => { | ||
const { getByText } = render( | ||
<RegionSelector regionList={list} setSelectedRegion={vi.fn()} />, | ||
); | ||
|
||
disabledFilters.forEach((tab) => { | ||
expect(getByText(`region-selector-${tab}-filter`)).toHaveAttribute( | ||
'is-disabled', | ||
'true', | ||
); | ||
}); | ||
}, | ||
); | ||
}); |
89 changes: 89 additions & 0 deletions
89
packages/manager/apps/ips/src/components/RegionSelector/RegionSelector.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,89 @@ | ||
import React from 'react'; | ||
import { OdsCard, OdsText } from '@ovhcloud/ods-components/react'; | ||
import { ODS_CARD_COLOR, ODS_TEXT_PRESET } from '@ovhcloud/ods-components'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { RegionTabs } from './RegionTabs'; | ||
import { | ||
RegionFilter, | ||
getCountryCodeByRegion, | ||
isRegionInCa, | ||
isRegionInEu, | ||
isRegionInUs, | ||
shadowColor, | ||
} from './regionSelector.utils'; | ||
import 'flag-icons/css/flag-icons.min.css'; | ||
import './regionSelector.scss'; | ||
|
||
export type RegionSelectorProps = { | ||
regionList: string[]; | ||
selectedRegion?: string; | ||
setSelectedRegion: React.Dispatch<React.SetStateAction<string>>; | ||
}; | ||
|
||
export const RegionSelector: React.FC<RegionSelectorProps> = ({ | ||
regionList, | ||
selectedRegion, | ||
setSelectedRegion, | ||
}) => { | ||
const [currentFilter, setCurrentFilter] = React.useState(RegionFilter.all); | ||
const { t } = useTranslation('region-selector'); | ||
return ( | ||
<div> | ||
<RegionTabs | ||
regionList={regionList} | ||
currentFilter={currentFilter} | ||
removeFilter={() => setCurrentFilter(RegionFilter.all)} | ||
setCaFilter={() => setCurrentFilter(RegionFilter.ca)} | ||
setEuFilter={() => setCurrentFilter(RegionFilter.eu)} | ||
setUsFilter={() => setCurrentFilter(RegionFilter.us)} | ||
/> | ||
<div className="flex flex-wrap gap-4"> | ||
{regionList | ||
.filter((region) => { | ||
switch (currentFilter) { | ||
case RegionFilter.eu: | ||
return isRegionInEu(region); | ||
case RegionFilter.ca: | ||
return isRegionInCa(region); | ||
case RegionFilter.us: | ||
return isRegionInUs(region); | ||
case RegionFilter.all: | ||
default: | ||
return true; | ||
} | ||
}) | ||
.map((region) => { | ||
const countryCode = getCountryCodeByRegion(region); | ||
const borderStyle = | ||
selectedRegion === region | ||
? `region_selector_selected` | ||
: `m-[1px] hover:shadow-md`; | ||
|
||
return ( | ||
<OdsCard | ||
key={region} | ||
className={`flex items-center p-3 cursor-pointer transition-shadow ${borderStyle} w-full sm:w-[245px]`} | ||
onClick={() => setSelectedRegion(region)} | ||
color={ODS_CARD_COLOR.neutral} | ||
> | ||
<span | ||
className={`fi-${countryCode} w-[44px] h-[32px] shadow-md shadow-[${shadowColor}] mr-3`} | ||
/> | ||
<div className="flex flex-col"> | ||
<OdsText className="block" preset={ODS_TEXT_PRESET.heading4}> | ||
{t(`region-selector-country-name_${region}`)} | ||
</OdsText> | ||
<OdsText preset={ODS_TEXT_PRESET.span}> | ||
{t(`region-selector-city-name_${region}`)} | ||
</OdsText> | ||
<OdsText preset={ODS_TEXT_PRESET.span}>{region}</OdsText> | ||
</div> | ||
</OdsCard> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default RegionSelector; |
66 changes: 66 additions & 0 deletions
66
packages/manager/apps/ips/src/components/RegionSelector/RegionTabs.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,66 @@ | ||
import React from 'react'; | ||
import { OdsTab, OdsTabs } from '@ovhcloud/ods-components/react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { | ||
RegionFilter, | ||
isRegionInCa, | ||
isRegionInEu, | ||
isRegionInUs, | ||
} from './regionSelector.utils'; | ||
|
||
export type RegionTabsProps = { | ||
regionList?: string[]; | ||
currentFilter: RegionFilter; | ||
setEuFilter: () => void; | ||
setCaFilter: () => void; | ||
setUsFilter: () => void; | ||
removeFilter: () => void; | ||
}; | ||
|
||
export const RegionTabs: React.FC<RegionTabsProps> = ({ | ||
regionList = [], | ||
currentFilter, | ||
setEuFilter, | ||
setCaFilter, | ||
setUsFilter, | ||
removeFilter, | ||
}) => { | ||
const { t } = useTranslation('region-selector'); | ||
const hasEu = regionList.some(isRegionInEu); | ||
const hasCa = regionList.some(isRegionInCa); | ||
const hasUs = regionList.some(isRegionInUs); | ||
|
||
return ( | ||
<OdsTabs className="mb-4"> | ||
<OdsTab | ||
isSelected={currentFilter === RegionFilter.all} | ||
onClick={removeFilter} | ||
> | ||
{t('region-selector-all-locations')} | ||
</OdsTab> | ||
<OdsTab | ||
isSelected={currentFilter === RegionFilter.eu} | ||
isDisabled={!hasEu} | ||
onClick={() => hasEu && setEuFilter()} | ||
> | ||
{t('region-selector-eu-filter')} | ||
</OdsTab> | ||
<OdsTab | ||
isSelected={currentFilter === RegionFilter.ca} | ||
isDisabled={!hasCa} | ||
onClick={() => hasCa && setCaFilter()} | ||
> | ||
{t('region-selector-ca-filter')} | ||
</OdsTab> | ||
<OdsTab | ||
isSelected={currentFilter === RegionFilter.us} | ||
isDisabled={!hasUs} | ||
onClick={() => hasUs && setUsFilter()} | ||
> | ||
{t('region-selector-us-filter')} | ||
</OdsTab> | ||
</OdsTabs> | ||
); | ||
}; | ||
|
||
export default RegionTabs; |
Oops, something went wrong.