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

implement notification action #36

Merged
merged 8 commits into from
Feb 11, 2023
Merged

Conversation

lxxxvi
Copy link
Contributor

@lxxxvi lxxxvi commented Feb 4, 2023

Hi 👋

This is an idea resp. attempt to implement #4

# initial signature from the issue:
turbo_stream.notification(title, options, **attributes)

However... instead of having one options argument, I took the approach of making the options available individually... I am unsure how useful/usless this is. What do you think?

Also, there's one skipped test at the moment.
If we use the actions and/or silent option, the browser log mentions this:

 🚧 Browser logs:
      TypeError: Failed to construct 'Notification': Actions are only supported for persistent notifications shown using ServiceWorkerRegistration.showNotification().
        at Function.invoke (node_modules/sinon/pkg/sinon-esm.js:2386:27)
        at new Notification (node_modules/sinon/pkg/sinon-esm.js:2705:26)
        at StreamElement.rt (dist/index.js:1:12153)
        at Object.renderElement [as render] (node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js:3656:26)
        at node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js:3675:36
        at async StreamElement.connectedCallback (node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js:3660:13)

Do you have an idea how to resolve this, if at all?

Copy link
Owner

@marcoroth marcoroth left a comment

Choose a reason for hiding this comment

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

Thank you @lxxxvi for taking this one on!

However... instead of having one options argument, I took the approach of making the options available individually... I am unsure how useful/usless this is. What do you think?

Technically there are two "signatures", the one shown in the initial issue is the one which could be used from the Ruby side when calling the notification action on turbo_stream. However this doesn't mean that the options also need to be in an options attribute on the <turbo-stream> element itself.

I think we should keep the signature on the Ruby side as close as possible to the regular Notification API, as shown here: https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#syntax

Meaning, that if you had a call like:

turbo_stream.notification("My Notification", { lang: "en", body: "The body", icon: "http://url.to.icon" })

it could still get translated into this, using individual attributes for each option:

<turbo-stream title="My Notification" lang="en" body="The Body" icon="http://url.to.icon"></turbo-stream>

But since that is the "responsibility" of the turbo_power-rails repo, we can ignore that for now.

TL;DR: I think it's fine to split up the attributes on the <turbo-stream> element like you did 👍🏼


Do you have an idea how to resolve this, if at all?

I would assume that if you use a fake instead of a spy for the test that it could resolve this. Technically the action doesn't need to test if the action actually can instantiate a Notification which it does with the spy. It should be sufficient if we can ensure that the action translates the options in the right new Notification(...) call.

@marcoroth
Copy link
Owner

marcoroth commented Feb 8, 2023

I also just realized, we might need to call Notification.requestPermission() first somewhere, otherwise it might fail.

@lxxxvi
Copy link
Contributor Author

lxxxvi commented Feb 8, 2023

@marcoroth

Thank you 👍

I think I was able to resolve the ServiceWorkerRegistration issue by using stub instead of spy.

@lxxxvi
Copy link
Contributor Author

lxxxvi commented Feb 8, 2023

I also just realized, we might need to call Notification.requestPermission() first somewhere, otherwise it might fail.

Oh... where should we do that? In the notification function itself?

@lxxxvi
Copy link
Contributor Author

lxxxvi commented Feb 10, 2023

@marcoroth Thanks a lot for your help today. 🙌

I have refactored the code as discussed. Feel free to review it and let me know if I can improve anything.

Copy link
Owner

@marcoroth marcoroth left a comment

Choose a reason for hiding this comment

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

Thank you @lxxxvi! I think this is pretty good now. I really like how the tests turned out, they are much more readable and easier to understand than what we had before!

I've got one small remark, but other than that this looks really good to me!

src/actions/notification.ts Outdated Show resolved Hide resolved
Co-authored-by: Marco Roth <marco.roth@intergga.ch>
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.

2 participants