From 3ba31bc9f53bfdfe04489a065bbe51ddc94e13bb Mon Sep 17 00:00:00 2001 From: Irwan Fathurrahman Date: Tue, 5 Sep 2023 13:49:12 +0700 Subject: [PATCH] Update ArcgGis approach --- .../components/ArcGisAuthorization/index.jsx | 47 +++++++++++++++++++ .../SidePanelTree/Description/index.js | 13 +++-- .../src/components/SidePanelTree/index.jsx | 16 ++++--- .../src/components/SidePanelTree/style.scss | 4 ++ .../LeftPanel/ContextLayers/Layer.jsx | 30 ++++++------ .../templates/frontend/arcgis_callback.html | 15 ++++++ django_project/frontend/urls/__init__.py | 2 + .../frontend/views/arcgis_callback.py | 23 +++++++++ 8 files changed, 127 insertions(+), 23 deletions(-) create mode 100644 django_project/frontend/src/components/ArcGisAuthorization/index.jsx create mode 100644 django_project/frontend/templates/frontend/arcgis_callback.html create mode 100644 django_project/frontend/views/arcgis_callback.py diff --git a/django_project/frontend/src/components/ArcGisAuthorization/index.jsx b/django_project/frontend/src/components/ArcGisAuthorization/index.jsx new file mode 100644 index 000000000..e39b893bd --- /dev/null +++ b/django_project/frontend/src/components/ArcGisAuthorization/index.jsx @@ -0,0 +1,47 @@ +/** + * GeoSight is UNICEF's geospatial web-based business intelligence platform. + * + * Contact : geosight-no-reply@unicef.org + * + * .. note:: This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * __author__ = 'irwan@kartoza.com' + * __date__ = '05/09/2023' + * __copyright__ = ('Copyright 2023, Unicef') + */ + +import React from "react"; +import Cookies from "js-cookie"; +import LoginIcon from '@mui/icons-material/Login'; + + +/** + * ArcGis authorization + */ + +export default function ArcGisAuthorization({ data }) { + const arcGisDomain = data.url.split('rest')[0] + + const { authUrl, clientId } = { + authUrl: 'https://www.arcgis.com/sharing/rest/oauth2/authorize/', + clientId: 'wEv82B8xVKlg9TUv' + } + + function onReceivedMessage(event) { + if (event.origin === document.location.origin) { + Cookies.set(arcGisDomain, event.data) + document.location.reload(); + } + } + + /** Login to arcgis **/ + const login = () => { + window.open(`${authUrl}?client_id=${clientId}&redirect_uri=${document.location.origin}/arcgis-callback&response_type=token&expiration=20160`, "popup", "popup=true"); + window.addEventListener('message', onReceivedMessage, false); + } + + return +} \ No newline at end of file diff --git a/django_project/frontend/src/components/SidePanelTree/Description/index.js b/django_project/frontend/src/components/SidePanelTree/Description/index.js index 84059d6f0..f6e0ceddc 100644 --- a/django_project/frontend/src/components/SidePanelTree/Description/index.js +++ b/django_project/frontend/src/components/SidePanelTree/Description/index.js @@ -22,6 +22,7 @@ import CustomPopover from "../../CustomPopover"; import { fetchingData } from "../../../Requests"; import { formatDateTime } from "../../../utils/main"; import { InfoFillIcon } from "../../Icons/" +import ArcGisAuthorization from "../../ArcGisAuthorization"; import './style.scss'; @@ -65,9 +66,9 @@ export default function LayerDescription({ layer }) { setLoading(false) } ) - } + const contextLayerNeedLogin = layerType === LAYER_TYPE_CONTEXT_LAYER && ["token required", "invalid token."].includes(layer?.error?.toLowerCase()) return (
@@ -83,12 +84,18 @@ export default function LayerDescription({ layer }) { horizontal: 'left', }} Button={ - + contextLayerNeedLogin ? +
+ +
: + } showOnHover={true} >
- {layer.error} + { + contextLayerNeedLogin ? "You need to authorize to access this resource by click this icon" : layer.error + }
: diff --git a/django_project/frontend/src/components/SidePanelTree/index.jsx b/django_project/frontend/src/components/SidePanelTree/index.jsx index 0d80ff673..fefa63236 100644 --- a/django_project/frontend/src/components/SidePanelTree/index.jsx +++ b/django_project/frontend/src/components/SidePanelTree/index.jsx @@ -292,9 +292,11 @@ export default function SidePanelTreeView( } /> - {treeData.data?.legend && selected.indexOf(nodesDataId) >= 0 ? -
: ''} + { + treeData.data?.legend && selected.indexOf(nodesDataId) >= 0 ? +
: '' + }
: groupSelectable ? }> - {Array.isArray(treeData.children) - ? treeData.children.map((node) => renderTree(node)) - : null} + { + Array.isArray(treeData.children) + ? treeData.children.map((node) => renderTree(node)) + : null + } }; diff --git a/django_project/frontend/src/components/SidePanelTree/style.scss b/django_project/frontend/src/components/SidePanelTree/style.scss index 5b947a84d..b730ff02b 100644 --- a/django_project/frontend/src/components/SidePanelTree/style.scss +++ b/django_project/frontend/src/components/SidePanelTree/style.scss @@ -37,6 +37,10 @@ .MuiTreeItem-content { flex-direction: row-reverse; + + &.Mui-disabled { + opacity: 1; + } } } } diff --git a/django_project/frontend/src/pages/Dashboard/LeftPanel/ContextLayers/Layer.jsx b/django_project/frontend/src/pages/Dashboard/LeftPanel/ContextLayers/Layer.jsx index 3a417875f..49c42f14d 100644 --- a/django_project/frontend/src/pages/Dashboard/LeftPanel/ContextLayers/Layer.jsx +++ b/django_project/frontend/src/pages/Dashboard/LeftPanel/ContextLayers/Layer.jsx @@ -1,17 +1,17 @@ /** -* GeoSight is UNICEF's geospatial web-based business intelligence platform. -* -* Contact : geosight-no-reply@unicef.org -* -* .. note:: This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU Affero General Public License as published by -* the Free Software Foundation; either version 3 of the License, or -* (at your option) any later version. -* -* __author__ = 'irwan@kartoza.com' -* __date__ = '13/06/2023' -* __copyright__ = ('Copyright 2023, Unicef') -*/ + * GeoSight is UNICEF's geospatial web-based business intelligence platform. + * + * Contact : geosight-no-reply@unicef.org + * + * .. note:: This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * __author__ = 'irwan@kartoza.com' + * __date__ = '13/06/2023' + * __copyright__ = ('Copyright 2023, Unicef') + */ /* ========================================================================== Return layer @@ -19,6 +19,7 @@ import React from 'react'; +import Cookies from "js-cookie"; import EsriData from "../../../../utils/esri/esri-data"; import { dictDeepCopy } from "../../../../utils/main"; import { popupTemplate } from "../../MapLibre/Popup"; @@ -41,8 +42,9 @@ export function RasterTileLayer( export async function ArcgisLayer( layerData, layerFn, legendFn, errorFn, onEachFeature, objectFn ) { + const arcGisDomain = layerData.url.split('rest')[0] const options = { - token: layerData.token + token: layerData.token ? layerData.token : Cookies.get(arcGisDomain) }; const esriData = new EsriData( layerData.name, layerData.url, diff --git a/django_project/frontend/templates/frontend/arcgis_callback.html b/django_project/frontend/templates/frontend/arcgis_callback.html new file mode 100644 index 000000000..ac706acc8 --- /dev/null +++ b/django_project/frontend/templates/frontend/arcgis_callback.html @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/django_project/frontend/urls/__init__.py b/django_project/frontend/urls/__init__.py index b8c14757a..92df249f0 100644 --- a/django_project/frontend/urls/__init__.py +++ b/django_project/frontend/urls/__init__.py @@ -17,6 +17,7 @@ from django.conf.urls import url from django.urls import include +from frontend.views.arcgis_callback import ArcGisCallbackView from frontend.views.georepo_auth_failed import GeoRepoAuthFailedPageView from frontend.views.home import HomePageView from frontend.views.login import LoginPageView @@ -44,5 +45,6 @@ url(r'^georepo_auth_failed/', GeoRepoAuthFailedPageView.as_view(), name='georepo_auth_failed'), url(r'^admin/', include(admin_url)), + url(r'^arcgis-callback', ArcGisCallbackView.as_view()), url(r'^$', HomePageView.as_view(), name='home-view'), ] diff --git a/django_project/frontend/views/arcgis_callback.py b/django_project/frontend/views/arcgis_callback.py new file mode 100644 index 000000000..80f308c86 --- /dev/null +++ b/django_project/frontend/views/arcgis_callback.py @@ -0,0 +1,23 @@ +# coding=utf-8 +""" +GeoSight is UNICEF's geospatial web-based business intelligence platform. + +Contact : geosight-no-reply@unicef.org + +.. note:: This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + +""" +__author__ = 'irwan@kartoza.com' +__date__ = '05/09/2023' +__copyright__ = ('Copyright 2023, Unicef') + +from django.views.generic import TemplateView + + +class ArcGisCallbackView(TemplateView): + """ArcGisCallback View.""" + + template_name = 'frontend/arcgis_callback.html'