Skip to content

Commit

Permalink
set up import and clean up main
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehraVictoria committed Feb 11, 2025
1 parent 0c8f474 commit cf585c9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 39 deletions.
57 changes: 36 additions & 21 deletions mex/editor/aux_search/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def expand_properties_button(result: AuxResult, index: int) -> rx.Component:
)


def import_button(index: int) -> rx.Component:
"""Render a button to import the aux search result to the MEx backend."""
return rx.button(
"Import",
on_click=lambda: AuxState.import_result(index), # type: ignore[call-arg, arg-type]
align="end",
)


def render_preview(result: AuxResult) -> rx.Component:
"""Render a preview of the aux search result."""
return rx.text(
Expand Down Expand Up @@ -57,30 +66,36 @@ def render_all_properties(result: AuxResult) -> rx.Component:
)


def result_title_and_buttons(result: AuxResult, index: int) -> rx.Component:
"""Render the title and buttons for an aux search result."""
return rx.hstack(
rx.text(
rx.hstack(
rx.foreach(
result.title,
render_value,
)
),
weight="bold",
style={
"whiteSpace": "nowrap",
"overflow": "hidden",
"textOverflow": "ellipsis",
"width": "95%",
},
),
expand_properties_button(result, index),
import_button(index),
style={"width": "100%"},
)


def aux_search_result(result: AuxResult, index: int) -> rx.Component:
"""Render an aux search result with expand-button and preview or all properties."""
"""Render an aux search result with title, buttons and preview or all properties."""
return rx.box(
rx.card(
rx.vstack(
rx.hstack(
rx.text(
rx.hstack(
rx.foreach(
result.title,
render_value,
)
),
weight="bold",
style={
"whiteSpace": "nowrap",
"overflow": "hidden",
"textOverflow": "ellipsis",
"width": "95%",
},
),
expand_properties_button(result, index),
style={"width": "100%"},
),
result_title_and_buttons(result, index),
rx.cond(
result.show_properties,
render_all_properties(result),
Expand Down Expand Up @@ -135,7 +150,7 @@ def search_results() -> rx.Component:
width="100%",
),
rx.foreach(
AuxState.results,
AuxState.results_transformed,
aux_search_result,
),
pagination(),
Expand Down
51 changes: 33 additions & 18 deletions mex/editor/aux_search/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@

from mex.common.backend_api.connector import BackendApiConnector
from mex.common.backend_api.models import PaginatedItemsContainer
from mex.common.logging import logger
from mex.common.models import AnyExtractedModel
from mex.editor.aux_search.models import AuxResult
from mex.editor.aux_search.transform import transform_models_to_results
from mex.editor.exceptions import escalate_error
from mex.editor.state import State


class AuxState(State):
"""State management for the aux extractor search page."""

results: list[AuxResult] = []
results_transformed: list[AuxResult] = []
results_extracted: list[AnyExtractedModel] = []
total: int = 0
query_string: str = ""
current_page: int = 1
Expand All @@ -43,11 +44,13 @@ def disable_next_page(self) -> bool:
@rx.var
def current_results_length(self) -> int:
"""Return the number of current search results."""
return len(self.results)
return len(self.results_transformed)

def toggle_show_properties(self, index: int) -> None:
"""Toggle the show properties state."""
self.results[index].show_properties = not self.results[index].show_properties
self.results_transformed[index].show_properties = not self.results_transformed[
index
].show_properties

def set_query_string(self, value: str) -> Generator[EventSpec | None, None, None]:
"""Set the query string and refresh the results."""
Expand All @@ -69,6 +72,25 @@ def go_to_next_page(self) -> Generator[EventSpec | None, None, None]:
"""Navigate to the next page."""
return self.set_page(self.current_page + 1)

def import_result(self, index: int) -> Generator[EventSpec | None, None, None]:
"""Import the selected result to MEx backend."""
connector = BackendApiConnector.get()
try:
connector.post_extracted_items(
extracted_items=[self.results_extracted[index].model_copy()],
)
except HTTPError as exc:
yield from escalate_error(
"backend", "error importing aux search result: %s", exc.response.text
)
else:
yield rx.toast.success(
"Aux search result imported successfully",
duration=5000,
close_button=True,
dismissible=True,
)

def search(self) -> Generator[EventSpec | None, None, None]:
"""Search for wikidata organizations based on the query string."""
if self.query_string == "":
Expand All @@ -86,24 +108,17 @@ def search(self) -> Generator[EventSpec | None, None, None]:
)
except HTTPError as exc:
self.reset()
logger.error(
"error fetching wikidata items: %s",
exc.response.text,
exc_info=False,
)
yield rx.toast.error(
exc.response.text,
duration=5000,
close_button=True,
dismissible=True,
yield from escalate_error(
"backend", "error fetching wikidata items: %s", exc.response.text
)
else:
yield rx.call_script("window.scrollTo({top: 0, behavior: 'smooth'});")
response = PaginatedItemsContainer[AnyExtractedModel].model_validate(
container = PaginatedItemsContainer[AnyExtractedModel].model_validate(
response
) # type: ignore[assignment]
self.results = transform_models_to_results(response.items) # type: ignore[arg-type]
self.total = max(response.total, len(self.results)) # type: ignore[attr-defined]
)
self.results_extracted = container.items
self.results_transformed = transform_models_to_results(container.items)
self.total = max(container.total, len(self.results_transformed))

def refresh(self) -> Generator[EventSpec | None, None, None]:
"""Refresh the search page."""
Expand Down

0 comments on commit cf585c9

Please sign in to comment.