Skip to content

Commit c770eb4

Browse files
authored
chore(doc): improve documentation to understand how to use with dynamic values (#175)
1 parent 778f950 commit c770eb4

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

documentation.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,80 @@ If the specific locale for the country you target is available, we suggest to us
119119
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.
120120

121121
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.
128+
129+
#### Parameters:
130+
131+
| Parameter | Type | Required | Description |
132+
|:-------------------|:--------------------------------:|:-----------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
133+
| 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_`) |
136+
137+
138+
#### Returns:
139+
An object with the following method:
140+
- `add(widget: WidgetNames, options: WidgetOptions): AddReturnType`
141+
- 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
177+
// Initialize the Alma widgets Controller
178+
const widgets = Alma.Widgets.initialize('11gKoO333vEXacMNMUMUSc4c4g68g2Les4', Alma.ApiMode.TEST)
179+
180+
function updateWidget() {
181+
const quantity = parseInt(document.getElementById('quantity')?.value || '1', 10);
182+
currentPurchaseAmount = 450 * 100 * quantity;
183+
184+
widgets.add(Alma.Widgets.PaymentPlans, {
185+
container: '#alma-widget-payment-plans',
186+
locale: 'fr',
187+
purchaseAmount: currentPurchaseAmount,
188+
});
189+
}
190+
191+
// Initial render
192+
updateWidget();
193+
194+
// Add event listener to update the widget dynamically
195+
document.getElementById('quantity')?.addEventListener('change', updateWidget);
196+
```
197+
198+
By following this approach, you can dynamically update the widget whenever the `purchaseAmount` changes.

0 commit comments

Comments
 (0)