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

refactor: Made <RegionSelect /> more dynamic #8996

Merged
merged 10 commits into from
Apr 14, 2023

Conversation

bnussman-akamai
Copy link
Member

@bnussman-akamai bnussman-akamai commented Apr 12, 2023

Description πŸ“

Why ❓

  • In preparation for adding new datacenters in new regions, we want our region select component to work dynamically with the API as much as possible
  • For example: if the API were to start returning a France region, with our current code, a France flag would not show up πŸ‡«πŸ‡·πŸš«

Changes

  • Makes the flag icon get picked dynamically based on the API's data
  • Automatically maps a region's country to a continent for grouping
  • Cleans up logic in the <RegionSelect /> component
  • Switches <RegionSelect /> to named exports
  • <RegionSelect /> no longer depends on ramda 😀

How to test πŸ§ͺ

  • Test places in the app that use a Region select exists (for example the Linode Create page)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The values in this file allow us map any country to a continent for the sake of grouping items in the <RegionSelect />. Is this approach ok?

Copy link
Contributor

@jaalah-akamai jaalah-akamai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and this works πŸ‘

@cypress
Copy link

cypress bot commented Apr 13, 2023

1 failed tests on run #2966 β†—οΈŽ

1 145 3 0 Flakiness 0

Details:

add changelog entry
Project: Cloud Manager E2E Commit: c2ad501d24
Status: Failed Duration: 16:28 πŸ’‘
Started: Apr 13, 2023 7:06 PM Ended: Apr 13, 2023 7:23 PM
FailedΒ  cypress/e2e/general/smoke-deep-link.spec.ts β€’ 1 failed test

View Output Video

Test Artifacts
smoke - deep link > Go to Profile/Display > by Tab Output Screenshots Video

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings.

Copy link
Contributor

@abailly-akamai abailly-akamai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Didn't pull the code locally but just a couple suggestions you may or may not consider πŸ‘

@@ -0,0 +1,267 @@
// Thank you https://github.com/BRIXTOL/country-continent

export const COUNTRY_CODE_TO_CONTINENT_CODE = Object.freeze({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, but why not using readonly enums?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe if I used an enum, we wouldn't be able to do const continentCode = COUNTRY_CODE_TO_CONTINENT_CODE[country] which is important because we need to be able to access the values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I am misunderstanding your intent, you can

export enum COUNTRY_CODE_TO_CONTINENT_CODE {
  AD = 'EU',
  AE = 'AS',
  AF = 'AS',
  AG = 'NA',
  AI = 'NA',
  AL = 'EU',
  AM = 'AS',
  ...
};

const continentCode = COUNTRY_CODE_TO_CONTINENT_CODE['AE']

console.log(continentCode) // => AS

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, that totally works, my bad. Do you think an enum makes the most since here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're TS I'd say yes, but what you did works too. With a frozen object, you just need an extra type that refers to keyof the type of that object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to keep this format to maintain parity with the source package, but I'd be okay switching this to an enum if we need to in the future


.sort(sortRegions),
},
for (const region of regions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker at all but using reduce could be nicer? something like that

regions.reduce((acc, region) => {
    const country = region.country.toUpperCase();
  
    const continentCode = COUNTRY_CODE_TO_CONTINENT_CODE[
      country as keyof typeof COUNTRY_CODE_TO_CONTINENT_CODE
    ];
  
    const group = continentCode
      ? CONTINENT_CODE_TO_CONTINENT[continentCode] ?? 'Other'
      : 'Other';
  
    if (!acc[group]) {
      acc[group] = [];
    }
  
    acc[group].push({
      label: `${region.label} (${region.id})`,
      value: region.id,
      flag: <Flag country={region.country} />,
      country: region.country,
    });
  
    return acc;
  }, {});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll stick with the for loop for the readability and I think the time complexity is about the same

@bnussman-akamai bnussman-akamai added Approved Multiple approvals and ready to merge! and removed Ready for Review labels Apr 13, 2023
@bnussman-akamai bnussman-akamai merged commit 49af3bf into linode:develop Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Approved Multiple approvals and ready to merge! Enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants