2024-10-03 10:58:41 +02:00
|
|
|
// settingsManager.ts
|
2024-10-09 09:34:42 +02:00
|
|
|
import { changeSettings, getSettings } from "@/app/backend/database";
|
2024-09-24 09:27:49 +02:00
|
|
|
|
2024-10-03 10:58:41 +02:00
|
|
|
// Method to export localStorage to a JSON object
|
|
|
|
export function exportSettings(): string {
|
2024-10-07 09:09:15 +02:00
|
|
|
const settings: { [key: string]: string } = {};
|
2024-09-26 14:38:59 +02:00
|
|
|
|
2024-10-03 11:29:58 +02:00
|
|
|
// Loop through all keys in localStorage and add them to the settings object
|
2024-10-09 09:34:42 +02:00
|
|
|
if (typeof localStorage !== 'undefined') {
|
2024-10-07 11:16:51 +02:00
|
|
|
for (let i = 0; i < localStorage.length; i++) {
|
|
|
|
const key = localStorage.key(i);
|
|
|
|
if (key) {
|
|
|
|
if (key !== "accountName" && key !== "accountPassword" && key !== "accountEmail") {
|
|
|
|
settings[key] = localStorage.getItem(key) || "";
|
|
|
|
}
|
2024-10-03 11:29:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-03 10:58:41 +02:00
|
|
|
|
2024-10-03 11:29:58 +02:00
|
|
|
// Convert settings object to JSON string
|
|
|
|
return JSON.stringify(settings, null, 2);
|
2024-10-03 10:58:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Method to import settings from a JSON object, clearing old localStorage
|
|
|
|
export function importSettings(jsonData: string): void {
|
2024-10-03 11:29:58 +02:00
|
|
|
try {
|
|
|
|
const parsedSettings = JSON.parse(jsonData);
|
2024-10-03 10:58:41 +02:00
|
|
|
|
2024-10-03 11:29:58 +02:00
|
|
|
// Loop through parsed settings and save them in localStorage
|
2024-10-09 09:34:42 +02:00
|
|
|
if (typeof localStorage !== 'undefined') {
|
2024-10-07 11:16:51 +02:00
|
|
|
Object.keys(parsedSettings).forEach((key) => {
|
|
|
|
localStorage.setItem(key, parsedSettings[key]);
|
|
|
|
});
|
|
|
|
}
|
2024-10-03 10:58:41 +02:00
|
|
|
|
2024-10-03 11:29:58 +02:00
|
|
|
console.log("Settings imported successfully!");
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Invalid JSON data:", error);
|
|
|
|
}
|
2024-10-03 10:58:41 +02:00
|
|
|
}
|
2024-10-09 09:34:42 +02:00
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|