Skip to content

Commit ba56bfb

Browse files
committed
Make ingredient autocompleter work with new search API
Note that this is a workaround, ideally we would properly support the new results and not just convert them. See wger-project/wger#1724
1 parent 7188ce8 commit ba56bfb

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@testing-library/react": "^14.2.1",
3838
"@testing-library/user-event": "^14.5.2",
3939
"@types/jest": "^29.5.12",
40+
"@types/lodash": "^4.17.6",
4041
"@types/luxon": "^3.4.2",
4142
"@types/node": "^20.14.9",
4243
"@types/react": "^18.3.3",

src/components/Nutrition/widgets/IngredientAutcompleter.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ export function IngredientAutocompleter({ callback, initialIngredient }: Ingredi
5151
const [t, i18n] = useTranslation();
5252

5353
const fetchName = useMemo(
54-
() =>
55-
throttle(
56-
(request: string) => searchIngredient(request, i18n.language, searchEnglish).then(res => setOptions(res)),
57-
200,
58-
),
54+
() => throttle(
55+
(request: string) => searchIngredient(request, i18n.language, searchEnglish).then(res => setOptions(res)),
56+
200,
57+
),
5958
[i18n.language, searchEnglish],
6059
);
6160

@@ -88,6 +87,7 @@ export function IngredientAutocompleter({ callback, initialIngredient }: Ingredi
8887
noOptionsText={t('noResults')}
8988
isOptionEqualToValue={(option, value) => option.value === value.value}
9089
onChange={(event: any, newValue: IngredientSearchResponse | null) => {
90+
9191
setOptions(newValue ? [newValue, ...options] : options);
9292
setValue(newValue);
9393
callback(newValue);

src/services/ingredient.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22
import { Ingredient, IngredientAdapter } from "components/Nutrition/models/Ingredient";
3-
import { IngredientSearchResponse, IngredientSearchType } from "services/responseType";
3+
import { IngredientSearchResponse } from "services/responseType";
44
import { ApiIngredientType } from 'types';
55
import { ApiPath, LANGUAGE_SHORT_ENGLISH } from "utils/consts";
66
import { fetchPaginated } from "utils/requests";
@@ -9,7 +9,7 @@ import { makeHeader, makeUrl } from "utils/url";
99

1010
export const getIngredient = async (id: number): Promise<Ingredient> => {
1111
const { data: receivedIngredient } = await axios.get<ApiIngredientType>(
12-
makeUrl(ApiPath.INGREDIENT_PATH, { id: id }),
12+
makeUrl(ApiPath.INGREDIENTINFO_PATH, { id: id }),
1313
{ headers: makeHeader() },
1414
);
1515

@@ -25,7 +25,7 @@ export const getIngredients = async (ids: number[]): Promise<Ingredient[]> => {
2525
}
2626

2727
// eslint-disable-next-line camelcase
28-
const url = makeUrl(ApiPath.INGREDIENT_PATH, { query: { id__in: ids.join(',') } });
28+
const url = makeUrl(ApiPath.INGREDIENTINFO_PATH, { query: { id__in: ids.join(',') } });
2929
const adapter = new IngredientAdapter();
3030
const out: Ingredient[] = [];
3131

@@ -41,16 +41,32 @@ export const getIngredients = async (ids: number[]): Promise<Ingredient[]> => {
4141

4242

4343
export const searchIngredient = async (name: string, languageCode: string, searchEnglish: boolean = true): Promise<IngredientSearchResponse[]> => {
44+
// TODO: this currently only converts the results from the new API to the old format
45+
// but this should be properly converted.
46+
// See also https://github.com/wger-project/wger/pull/1724
47+
48+
4449
const languages = [languageCode];
4550
if (languageCode !== LANGUAGE_SHORT_ENGLISH && searchEnglish) {
4651
languages.push(LANGUAGE_SHORT_ENGLISH);
4752
}
4853

4954
const url = makeUrl(
50-
ApiPath.INGREDIENT_SEARCH_PATH,
51-
{ query: { term: name, language: languages.join(',') } }
55+
ApiPath.INGREDIENTINFO_PATH,
56+
{ query: { name__search: name, language__codes: languages.join(',') } }
5257
);
5358

54-
const { data } = await axios.get<IngredientSearchType>(url);
55-
return data.suggestions;
59+
const { data } = await axios.get(url);
60+
61+
return data.results.map((entry: any) => (
62+
{
63+
value: entry.name,
64+
data: {
65+
id: entry.id,
66+
name: entry.name,
67+
image: entry.image?.image,
68+
image_thumbnail: entry.image?.image,
69+
}
70+
}
71+
));
5672
};

src/services/responseType.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,3 @@ export interface IngredientSearchResponse {
3030
image_thumbnail: string | null,
3131
}
3232
}
33-
34-
export interface IngredientSearchType {
35-
suggestions: IngredientSearchResponse[];
36-
}

src/utils/consts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export enum ApiPath {
5252
MEAL = 'meal',
5353
MEAL_ITEM = 'mealitem',
5454
NUTRITIONAL_DIARY = 'nutritiondiary',
55-
INGREDIENT_PATH = 'ingredientinfo',
55+
INGREDIENTINFO_PATH = 'ingredientinfo',
5656
INGREDIENT_SEARCH_PATH = 'ingredient/search',
5757
INGREDIENT_WEIGHT_UNIT = 'ingredientweightunit'
5858
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,11 @@
23122312
resolved "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz"
23132313
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
23142314

2315+
"@types/lodash@^4.17.6":
2316+
version "4.17.6"
2317+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.6.tgz#193ced6a40c8006cfc1ca3f4553444fb38f0e543"
2318+
integrity sha512-OpXEVoCKSS3lQqjx9GGGOapBeuW5eUboYHRlHP9urXPX25IKZ6AnP5ZRxtVf63iieUbsHxLn8NQ5Nlftc6yzAA==
2319+
23152320
"@types/luxon@^3.4.2":
23162321
version "3.4.2"
23172322
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-3.4.2.tgz#e4fc7214a420173cea47739c33cdf10874694db7"

0 commit comments

Comments
 (0)