logo dile-components dile-components

dile-ajax-switch

The dile-ajax-switch component is a tool to set boolean properties in a resource.

Installation

npm i @dile/crud

Usage

Import the dile-ajax-switch component.

import '@dile/crud/components/ajax-switch/ajax-switch.js';

Use the component.

<dile-ajax-switch 
  endpoint="https://timer.escuelait.com/api/board-games/1/change-essential"
  method="patch"
  checkedLabel="Essential" 
  uncheckedLabel="Not essential"
></dile-ajax-switch>

This component is implemented with the dile-ajax component, so refer to the dile-ajax documentation for more information on configuration requirements.

Properties

Custom Events

The component dispatches the following custom events:

You can also listen to the custom events from dile-switch events.

Example listening to success/error events:

const switchElement = document.querySelector('dile-ajax-switch');

switchElement.addEventListener('dile-ajax-switch-success', (e) => {
  console.log('Switch updated successfully', e.detail);
});

switchElement.addEventListener('dile-ajax-switch-error', (e) => {
  console.log('Error updating switch', e.detail);
});

dile-ajax-switch example

<script type="module">
  import '@dile/crud/components/ajax-switch/ajax-switch.js';
</script>
<dile-ajax-switch 
  endpoint="https://timer.escuelait.com/api/board-games/1/change-essential"
  method="patch"
  checkedLabel="Essential" 
  uncheckedLabel="Not essential"
></dile-ajax-switch>