Skip to content

Commit

Permalink
added filtering of dishes by diet
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Ruf committed Jul 19, 2024
1 parent 0715dd6 commit 13502c6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Resources/src/enums/Diet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ export enum Diet {
VEGETARIAN = 'vegetarian',
MEAT = 'meat'
}

export function getDietTranslationMap() {
const dietTranslationMap = new Map<string, string>();
dietTranslationMap.set('vegetarisch', 'vegetarian');
dietTranslationMap.set('vegan', 'vegan');
dietTranslationMap.set('fleisch', 'meat');
return dietTranslationMap;
}
10 changes: 8 additions & 2 deletions src/Resources/src/stores/dishesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { refThrottled } from '@vueuse/core';
import getDishesForCombi from '@/api/getDishesForCombi';
import { useCategories } from './categoriesStore';
import { isResponseArrayOkay, isResponseObjectOkay } from '@/api/isResponseOkay';
import { Diet } from '@/enums/Diet';
import { Diet, getDietTranslationMap } from '@/enums/Diet';

export interface Dish {
id: number;
Expand Down Expand Up @@ -100,16 +100,22 @@ export function useDishes() {
});

/**
* Determines wether a dish contains the search string in its title or in the title of one of its variations
* Determines wether a dish contains the search string in its title or in the title of one of its variations or in its diet
*/
function dishContainsString(dish: Dish, searchStr: string) {
return (
dish.titleDe.toLowerCase().includes(searchStr.toLowerCase()) ||
dish.titleEn.toLowerCase().includes(searchStr.toLowerCase()) ||
dishContainsDiet(dish, searchStr) ||
dish.variations.map((variation) => dishContainsString(variation, searchStr)).includes(true)
);
}

function dishContainsDiet(dish: Dish, searchStr: string) {
const translatedSearchStr = getDietTranslationMap().get(searchStr.toLowerCase().trim()) ?? searchStr.toLowerCase();
return dish.diet.toString().toLowerCase().includes(translatedSearchStr);
}

/**
* Fetches a list of dishes from the API and updates the DishesState
*/
Expand Down

0 comments on commit 13502c6

Please sign in to comment.