Skip to content

Commit

Permalink
feature/mx-1768 prepare url search params (#245)
Browse files Browse the repository at this point in the history
### PR Context

- prep for #241

### Changes

- update mex-common to version 0.49.3
- BREAKING: you must start the local dev mode simply with `pdm run
editor` (no 2nd run)
- BREAKING: rename postfix_badge to render_badge (for consistency)
- simplify some styles

### Fixed

- decorate state handlers with `@rx.event` to satisfy new reflex
versions
- explicitly define cache strategies for vars with
`@rx.var(cache=False)`
  • Loading branch information
cutoffthetop authored Feb 4, 2025
1 parent 4f67d69 commit ec404fc
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
MEX_BACKEND_API_KEY: ${{ secrets.MEX_BACKEND_API_KEY }}
MEX_IDENTITY_PROVIDER: backend
run: |
pdm run editor run &
pdm run editor &
sleep 30 &&
curl --connect-timeout 1 --max-time 20 --retry 10 --retry-delay 1 http://localhost:8000/_system/check &&
curl --connect-timeout 1 --max-time 20 --retry 10 --retry-delay 1 http://localhost:8080/v0/_system/check &&
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changes

- update mex-common to version 0.48.0
- update mex-common to version 0.49.3
- BREAKING: you must start the local dev mode simply with `pdm run editor` (no 2nd run)
- BREAKING: rename postfix_badge to render_badge (for consistency)
- simplify some styles

### Deprecated

Expand All @@ -23,6 +26,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- decorate state handlers with `@rx.event` to satisfy new reflex versions
- explicitly define cache strategies for vars with `@rx.var(cache=False)`

### Security

## [0.8.0] - 2025-01-22
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ components of the MEx project are open-sourced under the same license as well.

### Editor

- `pdm run editor run` starts the editor service
- `pdm run editor` starts the editor service
6 changes: 3 additions & 3 deletions mex/editor/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def render_text(value: EditorValue) -> rx.Component:
)


def postfix_badge(value: EditorValue) -> rx.Component:
"""Render a generic badge after an editor value."""
def render_badge(value: EditorValue) -> rx.Component:
"""Render a generic badge for an editor value."""
return rx.badge(
value.badge,
radius="full",
Expand All @@ -61,7 +61,7 @@ def render_value(value: EditorValue) -> rx.Component:
),
rx.cond(
value.badge,
postfix_badge(value),
render_badge(value),
),
spacing="1",
)
12 changes: 3 additions & 9 deletions mex/editor/edit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ def editor_value_switch(
"""Return a switch for toggling subtractive rules."""
return rx.switch(
checked=value.enabled,
on_change=lambda enabled: cast(EditState, EditState).toggle_field_value(
field_name,
value,
enabled,
),
on_change=cast(EditState, EditState).toggle_field_value(field_name, value),
custom_attrs={"data-testid": f"switch-{field_name}-{primary_source}-{index}"},
)

Expand Down Expand Up @@ -53,10 +49,8 @@ def primary_source_switch(
"""Return a switch for toggling preventive rules."""
return rx.switch(
checked=model.enabled,
on_change=lambda enabled: cast(EditState, EditState).toggle_primary_source(
field_name,
cast(str, model.name.href),
enabled,
on_change=cast(EditState, EditState).toggle_primary_source(
field_name, model.name.href
),
custom_attrs={"data-testid": f"switch-{field_name}-{model.identifier}"},
)
Expand Down
18 changes: 16 additions & 2 deletions mex/editor/edit/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class EditState(State):
stem_type: str | None = None
editor_fields: list[str] = []

@rx.event
def refresh(self) -> Generator[EventSpec | None, None, None]:
"""Refresh the edit page."""
self.reset()
Expand Down Expand Up @@ -82,6 +83,7 @@ def refresh(self) -> Generator[EventSpec | None, None, None]:
preventive=rule_set.preventive,
)

@rx.event
def submit_rule_set(self) -> Generator[EventSpec | None, None, None]:
"""Convert the fields to a rule set and submit it to the backend."""
if (stem_type := self.stem_type) is None:
Expand Down Expand Up @@ -121,13 +123,25 @@ def _get_primary_sources_by_field_name(
msg = f"field not found: {field_name}"
raise ValueError(msg)

def toggle_primary_source(self, field_name: str, href: str, enabled: bool) -> None:
@rx.event
def toggle_primary_source(
self,
field_name: str,
href: str | None,
enabled: bool,
) -> None:
"""Toggle the `enabled` flag of a primary source."""
for primary_source in self._get_primary_sources_by_field_name(field_name):
if primary_source.name.href == href:
primary_source.enabled = enabled

def toggle_field_value(self, field_name: str, value: object, enabled: bool) -> None:
@rx.event
def toggle_field_value(
self,
field_name: str,
value: EditorValue,
enabled: bool,
) -> None:
"""Toggle the `enabled` flag of a field value."""
for primary_source in self._get_primary_sources_by_field_name(field_name):
for editor_value in primary_source.editor_values:
Expand Down
7 changes: 7 additions & 0 deletions mex/editor/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typer
from reflex.reflex import run


def main() -> None: # pragma: no cover
"""Start the editor service."""
typer.run(run)
1 change: 0 additions & 1 deletion mex/editor/merge/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ def index() -> rx.Component:
rx.heading(
"Merge",
custom_attrs={"data-testid": "merge-heading"},
style={"margin": "1em 0"},
)
)
4 changes: 2 additions & 2 deletions mex/editor/search/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def pagination() -> rx.Component:
"""Render pagination for navigating search results."""
return rx.center(
rx.button(
rx.text("Previous", weight="bold"),
rx.text("Previous"),
on_click=SearchState.go_to_previous_page,
disabled=SearchState.disable_previous_page,
spacing="2",
Expand All @@ -121,7 +121,7 @@ def pagination() -> rx.Component:
custom_attrs={"data-testid": "pagination-page-select"},
),
rx.button(
rx.text("Next", weight="bold"),
rx.text("Next"),
on_click=SearchState.go_to_next_page,
disabled=SearchState.disable_next_page,
spacing="2",
Expand Down
15 changes: 11 additions & 4 deletions mex/editor/search/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,58 @@ class SearchState(State):
current_page: int = 1
limit: int = 50

@rx.var
@rx.var(cache=False)
def disable_previous_page(self) -> bool:
"""Disable the 'Previous' button if on the first page."""
return self.current_page <= 1

@rx.var
@rx.var(cache=False)
def disable_next_page(self) -> bool:
"""Disable the 'Next' button if on the last page."""
max_page = math.ceil(self.total / self.limit)
return self.current_page >= max_page

@rx.var
@rx.var(cache=False)
def current_results_length(self) -> int:
"""Return the number of current search results."""
return len(self.results)

@rx.var
@rx.var(cache=False)
def total_pages(self) -> list[str]:
"""Return a list of total pages based on the number of results."""
return [f"{i + 1}" for i in range(math.ceil(self.total / self.limit))]

@rx.event
def set_query_string(self, value: str) -> Generator[EventSpec | None, None, None]:
"""Set the query string and refresh the results."""
self.query_string = value
return self.search()

@rx.event
def set_entity_type(self, value, index) -> Generator[EventSpec | None, None, None]:
"""Set the entity type for filtering and refresh the results."""
self.entity_types[index] = value
return self.search()

@rx.event
def set_page(
self, page_number: str | int
) -> Generator[EventSpec | None, None, None]:
"""Set the current page and refresh the results."""
self.current_page = int(page_number)
return self.search()

@rx.event
def go_to_previous_page(self) -> Generator[EventSpec | None, None, None]:
"""Navigate to the previous page."""
return self.set_page(self.current_page - 1)

@rx.event
def go_to_next_page(self) -> Generator[EventSpec | None, None, None]:
"""Navigate to the next page."""
return self.set_page(self.current_page + 1)

@rx.event
def search(self) -> Generator[EventSpec | None, None, None]:
"""Refresh the search results."""
# TODO(ND): use the user auth for backend requests (stop-gap MX-1616)
Expand All @@ -91,6 +97,7 @@ def search(self) -> Generator[EventSpec | None, None, None]:
self.results = transform_models_to_results(response.items)
self.total = response.total

@rx.event
def refresh(self) -> Generator[EventSpec | None, None, None]:
"""Refresh the search page."""
self.reset()
Expand Down
3 changes: 3 additions & 0 deletions mex/editor/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ class State(rx.State):
NavItem(title="Merge", href_template=r"/merge/"),
]

@rx.event
def logout(self) -> EventSpec:
"""Log out a user."""
self.reset()
return rx.redirect("/")

@rx.event
def check_login(self) -> EventSpec | None:
"""Check if a user is logged in."""
if self.user is None:
self.target_path_after_login = self.router.page.raw_path
return rx.redirect("/login")
return None

@rx.event
def load_nav(self) -> None:
"""Event hook for updating the navigation on page loads."""
self.item_id = self.router.page.params.get("item_id")
Expand Down
5 changes: 4 additions & 1 deletion mex/mex.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

app = rx.App(
html_lang="en",
theme=themes.theme(accent_color="blue"),
theme=themes.theme(
accent_color="blue",
has_background=False,
),
)
app.add_page(
edit_index,
Expand Down
42 changes: 21 additions & 21 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ requires-python = ">=3.11,<3.12"
dependencies = [
"babel>=2,<3",
"fastapi>=0.115,<1",
"mex-common @ git+https://github.com/robert-koch-institut/mex-common.git@0.48.0",
"mex-common @ git+https://github.com/robert-koch-institut/mex-common.git@0.49.3",
"pydantic>=2,<3",
"pytz>=2024,<2025",
"pyyaml>=6,<7",
"reflex-chakra>=0.6,<1",
"reflex>=0.6,<1",
"requests>=2,<3",
"starlette>=0.41,<1",
"typer>=0.15,<1",
"uvicorn>=0.32,<1",
]
optional-dependencies.dev = [
Expand All @@ -34,7 +35,7 @@ optional-dependencies.dev = [
]

[project.scripts]
editor = "reflex.reflex:cli"
editor = "mex.editor.main:main"

[tool.cruft]
template = "https://github.com/robert-koch-institut/mex-template"
Expand Down

0 comments on commit ec404fc

Please sign in to comment.