From 1271c4857145ab1464de20f0bf62867c68b03fd6 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:44:52 +0300 Subject: [PATCH] Mostly finished loading. User avatar stuff is left to do :sob: --- app.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 18aceb7..7098498 100644 --- a/app.js +++ b/app.js @@ -24,7 +24,7 @@ let servePort; // Object to store all the information we want to save let config = { - userName: 'Anonymous', + userName: '', userAvatar: '', rooms: [] }; @@ -95,8 +95,13 @@ async function initialize() { }); } + readConfigFromFile(); + config.rooms.forEach(room => { + addRoomToList(room); + }); + const registerDiv = document.querySelector('#register'); - if (registerDiv) { + if (registerDiv && !config.userName) { registerDiv.classList.remove('hidden'); } @@ -185,7 +190,7 @@ async function continueRegistration(regUsername) { return; } - config.username = regUsername; + config.userName = regUsername; setupDiv.classList.remove('hidden'); document.querySelector('#register').classList.add('hidden'); loadingDiv.classList.add('hidden'); @@ -193,7 +198,7 @@ async function continueRegistration(regUsername) { const randomTopic = crypto.randomBytes(32); document.querySelector('#chat-room-topic').innerText = truncateHash(b4a.toString(randomTopic, 'hex')); - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } async function createChatRoom() { @@ -240,7 +245,7 @@ function addRoomToList(topic) { roomList.appendChild(roomItem); config.rooms.push(topic); - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } function switchRoom(topic) { @@ -262,7 +267,7 @@ function leaveRoom() { document.querySelector('#setup').classList.remove('hidden'); config.rooms = config.rooms.filter(e => e !== currentRoom.topic); - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } function sendMessage(e) { @@ -397,7 +402,7 @@ async function updateIcon(username, avatarBuffer) { userIcon.src = avatarUrl; config.userAvatar = avatarUrl; - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } } @@ -413,11 +418,18 @@ function toggleSetupView() { setupDiv.classList.toggle('hidden'); } -function writeJsonToFile(filePath, data) { - fs.writeFile(filePath, JSON.stringify(data), (err) => { +function writeConfigToFile(filePath) { + fs.writeFile(filePath, JSON.stringify(config), (err) => { if (err) return console.error(err); console.log("File has been created"); }); } +function readConfigFromFile(filePath) { + fs.readFile(filePath, 'utf8', (err, data) => { + if(err) return console.error("Error while reading config.json! ", err); + config = JSON.parse(data); + }); +} + initialize();