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

feat: widget events for send to wallet #309

Merged
merged 11 commits into from
Oct 17, 2024
Merged

Conversation

DNR500
Copy link
Contributor

@DNR500 DNR500 commented Oct 8, 2024

Jira: LF-8931

This PR adds a "FormFieldChanged" to the widget. This event will fire when..

  • The user updates the any form values via the Widget UI - the event will only be emitted after the Widget has done its validation
  • A form value is updated via a external method to the Widget UI - either via config, url search params or formRef. These do not undergo the same validation as user entered values via the UI. Strict validation of values added to things like the config should be done before adding them to the config itself.

Note: that with this implementation the initialise value from the widget config will not emit an event.

To catch the event specifically for the send to wallet address change you will need to check the fieldName value in on the payload

widgetEvents.on(WidgetEvent.FormFieldChanged, (payload) => {
   if(payload.fieldName === "toAddress") {
      // sent to wallet address was changed
   }
)

The event payload will looks like this

{
  fieldName: "toAddress",
  oldValue: "0x8131aa556b78e6D25F53DA0F01c2f046AA1012d1",
  value: "0x29DaCdF7cCaDf4eE67c923b4C22255A4B2494eD7"
}

Widget Events Tooling

I've also added some basic tooling to the the playground to assist with testing and working with events. This should help with the other events we need to add to widget as well. This sits behind the dev view toogle.

To view the tool

  • Open Playground, go to the Playground settings and turn on the Dev view option
  • The Widget events control should be visible in the playground
  • You can toggle on and off an event listener per event
  • Or you can turn on all events and have the listener available on page load
  • Event names with there payloads are printed in the console

Widget Events tools

@DNR500 DNR500 added the testing label Oct 8, 2024
Copy link

github-actions bot commented Oct 8, 2024

Hey! This is your new endopint: https://10069250.widget-widgeteven.pages.dev

Copy link

github-actions bot commented Oct 8, 2024

Hey! This is your new endopint: https://04c1b7ee.widget-widgeteven.pages.dev

Copy link

github-actions bot commented Oct 8, 2024

Hey! This is your new endopint: https://733c2353.widget-widgeteven.pages.dev

packages/widget/src/stores/form/FormStore.tsx Outdated Show resolved Hide resolved
}

const fieldValueToEmittedEvents: FieldValueToEmittedEvents = {
toAddress: (address, emitter) =>
Copy link
Member

@chybisov chybisov Oct 9, 2024

Choose a reason for hiding this comment

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

Let's add all fields here to avoid adding them in the future anyway. We should also think about having one event for all of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lets go with one event for them all. It's simpler that 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.

Just gonna add in better type inference too

@@ -19,6 +19,7 @@ export enum WidgetEvent {
WalletConnected = 'walletConnected',
WidgetExpanded = 'widgetExpanded',
PageEntered = 'pageEntered',
SendToWalletAddressChanged = 'sendToWalletAddressChanged',
Copy link
Member

@chybisov chybisov Oct 9, 2024

Choose a reason for hiding this comment

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

If we add all fields, how about we come up with one event that handles all the fields with fieldName, oldValue and newValue.

Maybe name it something like

  • FormFieldChanged
  • FormValueChanged

Basically, the idea is to subscribe to the form changes, rather than individual events for every field. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, when doing this I did wonder it we would end you wanting to do it for all values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

have gone with this approach using FormFieldChanged, I'm gonna look at better type inference as well. Also I think I used value rather than newValue so will change that

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've improved the types now as well

Comment on lines 65 to 72
emitEventForFieldValueChange(
fieldName,
value,
actions.getFieldValues(fieldName)[0],
emitter,
);

actions.setFieldValue(fieldName, value, options);
Copy link
Member

Choose a reason for hiding this comment

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

Should we set value first and, after that fire an event?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The actions.getFieldValues will not have the old value to do the comparison to check for change if you call actions.setFieldValue first. I tried it the other way around initially

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have changed this

Comment on lines 79 to 88
(Object.keys(formValues) as FormFieldNames[]).forEach((fieldName) => {
emitEventForFieldValueChange(
fieldName,
formValues[fieldName],
actions.getFieldValues(fieldName)[0],
emitter,
);
});

actions.setUserAndDefaultValues(formValues);
Copy link
Member

Choose a reason for hiding this comment

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

Should we set value first and, after that fire an event?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The actions.getFieldValues will not have the old value to do the comparison to check for change if you call actions.setFieldValue first. I tried it the other way around initially

Copy link
Member

Choose a reason for hiding this comment

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

I guess we can save it to a variable like

const oldValue = actions.getFieldValues(fieldName)[0]

and then execute after.

Copy link
Contributor Author

@DNR500 DNR500 Oct 11, 2024

Choose a reason for hiding this comment

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

That would actually become a bit more complicated in the example above for the setUserAndDefaultValuesWithEmittedEvents - you would have to store the oldValues in another object map first and then once setUserAndDefaultValues is executed you would have to the iterate over the old values map.

I don't see what the benefit will be of changing the order of execution there and I think it will make the code a little less cleaner - but I do it and can decide if you are happier with it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have put something in place for this

Copy link

Hey! This is your new endopint: https://1ab1589f.widget-widgeteven.pages.dev

Copy link

Hey! This is your new endopint: https://530a5df8.widget-widgeteven.pages.dev

Copy link

Hey! This is your new endopint: https://11cad9c0.widget-widgeteven.pages.dev

@chybisov chybisov merged commit b247afb into main Oct 17, 2024
1 check passed
@chybisov chybisov deleted the widget-event-for-send-to-wallet branch October 17, 2024 12:51
Copy link

Hey! This is your new endopint: https://24af08e5.widget-widgeteven.pages.dev

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

Successfully merging this pull request may close these issues.

2 participants