From 3657cfa4d82f2f4f368c31a1f239811fe385032f Mon Sep 17 00:00:00 2001 From: Sajid Alam Date: Mon, 22 Jul 2024 16:07:41 +0100 Subject: [PATCH] Add new endpoint to fetch initial showDatasetsPreviews value Signed-off-by: Sajid Alam --- package/kedro_viz/api/rest/router.py | 15 +++++++++++++++ src/config.js | 11 +++++++++++ src/utils/preferences-api.js | 24 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/package/kedro_viz/api/rest/router.py b/package/kedro_viz/api/rest/router.py index e03c110483..1c3ba560e9 100644 --- a/package/kedro_viz/api/rest/router.py +++ b/package/kedro_viz/api/rest/router.py @@ -65,6 +65,21 @@ async def update_preferences(preferences: UserPreference): ) +@router.get("/preferences", response_model=UserPreference) +async def get_preferences(): + try: + show_dataset_previews = DataNodeMetadata.is_all_previews_enabled + return JSONResponse( + status_code=200, content={"showDatasetPreviews": show_dataset_previews} + ) + except Exception as exception: + logger.error("Failed to fetch preferences: %s", str(exception)) + return JSONResponse( + status_code=500, + content={"message": f"Failed to fetch preferences: {str(exception)}"}, + ) + + @router.get( "/pipelines/{registered_pipeline_id}", response_model=GraphAPIResponse, diff --git a/src/config.js b/src/config.js index ea25d9d9a6..e7b92ac04d 100644 --- a/src/config.js +++ b/src/config.js @@ -1,4 +1,5 @@ import { sanitizedPathname } from './utils'; +import { fetchPreferences } from './utils/preferences-api'; export const localStorageName = 'KedroViz'; export const localStorageFlowchartLink = 'KedroViz-link-to-flowchart'; @@ -84,6 +85,16 @@ export const settings = { }, }; +// Fetch preferences from the backend and update settings +(async () => { + try { + const preferences = await fetchPreferences(); + settings.showDatasetPreviews.default = preferences.showDatasetPreviews; + } catch (error) { + console.error('Error fetching initial preferences:', error); + } +})(); + // Sidebar groups is an ordered map of { id: label } export const sidebarGroups = { elementType: 'Element types', diff --git a/src/utils/preferences-api.js b/src/utils/preferences-api.js index d397b4658d..c6040a56a7 100644 --- a/src/utils/preferences-api.js +++ b/src/utils/preferences-api.js @@ -30,3 +30,27 @@ export const updatePreferences = async (showDatasetPreviews) => { throw error; } }; + +/** + * Fetch preferences from the backend. + * @return {Promise} Preferences object containing showDatasetPreviews. + */ +export const fetchPreferences = async () => { + try { + const response = await fetch('/api/preferences', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Failed to fetch preferences'); + } + + return await response.json(); + } catch (error) { + console.error('Error fetching preferences:', error); + throw error; + } +};