import React, { useState, useEffect, useContext } from "react";
import Paper from '@mui/material/Paper';
import './Location.css';
import { GlobalContext } from "./GlobalContext.js";
import { base } from "./Common.js";
/*
Creating a location:
* Go to Google Maps and find the location you want, selecting the
destination on the map. This updates the URL to include the longitude
and latitude.
* Copy the full URL (CTRL-L, CTRL-A, CTRL-C)
* Paste the URL here: ...
* Backend scrapes Lat and Lon from URL and uses that with the name of
the location to create a marker on the static map view
*/
const CreateLocation = () => {
return
;
}
function Location(props) {
const propLocation = props.location;
const { csrfToken, googleApiKey, setError } = useContext(GlobalContext);
const [ location, setLocation ] = useState(
typeof propLocation === 'object' ? propLocation : undefined);
const locationId =
typeof propLocation === 'number' ? propLocation : undefined;
useEffect(() => {
if (!csrfToken || location || !locationId) {
return;
}
const effect = async () => {
const res = await window.fetch(
`${base}/api/v1/locations/${locationId}`, {
method: 'GET',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'CSRF-Token': csrfToken
}
});
const data = await res.json();
if (res.status >= 400) {
setError(data.message ? data.message : res.statusText);
return;
}
if (!data) {
return;
}
setLocation(data);
}
effect();
}, [locationId, csrfToken, location, setError]);
const showLocation = (location) => {
const fields = Object.getOwnPropertyNames(location)
.filter(field => location[field])
.map(field => {
return
{field}
{ typeof location[field] === 'string'
&& location[field].match(/^http.*/)
&&
{field.toUpperCase()} }
{ typeof location[field] === 'string'
&& !location[field].match(/^http.*/)
&& location[field] }
{ typeof location[field] === 'number' && location[field]}
} );
return
{fields}
{ googleApiKey &&
}
;
};
return (
{ showLocation(location) }
);
}
export { Location };