How to make user data persistent in local storage in electron-app?

I’m building a tasks-to-do application using electron. As I’m new using it, I have found a lot of problems to make the tasks already uploaded persistent in the application.

I’m creating the tasks through a form completed by the user. This creates the task using dynamic HTML.

(Div form showed below)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><div class="div_GEN_form" id='div_GEN_form'>
<div class="form_content" id="form_content">
<div class="class_form_title">
<span class="class_title_content_form">Add your task</span>
</div>
<form id='GEN_form' name='GEN_form' class="form"></form>
<br><br>
<input type="image" src="./icons/BACK.png" alt="Back" class="BACK_img" onclick="DOM_class.CloseFormDiv();">
<input type="image" src="./icons/ADD.png" alt="Submit" class="ADD_img" id="submit_button">
</div>
</div>
</code>
<code><div class="div_GEN_form" id='div_GEN_form'> <div class="form_content" id="form_content"> <div class="class_form_title"> <span class="class_title_content_form">Add your task</span> </div> <form id='GEN_form' name='GEN_form' class="form"></form> <br><br> <input type="image" src="./icons/BACK.png" alt="Back" class="BACK_img" onclick="DOM_class.CloseFormDiv();"> <input type="image" src="./icons/ADD.png" alt="Submit" class="ADD_img" id="submit_button"> </div> </div> </code>
<div class="div_GEN_form" id='div_GEN_form'>
        <div class="form_content" id="form_content">
            <div class="class_form_title">
                <span class="class_title_content_form">Add your task</span>
            </div>

            <form id='GEN_form' name='GEN_form' class="form"></form>
            <br><br>

            <input type="image" src="./icons/BACK.png" alt="Back" class="BACK_img" onclick="DOM_class.CloseFormDiv();">
            <input type="image" src="./icons/ADD.png" alt="Submit" class="ADD_img" id="submit_button">
        </div>
    </div>

The real problem is that I cannot find a solution to make the tasks already added permanent, since when I close the application, every task dissapears. This is the way I’m trying:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>static AddDataToTable(typeImageSrc, date, name, comments) {
const table = document.getElementById('id_data_table');
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
const taskExists = tasks.some(task => task.date === date && task.name === name && task.comments === comments);
if (!taskExists) {
const newRow = document.createElement('div');
newRow.className = 'data_row';
newRow.innerHTML = `
<div class="data_cell"><img src="${typeImageSrc}" class="table_image"></div>
<div class="data_cell">${date}</div>
<div class="data_cell">${name}</div>
<div class="data_cell">${comments}</div>
<div class="data_cell"><img src="./icons/delete.png" class="delete_button" onclick="form.DeleteTask(this)"></div>
`;
table.appendChild(newRow);
tasks.push({ typeImageSrc, date, name, comments });
localStorage.setItem('tasks', JSON.stringify(tasks));
}
}
static DeleteTask(button) {
const row = button.closest('.data_row');
const cells = row.querySelectorAll('.data_cell');
const date = cells[1].innerText;
const name = cells[2].innerText;
const comments = cells[3].innerText;
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
const filteredTasks = tasks.filter(task => !(task.date === date && task.name === name && task.comments === comments));
localStorage.setItem('tasks', JSON.stringify(filteredTasks));
row.remove();
}
static LoadDataFromStorage() {
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks.forEach(task => {
this.AddDataToTable(task.typeImageSrc, task.date, task.name, task.comments);
});
}
}
module.exports = form;
form.LoadDataFromStorage();
</code>
<code>static AddDataToTable(typeImageSrc, date, name, comments) { const table = document.getElementById('id_data_table'); const tasks = JSON.parse(localStorage.getItem('tasks')) || []; const taskExists = tasks.some(task => task.date === date && task.name === name && task.comments === comments); if (!taskExists) { const newRow = document.createElement('div'); newRow.className = 'data_row'; newRow.innerHTML = ` <div class="data_cell"><img src="${typeImageSrc}" class="table_image"></div> <div class="data_cell">${date}</div> <div class="data_cell">${name}</div> <div class="data_cell">${comments}</div> <div class="data_cell"><img src="./icons/delete.png" class="delete_button" onclick="form.DeleteTask(this)"></div> `; table.appendChild(newRow); tasks.push({ typeImageSrc, date, name, comments }); localStorage.setItem('tasks', JSON.stringify(tasks)); } } static DeleteTask(button) { const row = button.closest('.data_row'); const cells = row.querySelectorAll('.data_cell'); const date = cells[1].innerText; const name = cells[2].innerText; const comments = cells[3].innerText; const tasks = JSON.parse(localStorage.getItem('tasks')) || []; const filteredTasks = tasks.filter(task => !(task.date === date && task.name === name && task.comments === comments)); localStorage.setItem('tasks', JSON.stringify(filteredTasks)); row.remove(); } static LoadDataFromStorage() { const tasks = JSON.parse(localStorage.getItem('tasks')) || []; tasks.forEach(task => { this.AddDataToTable(task.typeImageSrc, task.date, task.name, task.comments); }); } } module.exports = form; form.LoadDataFromStorage(); </code>
static AddDataToTable(typeImageSrc, date, name, comments) {
        const table = document.getElementById('id_data_table');

        const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
        const taskExists = tasks.some(task => task.date === date && task.name === name && task.comments === comments);

        if (!taskExists) {
            const newRow = document.createElement('div');
            newRow.className = 'data_row';

            newRow.innerHTML = `
                <div class="data_cell"><img src="${typeImageSrc}" class="table_image"></div>
                <div class="data_cell">${date}</div>
                <div class="data_cell">${name}</div>
                <div class="data_cell">${comments}</div>
                <div class="data_cell"><img src="./icons/delete.png" class="delete_button" onclick="form.DeleteTask(this)"></div>
            `;

            table.appendChild(newRow);

            tasks.push({ typeImageSrc, date, name, comments });
            localStorage.setItem('tasks', JSON.stringify(tasks));
        }
    }

    static DeleteTask(button) {
        const row = button.closest('.data_row');
        const cells = row.querySelectorAll('.data_cell');
        const date = cells[1].innerText;
        const name = cells[2].innerText;
        const comments = cells[3].innerText;

        const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
        const filteredTasks = tasks.filter(task => !(task.date === date && task.name === name && task.comments === comments));

        localStorage.setItem('tasks', JSON.stringify(filteredTasks));
        row.remove();
    }

    static LoadDataFromStorage() {
        const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
        tasks.forEach(task => {
            this.AddDataToTable(task.typeImageSrc, task.date, task.name, task.comments);
        });
    }
}

module.exports = form;
form.LoadDataFromStorage();

I have also a main.js where I tried to configure the settings:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false,
enableRemoteModule: true,
nodeIntegration: true
}
});
mainWindow.loadFile('index.html');
mainWindow.webContents.on('did-finish-load', () => {
// No need to add an event listener here since it's done in the HTML
});
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
</code>
<code>const { app, BrowserWindow } = require('electron'); const path = require('path'); function createWindow() { const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { preload: path.join(__dirname, 'preload.js'), contextIsolation: false, enableRemoteModule: true, nodeIntegration: true } }); mainWindow.loadFile('index.html'); mainWindow.webContents.on('did-finish-load', () => { // No need to add an event listener here since it's done in the HTML }); } app.on('ready', createWindow); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); </code>
const { app, BrowserWindow } = require('electron');
const path = require('path');

function createWindow() {
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            contextIsolation: false,
            enableRemoteModule: true,
            nodeIntegration: true
        }
    });

    mainWindow.loadFile('index.html');

    mainWindow.webContents.on('did-finish-load', () => {
        // No need to add an event listener here since it's done in the HTML
    });
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow();
    }
});

I know that I can use electron storage module, but I don’t know what to do.

Thank you

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật