main #123

Merged
Patrick_Pluto merged 3 commits from sageTheDm/interstellar_ai:main into main 2024-10-09 09:08:50 +02:00
Showing only changes of commit 6bd941031b - Show all commits

View file

@ -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 }> = (
<button className="apply" onClick={async () => {
getAllLocalStorageItems();
closeSettings();
sendToDatabase();
window.location.reload();
}}>
Apply