Selaa lähdekoodia

Dialogue de confimration avant suppression

Arthur Brandao 5 vuotta sitten
vanhempi
sitoutus
cc36356dd3
4 muutettua tiedostoa jossa 44 lisäystä ja 13 poistoa
  1. 16 1
      app/main/event.js
  2. 9 0
      app/render/function.js
  3. 8 7
      app/src/index.html
  4. 11 5
      app/src/js/script.js

+ 16 - 1
app/main/event.js

@@ -1,5 +1,20 @@
 const {ipcMain} = require('electron');
+const dialog = require('./function/dialog');
 
 ipcMain.on('refresh-card', (event, arg) => {
     mainWindow.webContents.send('refresh-card');
-})
+});
+
+ipcMain.on('confirm-delete', (event, arg) => {
+    dialog.message(
+        'Confirmation',
+        `Voulez vous vraiment supprimer la note ${arg}`,
+        'question',
+        ['Non', 'Oui'],
+        0,
+        false,
+        (index, btn) => {
+            event.sender.send('delete-card', index === 1);
+        }
+    );
+});

+ 9 - 0
app/render/function.js

@@ -16,4 +16,13 @@ func.loader = function () {
     }
 };
 
+func.confirmDelete = function (title) {
+    ipcRenderer.send('confirm-delete', title);
+    return new Promise(resolve => {
+        ipcRenderer.once('delete-card', (event, arg) => {
+            resolve(arg);
+        });
+    })
+};
+
 module.exports = func;

+ 8 - 7
app/src/index.html

@@ -168,7 +168,7 @@
                 });
             }
 
-            function deleteCard(span, fromCard) {
+            async function deleteCard(span, fromCard) {
                 let card;
                 let id;
                 // Get id and card element to remove
@@ -185,12 +185,13 @@
                     }
                 }
                 // Delete data
-                deleteData(id);
-                // Remove card on the DOM
-                card[2].remove();
-                M.toast({
-                    html: 'Carte supprimée',
-                });
+                if ( await deleteData(id)) {
+                    // Remove card on the DOM
+                    card[2].remove();
+                    M.toast({
+                        html: 'Carte supprimée',
+                    });
+                }
             }
 
             function exportCard(card, event) {

+ 11 - 5
app/src/js/script.js

@@ -96,13 +96,19 @@ function loadData(toArray) {
     });
 }
 
-function deleteData(id) {
+async function deleteData(id) {
     // If card in local storage remove
     if (localStorage[id]) {
-        localStorage.removeItem(id);
-        let list = JSON.parse(localStorage.list);
-        list.removeItem(id);
-        localStorage.setItem('list', JSON.stringify(list));
+        const data = JSON.parse(localStorage[id]);
+        if (await func.confirmDelete(data.title)) {
+            localStorage.removeItem(id);
+            let list = JSON.parse(localStorage.list);
+            list.removeItem(id);
+            localStorage.setItem('list', JSON.stringify(list));
+            return true;
+        } else {
+            return false;
+        }
     }
 }