Event-Planner / client / src / components / venue-search / favorite-venues / table-body.js
table-body.js
Raw

function calcCrow(lat1, lon1, lat2, lon2) 
    {
      var R = 6371; // km
      var dLat = toRad(lat2-lat1);
      var dLon = toRad(lon2-lon1);
      var lat1 = toRad(lat1);
      var lat2 = toRad(lat2);

      var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c;
      d = d / 1.609;
      return d.toFixed(1);
    }

// Converts numeric degrees to radians
function toRad(Value) 
    {
        return Value * Math.PI / 180;
    }

const TableBody = ({ tableData, columns }) => {

    return (
     <tbody>
      {tableData.map((data) => {
        const curr_lat = data.lat;
        const curr_long = data.long;
       return (
        <tr key={data.id}>
         {columns.map(({ accessor }) => {
          var tData = data[accessor] ? data[accessor] : "——";
          if (accessor === "price"){
            if (Number(tData) === 1){
                tData = "$"
              } else if (Number(tData) === 2) {
                tData = "$$"
              } else {
                tData = "$$$"
              }
          }
          if (accessor === "distance") {
            var user_lat = window.localStorage.getItem("user_lat");
            var user_long = window.localStorage.getItem("user_long");
            tData = calcCrow(user_lat, user_long, curr_lat, curr_long).toString();
            var miles = " miles";
            tData = tData.concat(miles);
          }
          return <td key={accessor}>{tData}</td>;
        })}
        </tr>
       );
      })}
     </tbody>
    );
   };
   
   export default TableBody;