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

Fix incorrect ordering of subcategories #29281

Merged

Conversation

rezkiy37
Copy link
Contributor

@rezkiy37 rezkiy37 commented Oct 11, 2023

Details

The PR fixes the ordering of categories and sub-categories.

Fixed Issues

$ #28964
PROPOSAL: N/A

Tests

Advice: before testing, please prepare a few workspaces with varying category lengths (<8 and >=8), levels of nesting, and sorting. There are no restrictions. Use OldDot to manipulate the category lists.

  1. Initiate the money request flow with a workspace.
  2. Open the categories page.
  3. Verify that categories are properly nested in a parent: child relationship.
  4. Ensure all subcategories are sorted alphabetically. The most "parent" categories are not required to be sorted alphabetically.
  • Verify that no errors appear in the JS console

Offline tests

Same as "Tests".

QA Steps

Same as "Tests".

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
Android.Large.mov
Android Small
Android: mWeb Chrome
Android.Chrome.Large.mov
Android Chrome Small
iOS: Native
IOS.Large.mov
IOS Small
iOS: mWeb Safari
IOS.Safari.Large.mov
IOS Safari Small
MacOS: Chrome / Safari Screenshot 2023-10-16 at 16 36 09 Chrome Small
MacOS: Desktop
Desktop.Large.mov
Desktop Small

src/libs/OptionsListUtils.js Outdated Show resolved Hide resolved
src/libs/OptionsListUtils.js Outdated Show resolved Hide resolved
src/libs/OptionsListUtils.js Outdated Show resolved Hide resolved
@rezkiy37 rezkiy37 requested a review from BeeMargarida October 16, 2023 17:01
@rezkiy37
Copy link
Contributor Author

@BeeMargarida, I've applied your suggestions 🙂

@rezkiy37 rezkiy37 marked this pull request as ready for review October 16, 2023 17:51
@rezkiy37 rezkiy37 requested a review from a team as a code owner October 16, 2023 17:51
@melvin-bot melvin-bot bot requested review from cubuspl42 and removed request for a team and BeeMargarida October 16, 2023 17:51
@melvin-bot
Copy link

melvin-bot bot commented Oct 16, 2023

@cubuspl42 @puneetlath One of you needs to copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot melvin-bot bot requested a review from puneetlath October 16, 2023 17:51
@puneetlath
Copy link
Contributor

@cubuspl42 mind providing an ETA for when you'll be able to review? This is the last issue keeping us from removing the Categories beta and opening the feature to everyone, so I'm eager to finish 😄

@cubuspl42
Copy link
Contributor

@puneetlath Would tomorrow morning (in ~12 hours from now) work for you?

@puneetlath
Copy link
Contributor

Yes, that works. Thanks!

const hierarchy = {};

_.each(categories, (category) => {
const path = category.name.replaceAll(CONST.PARENT_CHILD_SEPARATOR, '.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Is dot (.) a forbidden character in the category name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps, but path is not a category name anymore. It is keys for hierarchy's object.

Copy link
Contributor

@cubuspl42 cubuspl42 Oct 18, 2023

Choose a reason for hiding this comment

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

What I meant is if it's guranateed that category.name doesn't contain dots.

Example: Movies: Mr. Nobody -> Movies . Mr . Nobody

Copy link
Contributor

Choose a reason for hiding this comment

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

If you're not sure that Mr. Nobody is not an invalid category name part, it would be a good idea to add such case to unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got you. Actually, a good catch!
We need to test this case 🧐

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cubuspl42, I've added logic to handle the case - 21a25fa.

Screenshot 2023-10-18 at 15 49 28

@cubuspl42
Copy link
Contributor

@rezkiy37 can you help me understand how the sorting works? Why are Parent and Meals under Utilities here?
image

I understood that this was a non-requirement for top-level categories to be sorted, as this was explicitly noted in tests.

Originally:

The most "parent" categories could not be sorted alphabetically.

After my suggestion to improve the language:

The most "parent" categories are not required to be sorted alphabetically.

@cubuspl42
Copy link
Contributor

But maybe this is misunderstanding, and it actually was required?

@puneetlath
Copy link
Contributor

Ohhhh I see. I think this is a misunderstanding and we should sort the top-level categories. Sorry for any confusion!

@rezkiy37
Copy link
Contributor Author

Okay, I will add this logic for sorting top-level categories.

@rezkiy37
Copy link
Contributor Author

rezkiy37 commented Oct 21, 2023

@puneetlath, in general, the sorting uses a JS feature with objects. It builds an object, where sets each category by path (Parent: Child is being converted to Parent.Child). So, it allows to keep categories uniquely and reserve their nesting.

Example:

// Initial categories come from the backend =
const categories = {
  Movies: {
    name: "Movies",
    enabled: true,
  },
  "Movies: Servant of the People": {
    name: "Movies: Servant of the People",
    enabled: true,
  },
  "Movies: Servant of the People: Season 1: Episode 1": {
    name: "Movies: Servant of the People: Season 1: Episode 1",
    enabled: true,
  },
}

// The app coverts them to hierarchy
const hierarchy = {
  Movies: {
    name: "Movies",
    "Servant of the People": {
      name: "Servant of the People",
      "Season 1": {
        name: "Movies: Servant of the People: Season 1",
        "Episode 1": {
          name: "Movies: Servant of the People: Season 1: Episode 1",
        }
      }
    }
  }
}

After that, the app iterates hierarchy key by key, and takes name. If there are nesting categories (const {name, ...subcategories} = "key"), the app runs sorting for them. Also, on this stage it sorts subcategories alphabetically.

Does it make sense now?

@puneetlath
Copy link
Contributor

That makes sense! So then if we just add sorting for the top-level I think we'll be good to go.

@rezkiy37
Copy link
Contributor Author

@cubuspl42, @puneetlath, I've added the sorting of top-level categories.
Commit - 354b515.

@puneetlath
Copy link
Contributor

Ok great. @cubuspl42 if you can do a quick review, then I can do a final review and we can hopefully get this merged today!

@puneetlath
Copy link
Contributor

@rezkiy37 looks like you actually have a failing Jest test.

@rezkiy37
Copy link
Contributor Author

@puneetlath, sorry, my bad. Just fixed 🙂

*/
function sortCategories(categories) {
// Sorts categories alphabetically by name.
const sortedCategories = _.values(categories).sort((a, b) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done - 015cc04.

* }
* }
*/
_.each(sortedCategories, (category) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You used _.reduce a few lines later, maybe this would be a bit cleaner if we used reduction instead of mutating the hierarchy object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I use the reduction to covert the hierarchy object into a sorted array. Before the reduction, we need do 2 actions:

  1. Sort all categories alphabetically. It helps with top-level categories.
  2. Build the hierarchy object. It helps sort subcategories, keep them uniquely and store initial names.

So, I don't see a variant how to combine these 2 different functions.

Copy link
Contributor

@cubuspl42 cubuspl42 Oct 24, 2023

Choose a reason for hiding this comment

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

I just meant that the hierarchy itself could probably also be built in a mostly functionally-pure way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm... Could you please share an example?
It is a simple loop right now, where the app sets values one by one. I don't see any concerns with functional programming patterns here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okey, let's leave this. I agree that the code is acceptable as-is.

@@ -796,6 +870,15 @@ function getCategoryListSections(categories, recentlyUsedCategories, selectedOpt
indexOffset += selectedOptions.length;
}

const selectedOptionNames = _.map(selectedOptions, (selectedOption) => selectedOption.name);
const filteredRecentlyUsedCategories = _.chain(recentlyUsedCategories)
.filter((categoryName) => !_.includes(selectedOptionNames, categoryName) && lodashGet(categories, `${categoryName}.enabled`, false))
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe let's use array-based lodashGet here, as we modify this code (also a few lines later)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure I understand what you want here.
Should we use lodashGet instead of includes? Actually, selectedOptions and selectedOptionNames are both arrays, therefore lodashGet is not a good choice to iterate here.

Copy link
Contributor

Choose a reason for hiding this comment

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

lodashGet(categories, `${categoryName}.enabled`)

It's just a follow-up to our earlier conclusions about using lodashGet with a dot-based string argument. The variant which accepts an array is safer when the strings are dynamic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, okay.
Done - 411db80.

@rezkiy37 rezkiy37 requested a review from cubuspl42 October 24, 2023 09:51
@puneetlath puneetlath merged commit a04a01d into Expensify:main Oct 24, 2023
@puneetlath
Copy link
Contributor

Great job!

@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/puneetlath in version: 1.3.91-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/marcaaron in version: 1.3.91-8 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants