feat: Sort recipes by total time (#8692)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f16433dc66
commit
780c9db722
3 files changed
+68
-1
No files matched your search
@@ -83,6 +83,14 @@
|
||||
<v-list-item-title>{{ $t("general.last-made") }}</v-list-item-title>
|
||||
</div>
|
||||
</v-list-item>
|
||||
<v-list-item @click="sortRecipes(EVENTS.totalTime)">
|
||||
<div class="d-flex align-center flex-nowrap">
|
||||
<v-icon class="mr-2" inline>
|
||||
{{ $globals.icons.clockOutline }}
|
||||
</v-icon>
|
||||
<v-list-item-title>{{ $t("recipe.total-time") }}</v-list-item-title>
|
||||
</div>
|
||||
</v-list-item>
|
||||
<v-list-item @click="sortRecipes(EVENTS.shuffle)">
|
||||
<div class="d-flex align-center flex-nowrap">
|
||||
<v-icon class="mr-2" inline>
|
||||
@@ -211,9 +219,13 @@ const EVENTS = {
|
||||
created: "created",
|
||||
updated: "updated",
|
||||
lastMade: "lastMade",
|
||||
totalTime: "totalTime",
|
||||
shuffle: "shuffle",
|
||||
};
|
||||
|
||||
// recipes without a value can't be ranked by these, so they always go to the end regardless of direction
|
||||
const NULLS_LAST_ORDER_BY = ["total_time_seconds"];
|
||||
|
||||
const auth = useMealieAuth();
|
||||
const { $globals } = useNuxtApp();
|
||||
const { isOwnGroup } = useLoggedInState();
|
||||
@@ -275,8 +287,9 @@ const queryFilter = computed(() => {
|
||||
|
||||
async function fetchRecipes(pageCount = 1) {
|
||||
const orderDir = props.query?.orderDirection || preferences.value.orderDirection;
|
||||
const orderByNullPosition = props.query?.orderByNullPosition || orderDir === "asc" ? "first" : "last";
|
||||
const orderBy = effectiveOrderBy.value;
|
||||
const orderByNullPosition = props.query?.orderByNullPosition
|
||||
|| (NULLS_LAST_ORDER_BY.includes(orderBy) || orderDir !== "asc" ? "last" : "first");
|
||||
const localQuery = { ...props.query };
|
||||
if (orderBy === "random") {
|
||||
localQuery._searchSeed = randomSeed.value;
|
||||
@@ -435,6 +448,15 @@ async function sortRecipes(sortType: string) {
|
||||
true,
|
||||
);
|
||||
break;
|
||||
case EVENTS.totalTime:
|
||||
setter(
|
||||
"total_time_seconds",
|
||||
$globals.icons.sortAscending,
|
||||
$globals.icons.sortDescending,
|
||||
"asc",
|
||||
true,
|
||||
);
|
||||
break;
|
||||
case EVENTS.shuffle:
|
||||
setter(
|
||||
"random",
|
||||
|
||||
+5
@@ -205,6 +205,11 @@ const sortable = computed(() => [
|
||||
name: i18n.t("general.rating"),
|
||||
value: "rating",
|
||||
},
|
||||
{
|
||||
icon: $globals.icons.clockOutline,
|
||||
name: i18n.t("recipe.total-time"),
|
||||
value: "total_time_seconds",
|
||||
},
|
||||
{
|
||||
icon: $globals.icons.update,
|
||||
name: i18n.t("general.updated"),
|
||||
|
||||
@@ -998,6 +998,46 @@ def test_pagination_order_by_nulls(
|
||||
assert query.items[1] == food_without_label
|
||||
|
||||
|
||||
@pytest.mark.parametrize("order_direction", [OrderDirection.asc, OrderDirection.desc], ids=["asc", "desc"])
|
||||
def test_pagination_order_recipes_by_total_time_nulls_last(
|
||||
api_client: TestClient, unique_user_fn_scoped: TestUser, order_direction: OrderDirection
|
||||
):
|
||||
unique_user = unique_user_fn_scoped
|
||||
times = [3600, None, 600, 5400, None, 1800]
|
||||
recipes = [
|
||||
unique_user.repos.recipes.create(
|
||||
Recipe(
|
||||
user_id=unique_user.user_id,
|
||||
group_id=unique_user.group_id,
|
||||
name=random_string(),
|
||||
total_time_seconds=seconds,
|
||||
)
|
||||
)
|
||||
for seconds in times
|
||||
]
|
||||
|
||||
params: dict[str, str | int] = {
|
||||
"page": 1,
|
||||
"perPage": -1,
|
||||
"orderBy": "total_time_seconds",
|
||||
"orderDirection": order_direction.value,
|
||||
"orderByNullPosition": OrderByNullPosition.last.value,
|
||||
}
|
||||
response = api_client.get(api_routes.recipes, params=params, headers=unique_user.token)
|
||||
assert response.status_code == 200
|
||||
items = response.json()["items"]
|
||||
assert len(items) == len(recipes)
|
||||
|
||||
with_time = sorted(
|
||||
(r for r in recipes if r.total_time_seconds is not None),
|
||||
key=lambda r: r.total_time_seconds or 0,
|
||||
reverse=order_direction is OrderDirection.desc,
|
||||
)
|
||||
expected_ids = [str(r.id) for r in with_time]
|
||||
assert [item["id"] for item in items[: len(with_time)]] == expected_ids
|
||||
assert [item["totalTimeSeconds"] for item in items[len(with_time) :]] == [None, None]
|
||||
|
||||
|
||||
def test_pagination_shopping_list_items_with_labels(unique_user: TestUser):
|
||||
database = unique_user.repos
|
||||
|
||||
|
||||
Reference in new issue
Block a user