From 6bd941031ba825f64fa9fccff6d1c8a6eeeab1f6 Mon Sep 17 00:00:00 2001 From: sageTheDM Date: Wed, 9 Oct 2024 09:08:12 +0200 Subject: [PATCH] AHHHHHHH --- app/components/settings/Settings.tsx | 37 ++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app/components/settings/Settings.tsx b/app/components/settings/Settings.tsx index d1adb30..afe571d 100644 --- a/app/components/settings/Settings.tsx +++ b/app/components/settings/Settings.tsx @@ -15,7 +15,8 @@ import { changeSettings, createAccount, deleteAccount, - getSettings + getSettings, + sendToDatabase } from '../../backend/database'; import ThemeDropdown from './DropDownTheme'; @@ -299,11 +300,13 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = ( }; useEffect(() => { - let useName = localStorage.getItem("accountName") - let usePassword = localStorage.getItem("accountPassword") + const useName = localStorage.getItem("accountName"); + const usePassword = localStorage.getItem("accountPassword"); + if (useName && usePassword) { - importDatabase(useName, usePassword) + importDatabase(useName, usePassword); } + const savedTheme = localStorage.getItem('selectedTheme'); if (savedTheme) { setSelectedTheme(savedTheme); @@ -311,10 +314,27 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = ( } }, []); // Runs only once when the component mounts - const importDatabase = async (useName : string, usePassword : string) => { - const databaseSettings = await getSettings(useName, usePassword) - importSettings(databaseSettings) + const importDatabase = async (useName: string, usePassword: string) => { + const databaseSettings = await getSettings(useName, usePassword); + + // Ensure user settings exist before flattening and storing + if (typeof databaseSettings == 'object' && databaseSettings) { + JSON.stringify(importSettings(databaseSettings), null, 2); // Pass only the current user's settings + } else { + console.error('Database settings are not in the expected format.'); + } } + + const sendToDatabase = async () => { + let useName = localStorage.getItem("accountName") + let usePassword = localStorage.getItem("accountPassword") + if (useName && usePassword) { + if (await changeSettings(useName, usePassword, JSON.parse(exportSettings()))) { + alert('Data has been transferred') + } + } + }; + // Effect hooks to update localStorage whenever any state changes useEffect(() => { // Flatten nested objects @@ -365,7 +385,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = ( } if (await createAccount(useName, useEmail, usePassword)) { - if (await changeSettings(useName, usePassword, handleExport)) { + if (await changeSettings(useName, usePassword, settings)) { localStorage.setItem("currentName", useName) localStorage.setItem("currentPassword", usePassword) localStorage.setItem("currentEmail", useEmail) @@ -774,6 +794,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (