Working on Config system (Saving & Loading user data) #3
30
app.js
30
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();
|
||||
|
Loading…
Reference in New Issue
Block a user