Skip to content

Commit

Permalink
When there are multiple actions in the queue, only call the "updated"…
Browse files Browse the repository at this point in the history
… methods for the last action.
  • Loading branch information
adamghill committed Jun 22, 2024
1 parent 656a21c commit f4ccb2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
15 changes: 14 additions & 1 deletion django_unicorn/views/action_parsers/sync_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@
def handle(component_request: ComponentRequest, component: UnicornView, payload: Dict):
property_name = payload.get("name")
property_value = payload.get("value")
set_property_value(component, property_name, property_value, component_request.data)

call_updated_method = True

# If there is more than one action then only call the updated methods for the last action in the queue
if len(component_request.action_queue) > 1:
call_updated_method = False
last_action = component_request.action_queue[-1:][0]

if last_action.payload.get("name") == property_name and last_action.payload.get("value") == property_value:
call_updated_method = True

set_property_value(
component, property_name, property_value, component_request.data, call_updated_method=call_updated_method
)
10 changes: 6 additions & 4 deletions django_unicorn/views/action_parsers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def set_property_value(
property_name: str,
property_value: Any,
data: Optional[Dict] = None,
call_updated_method=True,
) -> None:
"""
Sets properties on the component.
Expand Down Expand Up @@ -59,8 +60,8 @@ class TestView(UnicornView):
component_or_field._set_property(
property_name_part,
property_value,
call_updating_method=False,
call_updated_method=True,
call_updating_method=False, # the updating method has already been called above
call_updated_method=call_updated_method,
)
else:
# Handle calling the updating/updated method for nested properties
Expand Down Expand Up @@ -101,7 +102,7 @@ class TestView(UnicornView):
if not is_relation_field:
setattr(component_or_field, property_name_part, property_value)

if hasattr(component, updated_function_name):
if hasattr(component, updated_function_name) and call_updated_method:
getattr(component, updated_function_name)(property_value)

data_or_dict[property_name_part] = property_value
Expand All @@ -128,4 +129,5 @@ class TestView(UnicornView):
else:
break

component.updated(property_name, property_value)
if call_updated_method:
component.updated(property_name, property_value)

0 comments on commit f4ccb2e

Please sign in to comment.