1
0

Map locations

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-04-09 16:40:25 -07:00
parent cf83d2b95b
commit 0daf3c72b6
7 changed files with 95 additions and 39 deletions

View File

@ -2,4 +2,3 @@ PORT=3001
PUBLIC_URL=/ PUBLIC_URL=/
HOST=nuc.ketrenos.com HOST=nuc.ketrenos.com
DANGEROUSLY_DISABLE_HOST_CHECK='true' DANGEROUSLY_DISABLE_HOST_CHECK='true'
LOG_LINE=1

View File

@ -1,5 +1,6 @@
const base = process.env.PUBLIC_URL; const base = process.env.PUBLIC_URL;
export { export {
base base
}; };

View File

@ -81,15 +81,13 @@ function Group() {
display: 'flex', display: 'flex',
flexDirection: 'column' flexDirection: 'column'
}}> }}>
<GlobalContext.Provider value={{user, setUser}}> { error && <div>{error}</div>}
{ error && <div>{error}</div>} { !error && <>
{ !error && <> <div>Group: {groupId}</div>
<div>Group: {groupId}</div> <div>Locations</div>
<div>Locations</div> { locations.map(location =>
{ locations.map(location => <Location location={location} key={location.id}/>) }
<Location location={location} key={location.id}/>) } </> }
</> }
</GlobalContext.Provider>
</Paper> </Paper>
); );
} }

View File

@ -12,14 +12,43 @@ import { base } from "./Common.js";
function Location(props) { function Location(props) {
const propLocation = props.location; const propLocation = props.location;
const { csrfToken } = useContext(GlobalContext); const { csrfToken } = useContext(GlobalContext);
const [googleApiKey, setGoogleApiKey] = useState(undefined);
const [ location, setLocation ] = useState( const [ location, setLocation ] = useState(
typeof propLocation === 'object' ? propLocation : undefined); typeof propLocation === 'object' ? propLocation : undefined);
const [ error, setError ] = useState(null);
const locationId = const locationId =
typeof propLocation === 'number' ? propLocation : undefined; typeof propLocation === 'number' ? propLocation : undefined;
const [ error, setError ] = useState(null);
useEffect(() => { useEffect(() => {
if (!csrfToken && (location || !locationId)) { if (!csrfToken || googleApiKey) {
return;
}
const effect = async () => {
const res = await window.fetch(
`${base}/api/v1/locations/google-api-key`, {
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;
}
setGoogleApiKey(data.key);
}
effect();
}, [csrfToken, googleApiKey, setGoogleApiKey]);
useEffect(() => {
if (!csrfToken || location || !locationId) {
return; return;
} }
@ -49,33 +78,56 @@ function Location(props) {
const createLocation = (location) => { const createLocation = (location) => {
const fields = Object.getOwnPropertyNames(location) const fields = Object.getOwnPropertyNames(location)
.map(field => <div key={field} .filter(field => location[field])
style={{ .map(field => {
display: 'flex', return <div key={field}
flexDirection: 'row', style={{
alignItems: 'flex-start', display: 'flex',
width: '100%' flexDirection: 'row',
}}> alignItems: 'flex-start',
<div style={{ width: '100%'
display: 'flex', }}>
alignSelf: 'flex-start', <div style={{
fontWeight: 'bold', display: 'flex',
minWidth: '8rem' alignSelf: 'flex-start',
}}>{field}</div> fontWeight: 'bold',
<div style={{ minWidth: '8rem'
display: 'flex', }}>{field}</div>
alignSelf: 'flex-start', <div style={{
textAlign: 'left' display: 'flex',
}}> alignSelf: 'flex-start',
{location[field]} textAlign: 'left'
}}>
{ typeof location[field] === 'string'
&& location[field].match(/^http.*/)
&& <a href={location[field]}>{field.toUpperCase()}</a> }
{ typeof location[field] === 'string'
&& !location[field].match(/^http.*/)
&& location[field] }
</div>
</div> </div>
</div> } );
);
return <div key={location.id} style={{ return <div key={location.id} style={{
display: 'flex', display: 'flex',
flexDirection: 'column', flexDirection: 'row',
alignItems: 'center' alignItems: 'center',
}}>{fields}</div>; justifyContent: 'space-between',
}}>
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center'
}}>{fields}</div>
{ googleApiKey && <iframe
title={location.id}
width="256"
height="128"
style={{ border: "0" }}
loading="lazy"
referrerPolicy="no-referrer-when-downgrade"
src={`https://www.google.com/maps/embed/v1/place?key=${googleApiKey}&q=${encodeURIComponent(location.name.replace(/ +/g, '+'))},Hillsboro+OR`}
/> }
</div>;
}; };
return ( return (

View File

@ -25,7 +25,7 @@ require('./console-line.js'); /* Monkey-patch console.log with line numbers */
* set in the headers */ * set in the headers */
app.set('trust proxy', true); app.set('trust proxy', true);
app.use(session({ app.use(session({
secret: 'm@g1x!c00k13$', secret: config.get('sessions.store-secret'),
maxAge: 30 * 24 * 60 * 60 * 1000, /* 1 month */ maxAge: 30 * 24 * 60 * 60 * 1000, /* 1 month */
resave: false, resave: false,
saveUninitialized: true, saveUninitialized: true,

View File

@ -15,5 +15,6 @@
"sessions": { "sessions": {
"db": "../db/sessions.db", "db": "../db/sessions.db",
"store-secret": "m@g1kc00ki3z!" "store-secret": "m@g1kc00ki3z!"
} },
"googleApi": "AIzaSyBiBThAIMggko6Gjdxti3WKm4JnSxJrZLE"
} }

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const config = require('config');
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
@ -12,6 +13,10 @@ router.put('/', (req, res) => {
res.status(200).send([ location ]); res.status(200).send([ location ]);
}); });
router.get('/google-api-key', (req, res) => {
return res.status(200).send({ key: config.get('googleApi') });
});
router.get('/:locationId?', (req, res) => { router.get('/:locationId?', (req, res) => {
const { locationId } = req.params; const { locationId } = req.params;
if (locationId) { if (locationId) {