All files / src/components ride.component.js

100% Statements 58/58
100% Branches 12/12
100% Functions 12/12
100% Lines 56/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                            6x     10x     1x 1x       6x 1x               3x   3x 3x 3x         3x     6x 1x       6x 3x 3x 3x 2x 2x   2x 1x 1x 1x     2x 2x       6x 6x 6x 6x 1x   6x 6x   6x 6x 6x 6x         2x       13x 13x 6x 6x 6x 6x   6x 4x 4x   4x     6x 6x   6x   6x 6x   6x   6x 6x             1x  
import { router } from '../router.js';
import { orderService } from '../services/order.service.js';
import { rideService } from '../services/ride.service.js';
import {
  heightTemplate,
  minimalRideHeightTemplate,
  templateRidePage,
} from './ride.template.js';
import { RoboComponent, Selector, cloneTemplate } from './robo.component.js';
 
export class RideComponent extends RoboComponent {
  /** @type {Ride} */
  ride;
 
  #numPeople = 1;
 
  get numPeople() {
    return this.#numPeople;
  }
  set numPeople(value) {
    this.#numPeople = value;
    this.#render();
  }
 
  /** @param {Event} event */
  handleRideSelect = (event) => {
    this.numPeople = parseInt(
      /** @type {HTMLInputElement} */ (event.target).value,
    );
  };
 
  /** @return {Person[]} */
  getPeople() {
    /** @type {Person[]} */
    const people = [];
 
    const heightInputs = [...this.by.id.heightInputs.querySelectorAll('input')];
    for (let i = 0; i < this.numPeople; i++) {
      people.push({
        index: i + 1,
        height: heightInputs[i]?.valueAsNumber ?? 0,
      });
    }
    return people;
  }
 
  cancel = () => {
    router.navigate([]);
  };
 
  /** @param {Event} event */
  submit = (event) => {
    event.preventDefault();
    const people = this.getPeople();
    if (this.ride.minHeight) {
      const invalidHeights = people.filter(
        ({ height }) => !this.hasValidHeight(height),
      );
      if (invalidHeights.length) {
        this.by.id.alert.style.display = 'block';
        this.by.id.alertText.innerHTML = `${invalidHeights.map(({ index }) => `Person ${index} is too short for this ride`).join('<br />')}`;
        return;
      }
    }
    orderService.currentOrder = { ride: this.ride, people };
    router.navigate(['success']);
  };
 
  connectedCallback() {
    this.appendChild(cloneTemplate(templateRidePage));
    const rideId = this.getAttribute('ride-id');
    rideService.getRide(rideId).then((ride) => {
      if (!ride) {
        router.navigate([]);
      }
      this.ride = ride;
      this.#render();
    });
    this.by.id.peopleSelect.addEventListener('input', this.handleRideSelect);
    this.by.id.previousBtn.addEventListener('click', this.cancel);
    this.by.id.form.addEventListener('submit', this.submit);
    this.#render();
  }
 
  /** @param {number} height */
  hasValidHeight(height) {
    return this.ride.minHeight < height;
  }
 
  #render() {
    this.by.id.alert.style.display = 'none';
    if (this.ride) {
      this.by.id.title.innerText = this.ride.name;
      const img = /** @type {HTMLImageElement}*/ (this.by.id.rideImage);
      img.src = this.ride.image;
      img.alt = this.ride.name;
 
      if (this.ride.minHeight) {
        const p = cloneTemplate(minimalRideHeightTemplate);
        new Selector(p).class.minHeight.textContent =
          this.ride.minHeight.toString();
        this.by.id.heightInputs.replaceChildren(
          p,
          ...new Array(this.numPeople).fill('').map((_, index) => {
            const element = cloneTemplate(heightTemplate);
            const row = new Selector(element);
            const label = /** @type {HTMLLabelElement} */ (
              row.class.colFormLabel
            );
            label.innerText = `Height for person ${index + 1}`;
            label.htmlFor = `heightInput-${index}`;
            const input = /** @type {HTMLInputElement} */ (
              row.class.formControl
            );
            input.id = `heightInput-${index}`;
            return element;
          }),
        );
      }
    }
  }
}
customElements.define('robo-ride', RideComponent);