I have immense pain in the left temple.

This commit is contained in:
Patrick_Pluto 2024-10-09 09:34:42 +02:00
parent 5adb7ff56e
commit 6435bfad8d
3 changed files with 96 additions and 78 deletions

View file

@ -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.');
}
}