From 2eb04dfcb089374b4a7b41b40ffe012f28c9cefa Mon Sep 17 00:00:00 2001 From: rosanna-smith Date: Fri, 13 Dec 2024 11:55:27 +1100 Subject: [PATCH 1/4] code in crateoview.vue for remembering selected mode after uploading spreadsheet data --- src/app/views/CrateoView.vue | 59 +++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/app/views/CrateoView.vue b/src/app/views/CrateoView.vue index b449802..31c29a6 100644 --- a/src/app/views/CrateoView.vue +++ b/src/app/views/CrateoView.vue @@ -335,7 +335,18 @@ function ready(roc, refresh) { const conformsToCrate = roc.rootDataset['conformsTo'] || []; const profileIndex = data.profiles.findIndex(p => conformsToCrate.some(ct => (p?.conformsToUri || []).includes(ct['@id']))); - data.selectedProfile = profileIndex >= 0 ? profileIndex : 0; + console.log('Profile Index:', profileIndex); // Accessing profileIndex here + + if (profileIndex.value >= 0) { + const selectedProfile = profileOptions.value[profileIndex.value]; + data.selectedProfile = selectedProfile?.value || profileOptions.value[0]?.value; + } else { + // If profileIndex is invalid, fallback only if no savedProfileValue is found + if (!savedProfileValue.value) { + data.selectedProfile = profileOptions.value[0]?.value; // Fallback to the first profile if no saved profile exists + } + } + data.loading = false; console.log('ready'); editor.crate = roc; @@ -343,6 +354,52 @@ function ready(roc, refresh) { } const activeNames = ref(['1']); + +import { onMounted, watch } from 'vue'; + +const savedProfileValue = ref(null); // reactive reference for the saved profile value +const profileIndex = ref(-1); // default value for profileIndex, set to -1 initially + +onMounted(() => { + // // Check if it's the first page load or a new session + // if (!sessionStorage.getItem('sessionInitialized')) { + // // If not already set, initialize the session + // sessionStorage.setItem('sessionInitialized', 'true'); + // // Clear localStorage on first load (new tab/window) + // localStorage.clear(); + // } + // Retrieve the saved profile from localStorage + savedProfileValue.value = localStorage.getItem('selectedProfile'); + + // Use the computed profileOptions value to get the options + const options = profileOptions.value; + // Try to load the saved profile from localStorage if it exists + if (savedProfileValue.value) { + const savedProfile = options.find(option => option.value === savedProfileValue.value); + if (savedProfile) { + // If a valid saved profile is found, set it as the selected profile + data.selectedProfile = savedProfile.value; + } else { + // If the saved profile is no longer valid, fallback to profileIndex or default to 0 + data.selectedProfile = profileIndex.value >= 0 ? options[profileIndex.value]?.value : options[0]?.value; + } + } else { + // No saved profile found in localStorage, fallback to profileIndex or default to 0 + data.selectedProfile = profileIndex.value >= 0 ? options[profileIndex.value]?.value : options[0]?.value; + } + // If no profile has been selected and no valid profile is found, default to the first profile + if (!data.selectedProfile) { + data.selectedProfile = options[0]?.value; // Default to the first profile in options + } +}); + +// Watch for changes in selectedProfile and update localStorage +watch(() => data.selectedProfile, (newValue) => { + if (newValue) { + localStorage.setItem('selectedProfile', newValue); + } +}); +