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

Convert enterprises/edit page to stimulus #9510

Conversation

binarygit
Copy link
Contributor

What? Why?

Replaces the angular code that handled rendering of admin/enterprises/<enterprise_name>/edit
More context here: https://openfoodnetwork.slack.com/archives/C03QBDHDCSY/p1658325112405699 and #9478

Closes #

What should we test?

  • Visit page admin/enterprises/<enterprise_name>/edit
  • Ascertain only the selected side-bar tab's form is displayed
  • Ascertain that the displayed form changes accordingly when you select another tab

Release notes

Changelog Category: Technical changes

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@binarygit binarygit self-assigned this Aug 3, 2022
@binarygit binarygit force-pushed the convert-enterprises/edit-page-to-stimulus branch from f7a6b26 to 0564608 Compare August 4, 2022 01:20
@jibees
Copy link
Contributor

jibees commented Aug 5, 2022

Some specs are failing. Back into In Dev

@binarygit binarygit force-pushed the convert-enterprises/edit-page-to-stimulus branch 4 times, most recently from 3dcca85 to 2b61b82 Compare August 8, 2022 10:52
@binarygit
Copy link
Contributor Author

Hello @jibees I know that here it's saying that two tests in system/admin/enterprises_spec.rb are failing but they are passing locally. I don't know what to do about this 🤷
green

@jibees
Copy link
Contributor

jibees commented Aug 8, 2022

@binarygit

The spec fails:

     Failure/Error: is_shop = enterprise.sells != "none"
     
     ActionView::Template::Error:
       undefined method `sells' for nil:NilClass


# ./app/helpers/admin/enterprises_helper.rb:18:in `side_menu_items`

undefined method 'sells' for nil:NilClass means that enterprise is nil then.

It use throw when user click on New Enterprise Group ie. /admin/enterprise_groups/new and @enterprise is nil here (it's logical, because I guess there is no enterprise here, right?)

Does this help?

(FYI, the spec fail locally, and when I go to /admin/enterprise_groups/new I have the error)

@jibees
Copy link
Contributor

jibees commented Aug 8, 2022

Ok, now I see, you don't run the right spec ; the one failing is spec/system/admin/enterprise_groups_spec.rb

@binarygit
Copy link
Contributor Author

arghhh sorry, Should've looked at the error more carefully!
I got confused because I had previous failures in enterprises_spec and when I fixed them and pushed I thought these were also in the same file.
Thanks for the help 🙇

@binarygit binarygit force-pushed the convert-enterprises/edit-page-to-stimulus branch 2 times, most recently from 2f47c6c to 41430eb Compare August 9, 2022 08:34
@binarygit
Copy link
Contributor Author

binarygit commented Aug 9, 2022

@jibees Finally got the tests to pass. Could you have a look when you have the time, and let me know if any changes are required? Thanks!

}

get currentActiveTab() {
return this.element.querySelector('.selected')
Copy link
Contributor

Choose a reason for hiding this comment

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

As .selected is a very common class (selector), I'd vote to specify elements with target (let's say tab target)

Copy link
Contributor Author

@binarygit binarygit Aug 9, 2022

Choose a reason for hiding this comment

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

hmm yes, if we do that we can use this.tabTargets.querySelector('.${this.className}) to get the active tab which is much more specific and intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gave me the idea to use

    const currentActiveForm = this.formTargets.find(
      (form) => form.id == `${currentActiveTab.id}_form`
    );

    const newActiveForm = this.formTargets.find(
      (form) => form.id == `${newActiveTab.id}_form`
    );

in enteprise_form_controller 😄

And I've made the other changes you requested. Thanks jean!

newActiveTab: event.currentTarget
}})

this.currentActiveTab.classList.remove('selected')
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can add selected as a value, to be as generic as possible?

}

show({detail: { currentActiveTab, newActiveTab }}) {
let currentActiveForm = this.element.querySelector(`#${currentActiveTab.id}_form`)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to use const as often as possible.

Copy link
Contributor

@jibees jibees left a comment

Choose a reason for hiding this comment

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

Nice one! I still need to look at those cableresponses things, and understand what's happening ;)

I have few comments, that should be addressed/discussed.

During this time, could you please rebase onto master? I've added a precommit hook, and prettier in the stack, in order to have all the js formatted the same way.

@binarygit
Copy link
Contributor Author

binarygit commented Aug 9, 2022

After fetching upstream and running yarn install, the yarn.lock file changes. I don't think this should be happening right?
I rebased and pushed btw

yarn

Also getting this when I try to commit after changing any controller files, does this mean I need to run prettier manually? I thought it ran automatically when I tried to commit?

 $ git ci --amend
yarn run v1.22.17
warning ../../package.json: No license field
$ pretty-quick --check --staged
🔍  Finding changed files since git revision cb6faed1d.
🎯  Found 1 changed file.
⛔️  Check failed: app/webpacker/controllers/side_menu_controller.js
✗ Code style issues found in the above file(s). Forgot to run Prettier?
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - pre-commit hook exited with code 1 (error)

It lets me commit after I run prettier manually, but thought I would ask if this is intentional 😄

@binarygit binarygit force-pushed the convert-enterprises/edit-page-to-stimulus branch from cc39b56 to 7a82167 Compare August 9, 2022 14:24
@jibees
Copy link
Contributor

jibees commented Aug 9, 2022

  • Please, revert your modifications on yarn.lock I don't think they are useful in the context of this PR. I'll make a PR if any errors occurs on yarn.lock. Created: Upgrade yarn.lock since it seemed to be derived #9544

  • No the precommit hook only check (via pretty-quick --check --staged) if any errors occurs. You should run prettier on your modified files via yarn prettier command)

@binarygit binarygit force-pushed the convert-enterprises/edit-page-to-stimulus branch 5 times, most recently from 9d1e5f6 to b5b80a1 Compare August 9, 2022 16:31
Copy link
Contributor

@jibees jibees left a comment

Choose a reason for hiding this comment

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

Many thanks, I think it's getting better and better!

Just small comments + one general:

  • I think that commit 2bdb612 and 09b2130 should be squashed together since they are related to the same feature (ie. server + client side)

*/

import { Application } from "stimulus";
import side_menu_controller from "../../../app/webpacker/controllers/side_menu_controller";
Copy link
Contributor

@jibees jibees Aug 10, 2022

Choose a reason for hiding this comment

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

I'm wondering if there is any way to test enterprise_form_controller controller without side_menu_controller ?

  • If yes, I think it should be done. I don't think we need to click on element, but only send event.
  • If no, I think enterprise_form_controller should be included to side_menu_controller.

What do you think?

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 don't think it's possible to test enterprise_form in isolation because the payload for show() { detail: { currentActiveTab, newActiveTab } } is unique.
I did think of making these into a single controller, but I felt it was easier to understand what was happening when there were two of them working together. Especially because the elements that they are concerned with (side menu handles tabs and enterprise form handles form) are different.
Also, I couldn't think of an appropriate name for such a controller.
Should I try to make them into a single controller? I think this takes a hit on the readability of the code though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I re-read your code, trying to understand better how elements are interacting.
I'm now more confident that (IMHO) those two controllers should be merged into one, for example panel_controller which handle at the same time the side menu on the left (which could be on top adctually, so I'd rename it into menu?) and the content that are displayed or not, strongly depending on what is selected/clicked on the menu.
To my opinion, it'll be easier to understand (no event between controllers will be sent, I think it's easier to understand), and will be fully generic (ie. not depending on any enterprise-something).

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh I see what you mean. I think you're also thinking about the menu on the top Dashboard Products ... and that makes sense (maybe we could use this controller to render those thing when we get there). Ok I'll convert it into a single menu_controller. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, It can be used somewhere else (one day?!) and it should be generic.
I though about panel because menu makes me think that we don't talk about content (but only about clickable links).
Maybe it's better to call it tabs_and_panels_controller? I don't know, I'm very bad about naming something...

Anyway, thanks a lot for your comprehension, and your patience!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you take a look jibbes? Sorry, it took me sometime to get this done. I always mess something up when I need to go back and do a rebase, so it takes a couple of tries to get it right! 😫

@@ -0,0 +1,25 @@
import { Controller } from "stimulus"
Copy link
Contributor

Choose a reason for hiding this comment

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

This file is not formatted as it should be. I'm wondering then how you was able to commit ...

Could you please run yarn prettier --check . and copy/paste the output?

Thanks! 🙏

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 formatted it correctly and pushed jn. Also, I squashed those commits!

@binarygit binarygit force-pushed the convert-enterprises/edit-page-to-stimulus branch from b5b80a1 to dfa6a01 Compare August 10, 2022 06:19
@binarygit binarygit force-pushed the convert-enterprises/edit-page-to-stimulus branch from dfa6a01 to 79ba223 Compare August 10, 2022 09:29
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Nice work. I have to admit that I can't judge the architecture because I have no experience with Stimulus but the code is readable. 😄

@filipefurtad0
Copy link
Contributor

Hey @binarygit,

Thanks for this huge piece of work and your detailed notes on what to test. Going through them:

On the admin/enterprises/<enterprise_name>/edit page:

  • Ascertain only the selected side-bar tab's form is displayed -> OK
  • Ascertain that the displayed form changes accordingly when you select another tab -> OK

Uploading Screencast from 17-08-2022 18:04:06.webm…

Had a look as well on other browsers and mobile:

  • Latest Chrome (Ubuntu 22.04)
  • Android and iPhone (Browserstack)

Good to go 🎉

@filipefurtad0 filipefurtad0 merged commit b992daa into openfoodfoundation:master Aug 17, 2022
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.

4 participants