Skip to content

Commit

Permalink
fix: don't let the default override any use made changes on "back"
Browse files Browse the repository at this point in the history
So far, we did store the page state in the browser history. However,
when unpacking it (on hitting "back"), the defaults did override any
selection made by the user so far.

This change fixes this by tracking both the "modification" state as well
as the "defaults" which need to get applied. Also when the user
resets the search.
  • Loading branch information
ctron committed Nov 9, 2023
1 parent 6b43f3a commit 2ab804e
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 73 deletions.
13 changes: 7 additions & 6 deletions spog/ui/crates/components/src/advisory/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use yew_more_hooks::prelude::*;

#[derive(PartialEq, Properties)]
pub struct AdvisorySearchControlsProperties {
pub search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
pub search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
}

#[function_component(AdvisorySearchControls)]
Expand All @@ -39,7 +39,7 @@ pub fn advisory_search_controls(props: &AdvisorySearchControlsProperties) -> Htm

#[hook]
pub fn use_advisory_search(
search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
pagination: UsePagination,
callback: Callback<UseAsyncHandleDeps<SearchResult<Rc<Vec<AdvisorySummary>>>, String>>,
) -> UseStandardSearch {
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct AdvisorySearchProperties {
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
struct PageState {
pagination: PaginationControl,
search_params: SearchMode<DynamicSearchParameters>,
search_params: HistorySearchState<DynamicSearchParameters>,
}

#[function_component(AdvisorySearch)]
Expand All @@ -87,11 +87,12 @@ pub fn advisory_search(props: &AdvisorySearchProperties) -> Html {
search_params: match props.query.as_ref().filter(|s| !s.is_empty()) {
Some(terms) => SearchMode::Complex(terms.clone()),
None => Default::default(),
},
}
.into(),
..Default::default()
});

let search_params = use_reducer_eq(|| page_state.search_params.clone());
let search_params = use_reducer_eq(|| SearchState::from(page_state.search_params.clone()));
let total = use_state_eq(|| None);
let pagination = use_pagination(*total, || page_state.pagination);
let state = use_state_eq(UseAsyncState::default);
Expand All @@ -117,7 +118,7 @@ pub fn advisory_search(props: &AdvisorySearchProperties) -> Html {
page_state,
PageState {
pagination: **pagination,
search_params: (*search_params).clone(),
search_params: (*search_params).clone().into(),
},
);

Expand Down
15 changes: 9 additions & 6 deletions spog/ui/crates/components/src/cve/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use yew_more_hooks::prelude::*;

#[derive(PartialEq, Properties)]
pub struct CveSearchControlsProperties {
pub search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
pub search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
}

#[function_component(CveSearchControls)]
Expand All @@ -39,7 +39,7 @@ pub fn cve_search_controls(props: &CveSearchControlsProperties) -> Html {

#[hook]
pub fn use_cve_search(
search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
pagination: UsePagination,
callback: Callback<UseAsyncHandleDeps<SearchResult<Rc<Vec<CveSearchDocument>>>, String>>,
) -> UseStandardSearch {
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct CveSearchProperties {
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
struct PageState {
pagination: PaginationControl,
search_params: SearchMode<DynamicSearchParameters>,
search_params: HistorySearchState<DynamicSearchParameters>,
}

#[function_component(CveSearch)]
Expand All @@ -87,11 +87,14 @@ pub fn cve_search(props: &CveSearchProperties) -> Html {
search_params: match props.query.as_ref().filter(|s| !s.is_empty()) {
Some(terms) => SearchMode::Complex(terms.clone()),
None => Default::default(),
},
}
.into(),
..Default::default()
});

let search_params = use_reducer_eq::<SearchMode<DynamicSearchParameters>, _>(|| page_state.search_params.clone());
let search_params = use_reducer_eq::<SearchState<DynamicSearchParameters>, _>(|| {
SearchState::from(page_state.search_params.clone())
});
let total = use_state_eq(|| None);
let pagination = use_pagination(*total, || page_state.pagination);
let state = use_state_eq(UseAsyncState::default);
Expand All @@ -117,7 +120,7 @@ pub fn cve_search(props: &CveSearchProperties) -> Html {
page_state,
PageState {
pagination: **pagination,
search_params: (*search_params).clone(),
search_params: (*search_params).clone().into(),
},
);

Expand Down
14 changes: 7 additions & 7 deletions spog/ui/crates/components/src/hooks/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::search::{DynamicSearchParameters, SearchMode, SearchModeAction};
use crate::search::{DynamicSearchParameters, SearchMode, SearchModeAction, SearchState};
use patternfly_yew::prelude::*;
use spog_model::config::Filters;
use spog_ui_backend::{use_backend, Backend};
Expand All @@ -11,7 +11,7 @@ use yew_oauth2::prelude::{use_latest_access_token, LatestAccessToken};

#[derive(Clone)]
pub struct UseStandardSearch {
pub search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
pub search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
pub filter_input_state: Rc<InputState>,
pub onclear: Callback<()>,
pub onset: Callback<()>,
Expand All @@ -21,14 +21,14 @@ pub struct UseStandardSearch {

#[hook]
pub fn use_standard_search<S>(
search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
context: Rc<Filters>,
) -> UseStandardSearch
where
S: sikula::prelude::Search,
{
// the current value in the text input field
let text = use_state_eq(|| match &*search_params {
let text = use_state_eq(|| match &**search_params {
SearchMode::Complex(s) => s.clone(),
SearchMode::Simple(s) => s.terms().join(" "),
});
Expand All @@ -52,7 +52,7 @@ where
let onclear = use_callback((text.clone(), search_params.clone()), |_, (text, search_params)| {
text.set(String::new());
// trigger empty search
match **search_params {
match ***search_params {
SearchMode::Complex(_) => search_params.dispatch(SearchModeAction::Clear),
SearchMode::Simple(_) => search_params.dispatch(SearchModeAction::Clear),
}
Expand Down Expand Up @@ -94,13 +94,13 @@ pub struct SearchOperationContext {
pub access_token: Option<LatestAccessToken>,
pub page: usize,
pub per_page: usize,
pub search_params: SearchMode<DynamicSearchParameters>,
pub search_params: SearchState<DynamicSearchParameters>,
pub filters: Rc<Filters>,
}

#[hook]
pub fn use_generic_search<S, R, F, Fut, IF>(
search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
pagination: UsePagination,
callback: Callback<UseAsyncHandleDeps<R, String>>,
init_filter: IF,
Expand Down
15 changes: 9 additions & 6 deletions spog/ui/crates/components/src/packages/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use yew_more_hooks::prelude::*;

#[derive(PartialEq, Properties)]
pub struct PackageSearchControlsProperties {
pub search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
pub search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
}

#[function_component(PackageSearchControls)]
Expand All @@ -39,7 +39,7 @@ pub fn packages_search_controls(props: &PackageSearchControlsProperties) -> Html

#[hook]
pub fn use_package_search(
search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
pagination: UsePagination,
callback: Callback<UseAsyncHandleDeps<SearchResult<Rc<Vec<PackageInfoSummary>>>, String>>,
) -> UseStandardSearch {
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct PackageSearchProperties {
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
struct PageState {
pagination: PaginationControl,
search_params: SearchMode<DynamicSearchParameters>,
search_params: HistorySearchState<DynamicSearchParameters>,
}

#[function_component(PackagesSearch)]
Expand All @@ -87,11 +87,14 @@ pub fn packages_search(props: &PackageSearchProperties) -> Html {
search_params: match props.query.as_ref().filter(|s| !s.is_empty()) {
Some(terms) => SearchMode::Complex(terms.clone()),
None => Default::default(),
},
}
.into(),
..Default::default()
});

let search_params = use_reducer_eq::<SearchMode<DynamicSearchParameters>, _>(|| page_state.search_params.clone());
let search_params = use_reducer_eq::<SearchState<DynamicSearchParameters>, _>(|| {
SearchState::from(page_state.search_params.clone())
});
let total = use_state_eq(|| None);
let pagination = use_pagination(*total, || page_state.pagination);
let state = use_state_eq(UseAsyncState::default);
Expand All @@ -117,7 +120,7 @@ pub fn packages_search(props: &PackageSearchProperties) -> Html {
page_state,
PageState {
pagination: **pagination,
search_params: (*search_params).clone(),
search_params: (*search_params).clone().into(),
},
);

Expand Down
15 changes: 9 additions & 6 deletions spog/ui/crates/components/src/sbom/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use yew_more_hooks::prelude::*;

#[derive(PartialEq, Properties)]
pub struct SbomSearchControlsProperties {
pub search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
pub search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
}

#[function_component(SbomSearchControls)]
Expand All @@ -36,7 +36,7 @@ pub fn sbom_search_controls(props: &SbomSearchControlsProperties) -> Html {

#[hook]
pub fn use_sbom_search<S>(
search_params: UseReducerHandle<SearchMode<DynamicSearchParameters>>,
search_params: UseReducerHandle<SearchState<DynamicSearchParameters>>,
pagination: UsePagination,
callback: Callback<UseAsyncHandleDeps<SearchResult<Rc<Vec<SbomSummary>>>, String>>,
f: S,
Expand Down Expand Up @@ -80,7 +80,7 @@ pub struct SbomSearchProperties {
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
struct PageState {
pagination: PaginationControl,
search_params: SearchMode<DynamicSearchParameters>,
search_params: HistorySearchState<DynamicSearchParameters>,
}

#[function_component(SbomSearch)]
Expand All @@ -89,11 +89,14 @@ pub fn sbom_search(props: &SbomSearchProperties) -> Html {
search_params: match props.query.as_ref().filter(|s| !s.is_empty()) {
Some(terms) => SearchMode::Complex(terms.clone()),
None => Default::default(),
},
}
.into(),
..Default::default()
});

let search_params = use_reducer_eq::<SearchMode<DynamicSearchParameters>, _>(|| page_state.search_params.clone());
let search_params = use_reducer_eq::<SearchState<DynamicSearchParameters>, _>(|| {
SearchState::from(page_state.search_params.clone())
});
let total = use_state_eq(|| None);
let pagination = use_pagination(*total, || page_state.pagination);
let state = use_state_eq(UseAsyncState::default);
Expand Down Expand Up @@ -121,7 +124,7 @@ pub fn sbom_search(props: &SbomSearchProperties) -> Html {
page_state,
PageState {
pagination: **pagination,
search_params: (*search_params).clone(),
search_params: (*search_params).clone().into(),
},
);

Expand Down
Loading

0 comments on commit 2ab804e

Please sign in to comment.