@@ -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",
diff --git a/frontend/app/components/Domain/Recipe/RecipeExplorerPage/RecipeExplorerPageParts/RecipeExplorerPageSearch.vue b/frontend/app/components/Domain/Recipe/RecipeExplorerPage/RecipeExplorerPageParts/RecipeExplorerPageSearch.vue
index 6e4de25b8..e2c8af7bd 100644
--- a/frontend/app/components/Domain/Recipe/RecipeExplorerPage/RecipeExplorerPageParts/RecipeExplorerPageSearch.vue
+++ b/frontend/app/components/Domain/Recipe/RecipeExplorerPage/RecipeExplorerPageParts/RecipeExplorerPageSearch.vue
@@ -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"),
diff --git a/tests/unit_tests/repository_tests/test_pagination.py b/tests/unit_tests/repository_tests/test_pagination.py
index 2c7bbad4c..506ec34e3 100644
--- a/tests/unit_tests/repository_tests/test_pagination.py
+++ b/tests/unit_tests/repository_tests/test_pagination.py
@@ -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