You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation.md
+77Lines changed: 77 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,3 +119,80 @@ If the specific locale for the country you target is available, we suggest to us
119
119
The locale is used in the Widgets to display all wordings in the correct language but also to format numbers, dates, currencies into the standard format.
120
120
121
121
For example, for a pricing, the locale `pt` will format as `€ 100,00` while the locale `pt-PT` will format as `100,00 €`.
122
+
123
+
# To go further
124
+
125
+
### WidgetsController
126
+
127
+
The `WidgetsController` function is responsible for managing Alma widgets. It provides a method to dynamically add widgets to the DOM based on the provided configuration.
| apiData |`ApiData`|**required**| An object containing the following properties : domain, merchantId |
134
+
| apiData.domain |`ApiMode.TEST` OR `ApiMode.LIVE`|**required**| The domains LIVE & TEST are preconfigured when using the provided values from "Alma.ApiMode.[TEST or LIVE]. It represents the domain's url to be used for API calls (ex: "https://api.getalma.eu" for LIVE) |
135
+
| apiData.merchantId |`string`|**required**| The merchant's unique identifier provided by Alma (starting with `merchant_`) |
- Adds a widget to the DOM based on the specified widget type and options.
142
+
143
+
The `WidgetsController` is configured when implementing the method `initialize(merchantId: string, domain: Alma.ApiMode.TEST | Alma.ApiMode.LIVE)`
144
+
145
+
### Known Limitation:
146
+
The current implementation of the `addWidget` function does not automatically update the widget when the `purchaseAmount` changes dynamically.
147
+
Once a widget is rendered, any subsequent changes to the `purchaseAmount` will not be reflected unless the widget is manually re-rendered.
148
+
This is also the case for any other parameters passed to the widget.
149
+
The following approach can be used to handle other variables changes in the widget.
150
+
151
+
### How to Implement the Widget for Variable `purchaseAmount`:
152
+
To handle a variable `purchaseAmount`, you can follow these steps:
153
+
154
+
1.**Track Changes to `purchaseAmount`**
155
+
156
+
Use an event listener or a state management solution (e.g., React state or a global variable) to detect changes to the `purchaseAmount`.
157
+
158
+
For example `var purchaseAmount = document.getElementById('quantity').value`
159
+
160
+
2.**Re-render the Widget**
161
+
162
+
Call the `add` method again with the updated `purchaseAmount` to re-render the widget.
163
+
164
+
Note that Calling the `add` method multiple times will **unmount the previous widget** before rendering a new one. This behavior is implemented in the `addWidget` function (lines 37–41). Specifically:
165
+
- It checks if a `Root` instance already exists for the specified container (`rootMap.get(containerDiv)`).
166
+
- If a `Root` exists, it calls `unmount()` to remove the previous widget and deletes the reference from the `rootMap`.
167
+
- A new widget is then rendered in the same container.
168
+
169
+
This ensures that only one widget is active in the specified container at any given time
170
+
171
+
3.**Example Implementation**
172
+
173
+
Below is an example of how to implement a widget with a variable `purchaseAmount`:
174
+
175
+
```typescript
176
+
let currentPurchaseAmount =450*100; // Initial purchase amount in cents
0 commit comments