From 1b55caa0a2fdcd7dd8aca38ab5e1a923622fc07b Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Sun, 27 Sep 2020 18:18:30 +0200 Subject: [PATCH] Disable places page when mapbox token isn't defined --- docker-compose.example.yml | 5 ++ ui/src/Layout.js | 108 ++++++++++++++------------ ui/src/Pages/PlacesPage/PlacesPage.js | 18 ++++- 3 files changed, 81 insertions(+), 50 deletions(-) diff --git a/docker-compose.example.yml b/docker-compose.example.yml index 51d5443d..e18046ef 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -31,6 +31,11 @@ services: # change this value to http://example.com/ - PUBLIC_ENDPOINT=http://localhost:8000/ + # Optional: To enable map related features, you need to create a mapbox token. + # A token can be generated for free here https://account.mapbox.com/access-tokens/ + # It's a good idea to limit the scope of the token to your own domain, to prevent others from using it. + # - MAPBOX_TOKEN= + volumes: - api_cache:/app/cache diff --git a/ui/src/Layout.js b/ui/src/Layout.js index e0ae45f3..bff6587e 100644 --- a/ui/src/Layout.js +++ b/ui/src/Layout.js @@ -4,13 +4,13 @@ import styled from 'styled-components' import { NavLink } from 'react-router-dom' import { Icon } from 'semantic-ui-react' import Sidebar from './components/sidebar/Sidebar' -import { Query } from 'react-apollo' +import { useQuery } from 'react-apollo' import gql from 'graphql-tag' import { Authorized } from './AuthorizedRoute' import { Helmet } from 'react-helmet' import Header from './components/header/Header' -const adminQuery = gql` +const ADMIN_QUERY = gql` query adminQuery { myUser { admin @@ -18,6 +18,12 @@ const adminQuery = gql` } ` +const MAPBOX_QUERY = gql` + query mapboxEnabledQuery { + mapboxToken + } +` + const Container = styled.div` height: 100%; display: flex; @@ -88,54 +94,58 @@ const SideButtonLabel = styled.div` font-size: 16px; ` -const Layout = ({ children, title }) => ( - - - {title ? `${title} - Photoview` : `Photoview`} - - - - - - Photos - - - - Albums - - - - Places - - - {({ loading, error, data }) => { - if (data && data.myUser && data.myUser.admin) { - return ( - - - Settings - - ) - } +const Layout = ({ children, title }) => { + const adminQuery = useQuery(ADMIN_QUERY) + const mapboxQuery = useQuery(MAPBOX_QUERY) - return null - }} - - - - Log out - - - - - - {children} -
-
-
-
- -) + const isAdmin = + adminQuery.data && adminQuery.data.myUser && adminQuery.data.myUser.admin + + const mapboxEnabled = mapboxQuery.data && mapboxQuery.data.mapboxToken != null + + return ( + + + {title ? `${title} - Photoview` : `Photoview`} + + + + + + Photos + + + + Albums + + {mapboxEnabled ? ( + + + Places + + ) : null} + {isAdmin ? ( + + + Settings + + ) : null} + + + Log out + + + + + + {children} +
+
+
+
+ + ) +} Layout.propTypes = { children: PropTypes.any.isRequired, diff --git a/ui/src/Pages/PlacesPage/PlacesPage.js b/ui/src/Pages/PlacesPage/PlacesPage.js index 5b817992..05808fa4 100644 --- a/ui/src/Pages/PlacesPage/PlacesPage.js +++ b/ui/src/Pages/PlacesPage/PlacesPage.js @@ -37,7 +37,6 @@ const MapPage = () => { useEffect(() => { async function loadMapboxLibrary() { const mapbox = await import('mapbox-gl') - // mapbox.accessToken = setMapboxLibrary(mapbox) } loadMapboxLibrary() @@ -95,6 +94,23 @@ const MapPage = () => { }) }, [mapContainer, mapboxLibrary, mapboxData]) + if (mapboxData && mapboxData.mapboxToken == null) { + return ( + +

Mapbox token is not set

+

+ To use map related features a mapbox token is needed. +
A mapbox token can be created for free at{' '} + mapbox.com. +

+

+ Make sure the access token is added as the MAPBOX_TOKEN environment + variable. +

+
+ ) + } + return (