Merge pull request 'main' (#123) from sageTheDm/interstellar_ai:main into main

Reviewed-on: https://interstellardevelopment.org/code/code/React-Group/interstellar_ai/pulls/123
This commit is contained in:
Patrick 2024-10-09 09:08:49 +02:00
commit 5adb7ff56e

View file

@ -15,7 +15,8 @@ import {
changeSettings, changeSettings,
createAccount, createAccount,
deleteAccount, deleteAccount,
getSettings getSettings,
sendToDatabase
} from '../../backend/database'; } from '../../backend/database';
import ThemeDropdown from './DropDownTheme'; import ThemeDropdown from './DropDownTheme';
@ -299,11 +300,13 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
}; };
useEffect(() => { useEffect(() => {
let useName = localStorage.getItem("accountName") const useName = localStorage.getItem("accountName");
let usePassword = localStorage.getItem("accountPassword") const usePassword = localStorage.getItem("accountPassword");
if (useName && usePassword) { if (useName && usePassword) {
importDatabase(useName, usePassword) importDatabase(useName, usePassword);
} }
const savedTheme = localStorage.getItem('selectedTheme'); const savedTheme = localStorage.getItem('selectedTheme');
if (savedTheme) { if (savedTheme) {
setSelectedTheme(savedTheme); setSelectedTheme(savedTheme);
@ -311,10 +314,27 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
} }
}, []); // Runs only once when the component mounts }, []); // Runs only once when the component mounts
const importDatabase = async (useName : string, usePassword : string) => { const importDatabase = async (useName: string, usePassword: string) => {
const databaseSettings = await getSettings(useName, usePassword) const databaseSettings = await getSettings(useName, usePassword);
importSettings(databaseSettings)
// 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 // Effect hooks to update localStorage whenever any state changes
useEffect(() => { useEffect(() => {
// Flatten nested objects // Flatten nested objects
@ -365,7 +385,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
} }
if (await createAccount(useName, useEmail, usePassword)) { if (await createAccount(useName, useEmail, usePassword)) {
if (await changeSettings(useName, usePassword, handleExport)) { if (await changeSettings(useName, usePassword, settings)) {
localStorage.setItem("currentName", useName) localStorage.setItem("currentName", useName)
localStorage.setItem("currentPassword", usePassword) localStorage.setItem("currentPassword", usePassword)
localStorage.setItem("currentEmail", useEmail) localStorage.setItem("currentEmail", useEmail)
@ -774,6 +794,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
<button className="apply" onClick={async () => { <button className="apply" onClick={async () => {
getAllLocalStorageItems(); getAllLocalStorageItems();
closeSettings(); closeSettings();
sendToDatabase();
window.location.reload(); window.location.reload();
}}> }}>
Apply Apply