changeset 1394:72f9639594b2

browser (lists): code is now async + item removal for non creator: when user is creator of item, it is removed from list of interests and node is deleted, otherwise it is only removed from list of interests.
author Goffi <goffi@goffi.org>
date Sat, 27 Feb 2021 21:06:45 +0100
parents d9a328ddef9c
children 4ccf42d8aab7
files libervia/pages/_browser/aio_bridge.py libervia/pages/lists/_browser/__init__.py
diffstat 2 files changed, 39 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/_browser/aio_bridge.py	Sat Feb 27 21:03:55 2021 +0100
+++ b/libervia/pages/_browser/aio_bridge.py	Sat Feb 27 21:06:45 2021 +0100
@@ -19,8 +19,7 @@
         self.module, __, self.classname = str(self.fullname).rpartition(".")
 
     def __str__(self):
-        message = (": %s" % self.message) if self.message else ""
-        return self.classname + message
+        return f"{self.classname}: {self.message or ''}"
 
     def __eq__(self, other):
         return self.classname == other
--- a/libervia/pages/lists/_browser/__init__.py	Sat Feb 27 21:03:55 2021 +0100
+++ b/libervia/pages/lists/_browser/__init__.py	Sat Feb 27 21:06:45 2021 +0100
@@ -1,66 +1,52 @@
-from browser import bind, DOMNode
+from browser import DOMNode, document, aio
 from javascript import JSON
-from bridge import Bridge
+from aio_bridge import Bridge, BridgeException
 import dialog
 
 bridge = Bridge()
 
 
-def list_delete_cb(item_elt, item):
-    dialog.notification.show(f"{item['name']!r} has been deleted")
-
-
-def list_delete_eb(failure_, item_elt, item):
-    dialog.notification.show(
-        f"Error while deleting {item['name']!r}: {failure_['message']}",
-        "error"
-    )
-
-
-def interest_retract_cb(item_elt, item):
-    print(f"{item['name']!r} removed successfuly from list of interests")
-    item_elt.classList.add("state_deleted")
-    item_elt.bind("transitionend", lambda evt: item_elt.remove())
-    bridge.psNodeDelete(
-        item['service'],
-        item['node'],
-        callback=lambda: list_delete_cb(item_elt, item),
-        errback=lambda failure: list_delete_eb(failure, item_elt, item),
-    )
-
-
-def interest_retract_eb(failure_, item_elt, item):
-    dialog.notification.show(
-        f"Can't remove list {item['name']!r} from personal interests: "
-        f"{failure_['message']}",
-        "error"
-    )
-
-
-def delete_ok(evt, notif_elt, item_elt, item):
-    bridge.interestRetract(
-        "", item['id'],
-        callback=lambda: interest_retract_cb(item_elt, item),
-        errback=lambda failure:interest_retract_eb(failure, item_elt, item))
-
-
-def delete_cancel(evt, notif_elt, item_elt, item):
-    notif_elt.remove()
-    item_elt.classList.remove("selected_for_deletion")
-
-
-@bind(".action_delete", "click")
-def on_delete(evt):
+async def on_delete(evt):
     evt.stopPropagation()
     evt.preventDefault()
     target = evt.currentTarget
     item_elt = DOMNode(target.closest('.item'))
     item_elt.classList.add("selected_for_deletion")
     item = JSON.parse(item_elt.dataset.item)
-    dialog.Confirm(
+    confirmed = await dialog.Confirm(
         f"List {item['name']!r} will be deleted, are you sure?",
         ok_label="delete",
-    ).show(
-        ok_cb=lambda evt, notif_elt: delete_ok(evt, notif_elt, item_elt, item),
-        cancel_cb=lambda evt, notif_elt: delete_cancel(evt, notif_elt, item_elt, item),
-    )
+    ).ashow()
+
+    if not confirmed:
+        item_elt.classList.remove("selected_for_deletion")
+        return
+
+    try:
+        await bridge.interestRetract("", item['id'])
+    except BridgeException as e:
+        dialog.notification.show(
+            f"Can't remove list {item['name']!r} from personal interests: {e}",
+            "error"
+        )
+    else:
+        print(f"{item['name']!r} removed successfuly from list of interests")
+        item_elt.classList.add("state_deleted")
+        item_elt.bind("transitionend", lambda evt: item_elt.remove())
+        if item.get("creator", False):
+            try:
+                await bridge.psNodeDelete(
+                    item['service'],
+                    item['node'],
+                )
+            except BridgeException as e:
+                dialog.notification.show(
+                    f"Error while deleting {item['name']!r}: {e}",
+                    "error"
+                )
+            else:
+                dialog.notification.show(f"{item['name']!r} has been deleted")
+
+
+for elt in document.select('.action_delete'):
+    elt.bind("click", lambda evt: aio.run(on_delete(evt)))