Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the "sort by" value switch to default value when use paginate #2352

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions src/web/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use axum::{
use base64::{engine::general_purpose::STANDARD as b64, Engine};
use chrono::{DateTime, Utc};
use futures_util::stream::TryStreamExt;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use sqlx::Row;
use std::collections::{BTreeMap, HashMap, HashSet};
Expand Down Expand Up @@ -156,7 +157,6 @@ async fn get_search_results(
}

use crate::utils::APP_USER_AGENT;
use once_cell::sync::Lazy;
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, USER_AGENT};
use reqwest::Client as HttpClient;

Expand Down Expand Up @@ -509,7 +509,7 @@ pub(crate) async fn search_handler(
.get("query")
.map(|q| q.to_string())
.unwrap_or_else(|| "".to_string());
let sort_by = params
let mut sort_by = params
.get("sort")
.map(|q| q.to_string())
.unwrap_or_else(|| "relevance".to_string());
Expand Down Expand Up @@ -579,6 +579,20 @@ pub(crate) async fn search_handler(
return Err(AxumNope::NoResults);
}

let p = form_urlencoded::parse(query_params.as_bytes());
if let Some(v) = p
.filter_map(|(k, v)| {
if &k == "sort" {
Some(v.to_string())
} else {
None
}
})
.next()
{
sort_by = v;
};

get_search_results(&mut conn, &config, &query_params).await?
} else if !query.is_empty() {
let query_params: String = form_urlencoded::Serializer::new(String::new())
Expand Down Expand Up @@ -880,6 +894,64 @@ mod tests {
})
}

#[test]
fn search_result_can_retrive_sort_by_from_pagination() {
wrapper(|env| {
let mut crates_io = mockito::Server::new();
env.override_config(|config| {
config.registry_api_host = crates_io.url().parse().unwrap();
});

let web = env.frontend();
env.fake_release().name("some_random_crate").create()?;

let _m = crates_io
.mock("GET", "/api/v1/crates")
.match_query(Matcher::AllOf(vec![
Matcher::UrlEncoded("q".into(), "some_random_crate".into()),
Matcher::UrlEncoded("per_page".into(), "30".into()),
Matcher::UrlEncoded("page".into(), "2".into()),
Matcher::UrlEncoded("sort".into(), "recent-updates".into()),
]))
.with_status(200)
.with_header("content-type", "application/json")
.with_body(
json!({
"crates": [
{ "name": "some_random_crate" },
],
"meta": {
"next_page": "?q=some_random_crate&sort=recent-updates&per_page=30&page=2",
"prev_page": "?q=some_random_crate&sort=recent-updates&per_page=30&page=1",
}
})
.to_string(),
)
.create();

// click the "Next Page" Button, the "Sort by" SelectBox should keep the same option.
let next_page_url = format!(
"/releases/search?paginate={}",
b64.encode("?q=some_random_crate&sort=recent-updates&per_page=30&page=2"),
);
let response = web.get(&next_page_url).send()?;
assert!(response.status().is_success());

let page = kuchikiki::parse_html().one(response.text()?);
let is_target_option_selected = page
.select("#nav-sort > option")
.expect("missing option")
.any(|el| {
let attributes = el.attributes.borrow();
attributes.get("selected").is_some()
&& attributes.get("value").unwrap() == "recent-updates"
});
assert!(is_target_option_selected);

Ok(())
})
}

#[test]
fn search_result_passes_cratesio_pagination_links() {
wrapper(|env| {
Expand Down
Loading