I have a toggle switch that makes an http PATCH request when it is triggered. Below you can see how it looks like:
The template:
<div id="toggle-status-btn-container">
<div class="slider" [ngClass]="{'to-right': !shopIsOpen}"></div>
<div class="label-container">
<label class="store-opened" (click)="updateShopSlotsStatus(true)" [ngClass]="{'toggled-opened-text': !shopIsOpen}">OPENED</label>
</div>
<div class="label-container">
<label class="store-closed" (click)="updateShopSlotsStatus(false)" [ngClass]="{'toggled-closed-text': !shopIsOpen}">CLOSED</label>
</div>
</div>
It works great, but as of right now the user can spam this toggle switch, triggering multiple http requests in a row.
What can I do to prevent this behaviour? I don't want to trigger an http request if the user is spamming this toggle switch. Maybe there's a way to only trigger the last one? Since I'm very new to RxJs I don't really know how to solve this.
Here is the method that is called by the click event, in the component .ts:
updateShopSlotsStatus(isOpen: boolean){
this.shopIsOpen = isOpen;
this.apiService.updateShopSlotsStatus(isOpen, this.weekDay).subscribe();
}
And the http request, in a service file:
updateShopSlotsStatus(isOpen: boolean, weekDay: string){
const endpoint = this.url + "shipping/" + this.webzineId + "/delivery_slots" + "/";
return this.http.patch(endpoint, {"slots": { [weekDay] : { "enabled": isOpen }}});
}
Thanks in advance.