I have immense pain in the left temple.
This commit is contained in:
parent
5adb7ff56e
commit
6435bfad8d
3 changed files with 96 additions and 78 deletions
|
@ -5,6 +5,7 @@ import {
|
|||
getSettings
|
||||
} from '../backend/database';
|
||||
import Settings from './settings/Settings'; // Import the Settings component
|
||||
import { importDatabase } from './settings/settingUtils';
|
||||
|
||||
const Login: React.FC = () => {
|
||||
// State to handle popup visibility
|
||||
|
@ -23,14 +24,14 @@ const Login: React.FC = () => {
|
|||
|
||||
// On component mount, check if there are credentials in localStorage
|
||||
useEffect(() => {
|
||||
let savedAccountName:string|null;
|
||||
let savedAccountEmail:string|null;
|
||||
let savedAccountPassword:string|null;
|
||||
let savedAccountName: string | null;
|
||||
let savedAccountEmail: string | null;
|
||||
let savedAccountPassword: string | null;
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
savedAccountName = localStorage.getItem('accountName');
|
||||
savedAccountEmail = localStorage.getItem('accountEmail');
|
||||
savedAccountPassword = localStorage.getItem('accountPassword');
|
||||
|
||||
|
||||
// If credentials are found in localStorage, log the user in
|
||||
if (savedAccountName && savedAccountEmail && savedAccountPassword) {
|
||||
setAccountName(savedAccountName);
|
||||
|
@ -40,6 +41,14 @@ const Login: React.FC = () => {
|
|||
if (savedAccountName !== null && savedAccountPassword !== null) {
|
||||
const success = await checkCredentials(savedAccountName, savedAccountPassword);
|
||||
setIsLoggedIn(success); // Automatically log in
|
||||
const useName = localStorage.getItem("accountName");
|
||||
const usePassword = localStorage.getItem("accountPassword");
|
||||
|
||||
if (useName && usePassword) {
|
||||
await importDatabase(useName, usePassword);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
check();
|
||||
|
@ -64,11 +73,19 @@ const Login: React.FC = () => {
|
|||
setIsLoggedIn(true); // Successful login
|
||||
const data = await getSettings(accountName, password)
|
||||
if (data) {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem("dataFromServer", data)
|
||||
}
|
||||
}
|
||||
setShowLoginPopup(false); // Close the login popup
|
||||
const useName = localStorage.getItem("accountName");
|
||||
const usePassword = localStorage.getItem("accountPassword");
|
||||
|
||||
if (useName && usePassword) {
|
||||
await importDatabase(useName, usePassword);
|
||||
}
|
||||
|
||||
window.location.reload();
|
||||
} else {
|
||||
alert('Incorrect credentials');
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//#region imports
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { applyTheme } from './theme';
|
||||
import { exportSettings, importSettings } from './settingUtils'; // Import utility functions
|
||||
import { exportSettings, importSettings, sendToDatabase, importDatabase } from './settingUtils'; // Import utility functions
|
||||
import { getAllLocalStorageItems } from '../../backend/GetLocalStorage';
|
||||
import ColorSetting from './ColorSettings';
|
||||
import TextSettings from './TextSettings'
|
||||
|
@ -15,8 +15,6 @@ import {
|
|||
changeSettings,
|
||||
createAccount,
|
||||
deleteAccount,
|
||||
getSettings,
|
||||
sendToDatabase
|
||||
} from '../../backend/database';
|
||||
import ThemeDropdown from './DropDownTheme';
|
||||
|
||||
|
@ -302,7 +300,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
|
|||
useEffect(() => {
|
||||
const useName = localStorage.getItem("accountName");
|
||||
const usePassword = localStorage.getItem("accountPassword");
|
||||
|
||||
|
||||
if (useName && usePassword) {
|
||||
importDatabase(useName, usePassword);
|
||||
}
|
||||
|
@ -314,27 +312,6 @@ 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);
|
||||
|
||||
// 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
|
||||
|
@ -593,7 +570,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
|
|||
)}
|
||||
</div>
|
||||
);
|
||||
//#region custom --> foss
|
||||
//#region custom --> foss
|
||||
case 'foss':
|
||||
return (
|
||||
<div className="settings-section">
|
||||
|
@ -606,7 +583,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
|
|||
</div>
|
||||
);
|
||||
|
||||
//#region account
|
||||
//#region account
|
||||
case 'account':
|
||||
return (
|
||||
<div className="settings-section">
|
||||
|
@ -649,7 +626,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
|
|||
/>
|
||||
</div>
|
||||
);
|
||||
//#region api
|
||||
//#region api
|
||||
case 'api':
|
||||
return (
|
||||
<div className="settings-section">
|
||||
|
@ -703,7 +680,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
|
|||
</div>
|
||||
</div>
|
||||
);
|
||||
//#region import export
|
||||
//#region import export
|
||||
case 'im/export':
|
||||
return (
|
||||
<div className="settings-section">
|
||||
|
@ -759,52 +736,51 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
|
|||
//#region overall export
|
||||
return (
|
||||
<div className="popup-overlay">
|
||||
<div className="settings-content">
|
||||
<div className="settings-container">
|
||||
{/* Sidebar for desktop */}
|
||||
<div className="sidebar">
|
||||
<ul>
|
||||
<li onClick={() => setActiveSection('general')}>General</li>
|
||||
<li onClick={() => setActiveSection('privacy')}>Privacy</li>
|
||||
<li onClick={() => setActiveSection('theme')}>Theme</li>
|
||||
<li onClick={() => setActiveSection('foss')}>FOSS</li>
|
||||
<li onClick={() => setActiveSection('account')}>Account</li>
|
||||
<li onClick={() => setActiveSection('api')}>API Keys</li>
|
||||
<li onClick={() => setActiveSection('im/export')}>Import/Export</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div className="settings-content">
|
||||
<div className="settings-container">
|
||||
{/* Sidebar for desktop */}
|
||||
<div className="sidebar">
|
||||
<ul>
|
||||
<li onClick={() => setActiveSection('general')}>General</li>
|
||||
<li onClick={() => setActiveSection('privacy')}>Privacy</li>
|
||||
<li onClick={() => setActiveSection('theme')}>Theme</li>
|
||||
<li onClick={() => setActiveSection('foss')}>FOSS</li>
|
||||
<li onClick={() => setActiveSection('account')}>Account</li>
|
||||
<li onClick={() => setActiveSection('api')}>API Keys</li>
|
||||
<li onClick={() => setActiveSection('im/export')}>Import/Export</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div className="settings-main">
|
||||
{/* Dropdown for selections in responsive mode */}
|
||||
<div className="settings-option dropdown">
|
||||
<div className="dropdown-header"><h2>Select a Setting</h2></div>
|
||||
<select onChange={(e) => setActiveSection(e.target.value)} value={activeSection}>
|
||||
<option value="general">General</option>
|
||||
<option value="privacy">Privacy</option>
|
||||
<option value="theme">Theme</option>
|
||||
<option value="foss">FOSS</option>
|
||||
<option value="account">Account</option>
|
||||
<option value="api">API Keys</option>
|
||||
<option value="im/export">Import/Export</option>
|
||||
</select>
|
||||
</div>
|
||||
<h2>Settings for {accountName}</h2>
|
||||
{renderSettingsContent()}
|
||||
<button className="close-popup" onClick={closeSettings}>Close</button>
|
||||
<button className="apply" onClick={async () => {
|
||||
getAllLocalStorageItems();
|
||||
closeSettings();
|
||||
sendToDatabase();
|
||||
window.location.reload();
|
||||
}}>
|
||||
Apply
|
||||
</button>
|
||||
|
||||
</div>
|
||||
<div className="settings-main">
|
||||
{/* Dropdown for selections in responsive mode */}
|
||||
<div className="settings-option dropdown">
|
||||
<div className="dropdown-header"><h2>Select a Setting</h2></div>
|
||||
<select onChange={(e) => setActiveSection(e.target.value)} value={activeSection}>
|
||||
<option value="general">General</option>
|
||||
<option value="privacy">Privacy</option>
|
||||
<option value="theme">Theme</option>
|
||||
<option value="foss">FOSS</option>
|
||||
<option value="account">Account</option>
|
||||
<option value="api">API Keys</option>
|
||||
<option value="im/export">Import/Export</option>
|
||||
</select>
|
||||
</div>
|
||||
<h2>Settings for {accountName}</h2>
|
||||
{renderSettingsContent()}
|
||||
<button className="close-popup" onClick={closeSettings}>Close</button>
|
||||
<button className="apply" onClick={async () => {
|
||||
getAllLocalStorageItems();
|
||||
closeSettings();
|
||||
sendToDatabase();
|
||||
}}>
|
||||
Apply
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
// settingsManager.ts
|
||||
import { changeSettings, getSettings } from "@/app/backend/database";
|
||||
|
||||
// Method to export localStorage to a JSON object
|
||||
export function exportSettings(): string {
|
||||
const settings: { [key: string]: string } = {};
|
||||
|
||||
// Loop through all keys in localStorage and add them to the settings object
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
for (let i = 0; i < localStorage.length; i++) {
|
||||
const key = localStorage.key(i);
|
||||
if (key) {
|
||||
|
@ -26,7 +27,7 @@ export function importSettings(jsonData: string): void {
|
|||
const parsedSettings = JSON.parse(jsonData);
|
||||
|
||||
// Loop through parsed settings and save them in localStorage
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
Object.keys(parsedSettings).forEach((key) => {
|
||||
localStorage.setItem(key, parsedSettings[key]);
|
||||
});
|
||||
|
@ -37,3 +38,27 @@ export function importSettings(jsonData: string): void {
|
|||
console.error("Invalid JSON data:", error);
|
||||
}
|
||||
}
|
||||
|
||||
export const sendToDatabase = async () => {
|
||||
let useName = localStorage.getItem("accountName")
|
||||
let usePassword = localStorage.getItem("accountPassword")
|
||||
if (useName && usePassword) {
|
||||
let result = await changeSettings(useName, usePassword, JSON.parse(exportSettings()))
|
||||
if (result == true) {
|
||||
alert('Data has been transferred')
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
export 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) {
|
||||
importSettings(JSON.stringify(databaseSettings, null, 2)); // Pass only the current user's settings
|
||||
} else {
|
||||
console.error('Database settings are not in the expected format.');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue