Skip to content

Commit

Permalink
remove set operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Oct 7, 2022
1 parent 6aa6bbc commit f2b8871
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 60 deletions.
6 changes: 4 additions & 2 deletions conreq/_core/app_store/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from conreq._core.app_store.models import Category, Subcategory
from conreq.types import HomepageState

# pylint: disable=unused-argument


class PlaceholderApp:
uuid = 0
Expand Down Expand Up @@ -40,13 +42,13 @@ def app_store(state: HomepageState, set_state):

# Display loading animation until categories are loaded
if loading_needed and not categories:
state.set_loading(True)
state.viewport_loading = True
set_loading_needed(False)
set_state(state)

# Hide loading animation once categories are loaded
if not loading_needed and categories:
state.set_loading(False)
state.viewport_loading = False
set_loading_needed(True)
set_state(state)

Expand Down
10 changes: 5 additions & 5 deletions conreq/_core/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def set_tab_state(obj):

websocket = use_websocket()

if not tab_state or not tab_state._tab:
if not tab_state or not tab_state.tab:
return

html_class = tab_state._tab.html_class
html_class = tab_state.tab.html_class

return div(
{
Expand All @@ -47,7 +47,7 @@ def set_tab_state(obj):
},
div(
{"className": "tabbed-viewport"},
tab_state._tab.component(state, set_state),
tab_state.tab.component(state, set_state),
),
ul(
{"className": "tabbed-viewport-selector list-group"},
Expand Down Expand Up @@ -115,10 +115,10 @@ async def on_click(event):
tab.on_click(click_event)
return

tab_state.set_tab(tab)
tab_state.tab = tab
set_tab_state(tab_state)

return {
"className": f"list-group-item clickable{' active' if tab_state._tab is tab else ''}",
"className": f"list-group-item clickable{' active' if tab_state.tab is tab else ''}",
"onClick": on_click,
}
10 changes: 5 additions & 5 deletions conreq/_core/home/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ def set_state(obj):
new_obj = copy(obj)
_set_state(new_obj)

@idom.hooks.use_effect(dependencies=[state._viewport_intent])
@idom.hooks.use_effect(dependencies=[state.viewport_intent])
def set_viewport():
"""Determine what viewport to set the viewport based on intent."""
# sourcery skip:remove-redundant-if, merge-duplicate-blocks
if not state._viewport_intent:
if not state.viewport_intent:
return

# Replace the selected viewport
state._viewport = state._viewport_intent
state._viewport_intent = None
state.modal_state.set_show(False)
state._viewport = state.viewport_intent
state.viewport_intent = None
state.modal_state.show = False
state._modal = None

set_state(state)
Expand Down
8 changes: 5 additions & 3 deletions conreq/_core/home/components/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def modal(state: HomepageState, set_state):
"let conreq_modal = new bootstrap.Modal(document.getElementById('modal-container'), {backdrop: 'static', keyboard: false});"
+ (
"conreq_modal.show();"
if state.modal_state._show
if state.modal_state.show
else "conreq_modal.hide();"
+ "if (document.querySelector('.modal-backdrop.show') && !document.querySelector('.modal.show')) {!document.querySelector('.modal-backdrop.show').remove();}"
+ "if (document.querySelector('.modal-backdrop.show') &&"
"!document.querySelector('.modal.show'))"
"{!document.querySelector('.modal-backdrop.show').remove();}"
)
),
)
Expand Down Expand Up @@ -79,7 +81,7 @@ def modal_head(state: HomepageState, set_state):
# pylint: disable=unused-argument

async def close_modal(_):
state.modal_state.set_show(False)
state.modal_state.show = False
set_state(state)

return div(
Expand Down
14 changes: 7 additions & 7 deletions conreq/_core/home/components/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def sidebar(state: HomepageState, set_state):
@idom.hooks.use_effect(dependencies=[])
async def set_initial_tab():
# The initial tab has already been set
if state._viewport or state._viewport_intent:
if state._viewport or state.viewport_intent:
return None

# Use the configured default tab, if it exists
if config.homepage.default_sidebar_tab:
state._viewport_intent = config.homepage.default_sidebar_tab.viewport
state.viewport_intent = config.homepage.default_sidebar_tab.viewport
set_state(state)
return None

Expand All @@ -83,18 +83,18 @@ async def set_initial_tab():
if not group.tabs or group in USER_ADMIN_DEBUG:
continue
tab: SidebarTab = group.tabs[0]
state._viewport_intent = tab.viewport
state.viewport_intent = tab.viewport
set_state(state)
return None

# Tell the user to install some apps, if they don't have any
state._viewport_intent = Viewport(welcome)
state.viewport_intent = Viewport(welcome)
set_state(state)

async def username_on_click(_):
if not config.tabs.user_settings.main:
return
state._viewport_intent = config.tabs.user_settings.main.viewport
state.viewport_intent = config.tabs.user_settings.main.viewport
set_state(state)

return nav(
Expand Down Expand Up @@ -194,9 +194,9 @@ async def on_click(event):
# Switch tabs
if tab.viewport:
# Reset loading state (only set by user defined viewports)
state._viewport_loading = False
state.viewport_loading = False

state.set_viewport(tab.viewport)
state.viewport_intent = tab.viewport
set_state(state)

return div(
Expand Down
4 changes: 2 additions & 2 deletions conreq/_core/home/components/viewport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def viewport_loading_animation(state: HomepageState, set_state):
VIEWPORT_CONTAINER_LOADING
| (
{}
if state._viewport_intent or state._viewport_loading
if state.viewport_intent or state.viewport_loading
else {"className": VIEWPORT_CONTAINER_LOADING["className"] + " hidden"}
)
),
Expand Down Expand Up @@ -51,7 +51,7 @@ def viewport_attrs(base_attrs, state: HomepageState, _viewport: Viewport):
new_attrs = base_attrs
new_attrs = (
new_attrs | HIDDEN
if state._viewport_intent or state._viewport_loading
if state.viewport_intent or state.viewport_loading
else new_attrs | {}
)
if not _viewport.padding:
Expand Down
2 changes: 1 addition & 1 deletion conreq/_core/home/components/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def welcome(state: HomepageState, set_state):
async def on_click(_):
# pylint: disable=protected-access
state._viewport_intent = config._homepage.admin_sidebar_tabs[1].viewport
state.viewport_intent = config._homepage.admin_sidebar_tabs[1].viewport
set_state(state)

return html.div(
Expand Down
4 changes: 3 additions & 1 deletion conreq/_core/user_settings/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from conreq._core.components import tabbed_viewport
from conreq._core.user_settings import views

# pylint: disable=unused-argument


@component
@auth_required
Expand Down Expand Up @@ -55,5 +57,5 @@ async def sign_out_event(
_, websocket: IdomWebsocket, state: HomepageState, set_state, tab
):
await logout(websocket.scope)
state._viewport_intent = Viewport(lambda *_: script("window.location.reload()"))
state.viewport_intent = Viewport(lambda *_: script("window.location.reload()"))
set_state(state)
44 changes: 10 additions & 34 deletions conreq/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from idom.core.types import VdomDict
from sortedcontainers import SortedSet

# pylint: disable=protected-access
# pylint: disable=protected-access, too-few-public-methods


class Icon(VdomDict):
Expand Down Expand Up @@ -100,64 +100,40 @@ class HTMLTemplate:

@dataclass
class ModalState:
_show: bool = False
# FIXME: Some options are not available until IDOM supports react-bootstrap
# Solution: https://github.com/idom-team/idom/issues/786

# _size: str = "lg"
# _centered: bool = True
# _kwargs: dict = field(default_factory=dict)

def set_show(self, show: bool):
self._show = show

# def set_size(self, size: str):
# self._size = size

# def set_centered(self, centered: bool):
# self._centered = centered

# def set_kwargs(self, kwargs: dict):
# self._kwargs = kwargs
show: bool = False
# size: str = "lg"
# centered: bool = True
# kwargs: dict = field(default_factory=dict)


@dataclass
class HomepageState:
_viewport_loading: bool = False
viewport_loading: bool = False
"""A toggle to manually set the viewport loading state. This is only used
by user defined viewport components."""

_viewport_intent: Viewport | None = None
viewport_intent: Viewport | None = None
"""The viewport that needs to be loaded."""

_viewport: Viewport | None = None
"""The currently visible viewport."""

_modal_intent: Callable | None = None
modal_intent: Callable | None = None
"""The modal that needs to be loaded."""

_modal: Callable | None = None
"""The currently visible modal."""

modal_state: ModalState = ModalState()
modal_state: ModalState = field(default_factory=ModalState)
"""The modal's current state object."""

def set_loading(self, loading: bool):
self._viewport_loading = loading

def set_viewport(self, viewport: Viewport):
self._viewport_intent = viewport

def set_modal(self, modal: Callable):
self._modal_intent = modal


@dataclass
class TabbedViewportState:
_tab: SubTab | None

def set_tab(self, tab: SubTab):
self._tab = tab
tab: SubTab | None


@dataclass
Expand Down

0 comments on commit f2b8871

Please sign in to comment.