logo polydile logo polydile en modo oscuro dile-components

dile-crud-detail

The dile-crud-detail component is used to display the details of a specific item from a resource in a REST API.

It is a simple component that should be populated using a template, which defines how the resource element details will be displayed.

Installation

npm i @dile/crud

Usage

Import the dile-crud-detail component.

import '@dile/crud/components/detail/crud-detail.js';

Use the component.

<dile-crud-detail
  endpoint="https://timer.escuelait.com/api/countries/7"
  .itemDetailTemplate=${(country) => html`<demo-country-detail .country="${country}"></demo-country-detail>`}
></dile-crud-detail>

Properties

Methods

Events

Configuration

For this component to function properly, it is necessary to apply the configurations mentioned on the CRUD system documentation page.

Examples

Detail template component

Technically, it's not necessary to build a component to define the details of the item you want to display, as a simple template would be enough. However, to keep things organized and reusable, you would typically use a component dedicated to displaying the details, which also allows you to include additional functionality if needed.

<script type="module">
import { LitElement, html, css } from 'lit';

export class DemoCountryDetail extends LitElement {
  static styles = [
    css`
      :host {
        display: block;
        color: #303030;
      }
      p {
        margin: 0 0 1rem;
      }
      .last {
        margin-bottom: 0;
      }
    `
  ];

  static get properties() {
    return {
      country: { type: Object }
    };
  }

  render() {
    return html`
      <p>Name: ${this.country.name}</p>
      <p class="last">Continent: ${this.country.continent}</p>
    `;
  }
}
customElements.define('demo-country-detail', DemoCountryDetail);
</script>

Crud Detail

In this example, you can enter any identifier in the text field. When you click the "Show" button, the country with that ID will be loaded and displayed in the dile-crud-detail component.

<script type="module">
import '@dile/crud/components/detail/crud-detail.js';
import { LitElement, html, css } from 'lit';

class CrudCountryDetailDemo extends LitElement {
    static styles = [
      css`
        :host {
          display: block;
        }
        dile-crud-detail {
          margin-top: 1rem;
        }
        .hide {
          display: none;
        }
        .message {
          margin-top: 1rem;
          font-size: 0.875rem;  
        }
      `
    ];

    static get properties() {
      return {
        message: { type: String },
        error: { type: Boolean },
      };
    }

    constructor() {
      super();
      this.resource = 'https://timer.escuelait.com/api/countries/';
    }


    render() {
      return html`
        <dile-input label="Country Id" name="countryId" value="7"></dile-input>
        <dile-button @click=${this.showCountry}>Show</dile-button>
        <dile-crud-detail
          class="${this.error ? 'hide' : ''}"
          endpoint="https://timer.escuelait.com/api/countries/7"
          .itemDetailTemplate=${(country) => html`<demo-country-detail .country="${country}"></demo-country-detail>`}
          @crud-item-detail-loaded=${this.detailLoaded}
          @crud-item-detail-load-error=${this.detailLoadError}
        ></dile-crud-detail>
        <p class="message">${this.message}</p>
      `;
    }

    showCountry() {
      const elementId = this.shadowRoot.querySelector('dile-input').value;
      const detailElement = this.shadowRoot.querySelector('dile-crud-detail');
      detailElement.endpoint = this.resource + elementId;

    }

    detailLoaded() {
      this.error = false;
      this.message = 'Element loaded';
    }

    detailLoadError() {
      this.error = true;
      this.message = 'This country does not exists';
    }
  }
  customElements.define('crud-country-detail-demo', CrudCountryDetailDemo);

</script>
<crud-country-detail-demo></crud-country-detail-demo>