Source: modules/fetchPins.js

import makePin from "./makePin.js";
import toLatLon from "./toLatLon.js";

/**
 * @module fetchPins
 */

/** Fetches pin records from database and adds pin features to pin vector source layer
 * @async
 * @param {ol.source.Vector} pinSource - The relevant vector source layer to add pin features to
 */
const fetchPins = async (pinSource) => {
  const res = await fetch(import.meta.env.VITE_BACKEND_URL);
  const pinRecords = await res.json();

  pinRecords.forEach((pinRecord) => {
    const [latitude, longitude] = toLatLon(
      pinRecord.easting,
      pinRecord.northing,
      pinRecord.zone,
      pinRecord.zoneLetter
    );

    const pin = makePin(
      longitude.toFixed(5),
      latitude.toFixed(5),
      pinRecord.name,
      pinRecord.cheapestItem,
      pinRecord.cheapestFloz
    );

    pinSource.addFeature(pin);
  });
};

export default fetchPins;