From b1b77560428da35957cbaf869f1429d56079eb42 Mon Sep 17 00:00:00 2001 From: Eason <30045503+Eason0729@users.noreply.github.com> Date: Fri, 5 Apr 2024 03:08:41 +0000 Subject: [PATCH] feat(frontend): using None for default value --- frontend/src/pages/problems.rs | 61 ++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/frontend/src/pages/problems.rs b/frontend/src/pages/problems.rs index baafc51..172ff04 100644 --- a/frontend/src/pages/problems.rs +++ b/frontend/src/pages/problems.rs @@ -73,6 +73,15 @@ impl From for Pager { } } +macro_rules! on_option{ + ($e:expr,$t:ident) =>{ + match ($e as $t)==$t::default(){ + true=>None, + false=>Some($e as $t) + } + } +} + impl From for RawPager { fn from(value: Pager) -> Self { let mut text = None; @@ -83,15 +92,14 @@ impl From for RawPager { _ => {} }; - /// FIXME: use None on default value to shorten url Self { s: value.session, - o: Some(value.offset), - d: Some(value.direction as u8), + o: on_option!(value.offset,u64), + d: on_option!(value.direction, u8), text, sort_by, - e: Some(value.start_from_end as u8), - p: Some(value.page_number), + e: on_option!(value.start_from_end,u8), + p: on_option!(value.page_number,u64), } } } @@ -101,6 +109,22 @@ impl Pager { let raw: RawPager = self.into(); ["?", &*serde_qs::to_string(&raw).unwrap()].concat() } + fn text_search(text:String)->Self{ + Self{ + deps:SearchDeps::Text(text), + ..Default::default() + } + } + fn column_search(col:ProblemSortBy)->Self{ + Self{ + deps:SearchDeps::Column(col), + .. Default::default() + } + } + fn from_end(mut self, start_from_end:bool) ->Self{ + self.start_from_end=start_from_end; + self + } fn get() -> Memo { Memo::new(move |_| { use_query::() @@ -275,6 +299,31 @@ impl RenderInfo { } } +#[component] +pub fn ProblemSearch()->impl IntoView{ + let filter_text=create_rw_signal("".to_owned()); + let start_from_end=create_rw_signal(false); + let sort_by=create_rw_signal(ProblemSortBy::AcRate as i32); + + let submit=Memo::new(move |_|{ + let start_from_end =start_from_end.get(); + let text=filter_text.get(); + let sort_by:ProblemSortBy=sort_by.get().try_into().unwrap_or(ProblemSortBy::UpdateDate); + + let pager=match text.is_empty(){ + true=> Pager::column_search(sort_by), + false=>Pager::text_search(text) + }.from_end(start_from_end); + let query=pager.into_query(); + ["/problems",&query].concat() + }); + + view!{ +
+ +
+ } +} #[component] pub fn ProblemList() -> impl IntoView { fn difficulty_color(difficulty: u32) -> impl IntoView { @@ -342,7 +391,7 @@ pub fn ProblemList() -> impl IntoView { v.next_queries.into_iter().enumerate() .map(|(n,query)|view!{
  • - back {n+1} page + next {n+1} page
  • }).collect_view() }