comparison libervia/pages/lists/_browser/__init__.py @ 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 a84383c659b4
children 106bae41f5c8
comparison
equal deleted inserted replaced
1393:d9a328ddef9c 1394:72f9639594b2
1 from browser import bind, DOMNode 1 from browser import DOMNode, document, aio
2 from javascript import JSON 2 from javascript import JSON
3 from bridge import Bridge 3 from aio_bridge import Bridge, BridgeException
4 import dialog 4 import dialog
5 5
6 bridge = Bridge() 6 bridge = Bridge()
7 7
8 8
9 def list_delete_cb(item_elt, item): 9 async def on_delete(evt):
10 dialog.notification.show(f"{item['name']!r} has been deleted")
11
12
13 def list_delete_eb(failure_, item_elt, item):
14 dialog.notification.show(
15 f"Error while deleting {item['name']!r}: {failure_['message']}",
16 "error"
17 )
18
19
20 def interest_retract_cb(item_elt, item):
21 print(f"{item['name']!r} removed successfuly from list of interests")
22 item_elt.classList.add("state_deleted")
23 item_elt.bind("transitionend", lambda evt: item_elt.remove())
24 bridge.psNodeDelete(
25 item['service'],
26 item['node'],
27 callback=lambda: list_delete_cb(item_elt, item),
28 errback=lambda failure: list_delete_eb(failure, item_elt, item),
29 )
30
31
32 def interest_retract_eb(failure_, item_elt, item):
33 dialog.notification.show(
34 f"Can't remove list {item['name']!r} from personal interests: "
35 f"{failure_['message']}",
36 "error"
37 )
38
39
40 def delete_ok(evt, notif_elt, item_elt, item):
41 bridge.interestRetract(
42 "", item['id'],
43 callback=lambda: interest_retract_cb(item_elt, item),
44 errback=lambda failure:interest_retract_eb(failure, item_elt, item))
45
46
47 def delete_cancel(evt, notif_elt, item_elt, item):
48 notif_elt.remove()
49 item_elt.classList.remove("selected_for_deletion")
50
51
52 @bind(".action_delete", "click")
53 def on_delete(evt):
54 evt.stopPropagation() 10 evt.stopPropagation()
55 evt.preventDefault() 11 evt.preventDefault()
56 target = evt.currentTarget 12 target = evt.currentTarget
57 item_elt = DOMNode(target.closest('.item')) 13 item_elt = DOMNode(target.closest('.item'))
58 item_elt.classList.add("selected_for_deletion") 14 item_elt.classList.add("selected_for_deletion")
59 item = JSON.parse(item_elt.dataset.item) 15 item = JSON.parse(item_elt.dataset.item)
60 dialog.Confirm( 16 confirmed = await dialog.Confirm(
61 f"List {item['name']!r} will be deleted, are you sure?", 17 f"List {item['name']!r} will be deleted, are you sure?",
62 ok_label="delete", 18 ok_label="delete",
63 ).show( 19 ).ashow()
64 ok_cb=lambda evt, notif_elt: delete_ok(evt, notif_elt, item_elt, item), 20
65 cancel_cb=lambda evt, notif_elt: delete_cancel(evt, notif_elt, item_elt, item), 21 if not confirmed:
66 ) 22 item_elt.classList.remove("selected_for_deletion")
23 return
24
25 try:
26 await bridge.interestRetract("", item['id'])
27 except BridgeException as e:
28 dialog.notification.show(
29 f"Can't remove list {item['name']!r} from personal interests: {e}",
30 "error"
31 )
32 else:
33 print(f"{item['name']!r} removed successfuly from list of interests")
34 item_elt.classList.add("state_deleted")
35 item_elt.bind("transitionend", lambda evt: item_elt.remove())
36 if item.get("creator", False):
37 try:
38 await bridge.psNodeDelete(
39 item['service'],
40 item['node'],
41 )
42 except BridgeException as e:
43 dialog.notification.show(
44 f"Error while deleting {item['name']!r}: {e}",
45 "error"
46 )
47 else:
48 dialog.notification.show(f"{item['name']!r} has been deleted")
49
50
51 for elt in document.select('.action_delete'):
52 elt.bind("click", lambda evt: aio.run(on_delete(evt)))