Skip to content

Commit

Permalink
URL search params reflecting filters
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddavo committed Jul 19, 2022
1 parent b2b942b commit 2bcb59c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ def pred(self, org: Organization) -> bool:

class OrganizationFilter(Filter, metaclass=abc.ABCMeta):

def __init__(self, id, title, enabled):
def __init__(self, id, title, default):
self._id = id
self._title = title
self._enabled = enabled
self._default = default
self._enabled = default

@property
def id(self) -> str:
Expand All @@ -40,6 +41,10 @@ def title(self) -> str:
def enabled(self) -> bool:
return self._enabled

@property
def default(self) -> bool:
return self._default

@enabled.setter
def enabled(self, value: bool) -> None:
self._enabled = bool(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,31 @@ def get_networks(self) -> Set[str]:
return set((x.get_network().lower() for x in self))

@staticmethod
def get_filters(values=None, only_enabled=False, force_disabled=False) -> List[Filter]:
def get_filters(
values: List[str] = None,
only_enabled: bool = False,
force_disabled: bool = False,
diff: bool = False
) -> List[Filter]:
"""Gets the filters available for an OrganizationList
Args:
values (List[str], optional): The ids of the filters to set to enable, if None returns default values. Defaults to None.
only_enabled (bool, optional): When True, returns only the enabled filters. Defaults to False.
force_disabled (bool, optional): When True, forces all filters to the disabled state. Defaults to False.
diff (bool, optional): When True, values is the ids of the filters to change to the non-default value (see :func:`~OrganizationList.get_diff_filters`). Defaults to False.
Returns:
List[Filter]: The list of selected filters.
"""
filters = [f() for f in SIMPLE_FILTERS]

if values is not None:
for f in filters:
f.enabled = f.id in values
if not diff:
f.enabled = f.id in values
elif diff and f.id in values:
f.enabled = not f.default

if force_disabled:
for f in filters:
Expand All @@ -99,6 +118,11 @@ def get_filters(values=None, only_enabled=False, force_disabled=False) -> List[F
filters = [f for f in filters if f.enabled]

return filters

@staticmethod
def get_diff_filters(values=None):
""" Return filters which the value is different from the default """
return [f for f in OrganizationList.get_filters(values) if f.enabled != f.default]

@staticmethod
def get_filter_group(*args, **kwargs) -> OrganizationFilterGroup:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def generate_layout(
platform_id: str,
org_value: str,
network_value: str,
filter_values: List[str],
) -> List:
"""
Use this function to generate the app view.
Expand All @@ -49,7 +50,7 @@ def generate_layout(

return html.Div([
dbc.Container(
__generate_header(organization_list, ecosystem, update, org_value, network_value),
__generate_header(organization_list, ecosystem, update, org_value, network_value, filter_values),
className='top body mb-3 py-4'),
dbc.Container([
__generate_subheader(platform_id, datapoints),
Expand All @@ -71,6 +72,7 @@ def __generate_header(
update: str,
org_value: str,
network_value: str,
filter_values: List[str],
) -> dbc.Row:
selected: List[str] = __ECOSYSTEM_SELECTED['default']
if ecosystem in __ECOSYSTEM_SELECTED.keys():
Expand All @@ -85,7 +87,9 @@ def __generate_header(
# If we don't disable the filter, the DAO will be inmediately filtered out
# Fixes #85
filterGroup: OrganizationFilterGroup = organization_list.get_filter_group(
force_disabled=not OrganizationList.is_all_orgs(org_value)
values=filter_values,
force_disabled=not OrganizationList.is_all_orgs(org_value),
diff=True,
)

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ def _process_params(search: str) -> Dict[str, str]:
k,v = p.split('=', 1) # Split only on first ocurrence
params[k] = v

if 'filters' in params:
params['filters'] = params['filters'].split(',')

return params

def _params_string(d: Dict[str, str]):
if not d:
return ""


if 'filters' in d:
d['filters'] = ','.join(d['filters'])

return '?' + '&'.join(['='.join([k,v]) for k,v in d.items()])

def bind_callbacks(app) -> None: # noqa: C901
Expand All @@ -61,15 +67,21 @@ def bind_callbacks(app) -> None: # noqa: C901
Output('header-loading-state', 'children'), # <- THIS CAUSES THE FLASH
Input('page-content', 'data-subpage'),
State('page-content', 'data-org-id'),
State('url', 'search')
State('url', 'search'),
)
def change_subpage(subpage, org_id, search):
if not subpage:
return dcc.Location(pathname='/daohaus', id='default_redirect'), 'redirect'
elif subpage == ABOUT_SUBPAGE:
return generate_layout(body=about.get_layout()), ''
elif subpage in services:
return generate_layout(body=services[subpage].get_layout(org_value=org_id, network_value=_process_params(search).get('network', ''))), 'loading'
params = _process_params(search)

return generate_layout(body=services[subpage].get_layout(
org_value=org_id,
network_value=params.get('network', None),
filter_values=params.get('filters', []),
)), 'loading'

@app.callback(
Output('page-content', 'data-subpage'),
Expand Down Expand Up @@ -160,7 +172,11 @@ def org_filters(filter_values: List[str], network_value: str, org_value: str, or
params = {}

if network_value != ALL_NETWORKS_VALUE:
params = {'network': network_value}
params['network'] = network_value

ndf = OrganizationList.get_diff_filters(filter_values)
if ndf:
params['filters'] = [ f.id for f in ndf ]

# If the selected DAO was filtered out, fall back to All DAOs
if org_value in [ x['value'] for x in options ]:
Expand Down
5 changes: 2 additions & 3 deletions dao_analyzer/apps/daohaus/business/app_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def are_panes(self) -> bool:
return any(self.__controllers.values())


def get_layout(self, org_value: str=None, network_value: str = None) -> html.Div:
def get_layout(self, **kwargs) -> html.Div:
"""
Returns the app's layout.
"""
Expand All @@ -96,9 +96,8 @@ def get_layout(self, org_value: str=None, network_value: str = None) -> html.Div
ecosystem='daohaus',
update=self.__orgsDAO.get_last_update_str(),
platform_id=TEXT['css_id_organization'],
org_value=org_value,
datapoints=self.__get_datapoints(),
network_value=network_value,
**kwargs,
)


Expand Down

0 comments on commit 2bcb59c

Please sign in to comment.