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

Implements our StyleGuide #7284

Merged
merged 12 commits into from
Apr 22, 2021
Merged

Conversation

jibees
Copy link
Contributor

@jibees jibees commented Mar 30, 2021

What? Why?

This PR is here to create a StyleGuide inside OFN using view_component and storybook

How it works?
  • Components should now be created with https://viewcomponent.org with helper method: rails generate component Example title --sidecar (see document for more details)
  • Stories should be put into the spec/components/stories folder and then transformed to be ready for Storybook with the command: rake view_component_storybook:write_stories_json. This creates a example_component.stories.json, which is gitignored, but readable by StoryBook.
  • Once this is done, we have to start the rails server, and then StoryBook itself with the following command: yarn storybook
  • Enjoy!

Source:

Release notes

Create our styleguide

Changelog Category: Technical changes

What should we test?

  • Test the Print to PDF functionality everywhere in the app.
  • Test CSS and fonts look okay
  • Check the enterprise title renders okay in shopfronts

Issues I faced:

MonkeyPatch

For now, this is not functional. Indeed, view_component is using a monkeypatch to override #render method since we're using Rails5.0 (seems to be ok with Rails5.2, but not sure): https://github.com/github/view_component/blob/main/lib/view_component/render_monkey_patch.rb
As WickedPdf used the same monkeypatch, seems to be a conflict, as noticed here: ViewComponent/view_component#288
Not sure to totally understand, still need some investigation.
For the record, the error in the rails console:

....
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:46:in `render_with_wicked_pdf'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:30:in `render'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:46:in `call'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:46:in `render_with_wicked_pdf'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:30:in `render'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:46:in `call'
....

Resolved by cherrypicked: 2b19ea9 and 3893c07

@luisramos0
Copy link
Contributor

This is nice 👏
I wonder why so many changes in yarn.lock.

Could you share a screenshot of storybook running?

@jibees
Copy link
Contributor Author

jibees commented Mar 31, 2021

I wonder why so many changes in yarn.lock.

Storybook has a lot of dependencies, but yes, yarn.lock has large modification. Maybe it has also update some of our pre-existing dependencies?

Could you share a screenshot of storybook running?
Sure, very simple at this time:

Capture d’écran 2021-03-31 à 09 21 17

NB: the component itself is not displaying because of the monkeypatch conflict I guess.

@luisramos0
Copy link
Contributor

luisramos0 commented Mar 31, 2021 via email

@Matt-Yorkley
Copy link
Contributor

Matt-Yorkley commented Mar 31, 2021

Okay, option 1 is to use the ViewComponents config option render_monkey_patch_enabled = false, and then render components in the app using the #render_component method. This should work fine, but I think it won't work with StoryBook for now.

Option 2 might get a bit messy, we'll have to add an additional monkey-patch to wicked_pdf ourselves. We might already have a problem there, in that the current version of wicked_pdf uses #alias_method_chain, which is removed in Rails 5.2... there's definitely some complexity there. It might look like this.

@jibees
Copy link
Contributor Author

jibees commented Mar 31, 2021

Thanks a lot @Matt-Yorkley: I've cherrypicked your two commits, and now the error disappeared. 💪 🙏

Now I'm facing over CORS errors:

iframe.html?id=example-component--with-short-text&viewMode=story:1 Access to fetch at 'http://localhost:3000/rails/stories/example_component/with_short_text?title=OK' from origin 'http://localhost:61898' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Capture d’écran 2021-03-31 à 21 07 50

@jibees
Copy link
Contributor Author

jibees commented Mar 31, 2021

There you go!
Capture d’écran 2021-03-31 à 21 25 45

@Matt-Yorkley
Copy link
Contributor

Matt-Yorkley commented Mar 31, 2021

I've cherrypicked your two commits, and now the error disappeared

It's possible those two commits might break PDF creation elsewhere in the app, it was a quick attempt at a potential fix, but it would need careful testing...

@jibees
Copy link
Contributor Author

jibees commented Apr 1, 2021

I've cherrypicked your two commits, and now the error disappeared

It's possible those two commit might break PDFs elsewhere in the app, it was a quick fix, but it would need careful testing...

I already tried to make print to pdf functionality to work on my machine, but i always failed... ;(

@codecov
Copy link

codecov bot commented Apr 1, 2021

Codecov Report

Merging #7284 (9a670c7) into master (6dc2380) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7284   +/-   ##
=======================================
  Coverage   93.12%   93.12%           
=======================================
  Files         631      633    +2     
  Lines       18090    18096    +6     
=======================================
+ Hits        16846    16852    +6     
  Misses       1244     1244           
Impacted Files Coverage Δ
app/components/distributor_title_component.rb 100.00% <100.00%> (ø)
app/components/example_component.rb 100.00% <100.00%> (ø)
app/controllers/spree/admin/orders_controller.rb 83.33% <100.00%> (ø)
app/services/invoice_renderer.rb 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6dc2380...9a670c7. Read the comment docs.

@Matt-Yorkley
Copy link
Contributor

Hot tip: spec/services/invoice_renderer_spec.rb needs a little tweak on line 38, like this:

--  .to receive(:render_to_string)
++  .to receive(:render_to_string_with_wicked_pdf)

@jibees
Copy link
Contributor Author

jibees commented Apr 2, 2021

Hot tip: spec/services/invoice_renderer_spec.rb needs a little tweak on line 38, like this:

--  .to receive(:render_to_string)
++  .to receive(:render_to_string_with_wicked_pdf)

Thanks. I squashed your commit. 🙏

package.json Outdated Show resolved Hide resolved
Gemfile Outdated Show resolved Hide resolved
@jibees jibees force-pushed the styleguide branch 4 times, most recently from e696ce2 to 064bc70 Compare April 8, 2021 12:30
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.

Great work! I really appreciate how you take every challenge we through at you and learn along the way. You are making excellent progress.

I added some questions for my understanding. And do you think it would be possible to create a real example that we can use? Even if it's really small, like a text box somewhere.

Gemfile Show resolved Hide resolved
app/components/example_component.rb Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
config/application.rb Outdated Show resolved Hide resolved
config/initializers/wicked_pdf.rb Show resolved Hide resolved
app/controllers/stories_controller_decorator.rb Outdated Show resolved Hide resolved
@jibees jibees force-pushed the styleguide branch 2 times, most recently from 1c01eb4 to 025ac73 Compare April 9, 2021 09:10
@Matt-Yorkley
Copy link
Contributor

Matt-Yorkley commented Apr 22, 2021

Okay, app/controllers/spree/admin/orders_controller.rb line 92 needs this change:

-- render InvoiceRenderer.new.args(@order)
++ render_with_wicked_pdf InvoiceRenderer.new.args(@order)

and this also indicates we have inadequate test coverage on that specific bit of PDF invoice printing, right? We can make a separate issue to cover it 👍

Are there any cases where we send PDFs attached to emails?

Screenshot from 2021-04-22 11-57-09

The template for this email ☝️ basically just says "...please find attached invoice..."

Edit: yep, it's in the same controller (line 81) but it goes via InvoiceRenderer#render_to_string, which we've already updated 👍

@filipefurtad0
Copy link
Contributor

Thanks for feedback @Matt-Yorkley ! 👍
Created two tech-debt issues to cover:

@jibees
Copy link
Contributor Author

jibees commented Apr 22, 2021

@sauloperez @Matt-Yorkley
Draft: https://github.com/openfoodfoundation/openfoodnetwork/wiki/Creating-new-components Feel free to comment/edit/whatever.
Thanks a lot guys!

@jibees
Copy link
Contributor Author

jibees commented Apr 22, 2021

Thanks a lot for your tests, and for open issues.
I can do a test, but unfortunately, I have never been able to print to PDF on my machine. I still have a problem around wicked_pdf ... @Matt-Yorkley can you please take a look at these two problems?

@filipefurtad0 I've added dependencies for this PR : blocked by #7456 and #7455

@Matt-Yorkley
Copy link
Contributor

Matt-Yorkley commented Apr 22, 2021

can you please take a look at these two problems?

Yeah, I can take those two issues 👍

I have never been able to print to PDF on my machine.

Me neither, but I think we recently merged a change to master that means we can now run those PDF tests locally. I haven't re-checked it yet though.

As long as it passes manual testing here, I think we can cover those test coverage issues separately, without blocking this PR 👌

@jibees
Copy link
Contributor Author

jibees commented Apr 22, 2021

As long as it passes manual testing here, I think we can cover those test coverage issues separately, without blocking this PR 👌

Yes, you're right, I agree. I remove dependencies.

jibees and others added 12 commits April 22, 2021 15:08
 - Documentation : https://viewcomponent.org/
 - Our template engine generator is haml
 - With command: `rails generate component Example title --sidecar`
 - Delete generated spec file
 - Add `ViewComponent::TestHelpers` to get the helper method `render_inline`
 - Use Capybara to expect some assertion
 - Via `view_component_storybook` : https://github.com/jonspalmer/view_component_storybook
 - Configure both `view_component_storybook` and `storybook`
 - Add two addons: `@storybook/addon-docs` and `@storybook/addon-controls`
Update config comment for clarity
 - Thus, component can inherit from all CSS rules, rendering is as close as to the truth
 - Still need to import `admin` css file if we want to create components for admin part (and we probably want too). This will probably lead to overlapping problems between all CSS rules...
- This is a bit hacky, but as our CSS files are packed by rails I didn't find any better solution. We could also use any sass loader, but with this solution we can be confident on the fact that the generated CSS is the one used by the server that finally displays the component
 - Storybook need to access through a GET method on `/rails/stories/**` served by `ViewComponent::Storybook::StoriesController`
 - Configure exact policy with an initialized filter
 - Once story has been created, still need to generate the story itself (in json file format). Help with command: `rake view_component_storybook:write_stories_json`
 - Simple wrapper around `<h3 />` for distributor title, used in the shop front
and call WickedPdf::PdfHelper explicitly
@jibees
Copy link
Contributor Author

jibees commented Apr 22, 2021

@Matt-Yorkley
Modification done: 9a670c7 Thanks a lot for this one! 🙏
Moving back to Code Review

@filipefurtad0 filipefurtad0 added the pr-staged-au staging.openfoodnetwork.org.au label Apr 22, 2021
@filipefurtad0
Copy link
Contributor

Alright!

After your changes, clicking "Print Invoice" on the Actions menu or visiting /admin/orders/<order_nr>/print now prints the respective pdf invoice, for both invoice types.

Can't think of anywhere else where pdfs are created, I think we're good to go now.

Thanks for the quick fix 🚀

@filipefurtad0 filipefurtad0 removed the pr-staged-au staging.openfoodnetwork.org.au label Apr 22, 2021
@Matt-Yorkley
Copy link
Contributor

Alright, lets slap the button.

Thanks @jibees, sorry it was such a convoluted journey! 😅

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.

None yet

6 participants