Skip to content

Commit 4772852

Browse files
JonasdoubleyouJonas Wilms
andauthored
Add default ingredient list (#23)
* Add a basic card component * Basic card for drinks * Prevent empty submits * Add list of default ingredients * Properly load ingredients to ensure identity Co-authored-by: Jonas Wilms <unknown@example.org>
1 parent c185501 commit 4772852

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

frontend/src/searchform/IngredientList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { IIngredient } from "../../../types";
55

66
import * as API from "../api";
77

8-
const loadAllIngredients = API.getIngredients();
8+
export const loadAllIngredients = API.getIngredients();
9+
910

1011
const normalize = (str: string) =>
1112
str.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();

frontend/src/searchform/searchform.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import * as React from 'react';
22
import './searchform.css';
33

4-
import { IngredientList } from './IngredientList';
4+
import { IngredientList, loadAllIngredients } from './IngredientList';
55
import { IIngredient } from "../../../types";
66
import { useTipsySelector } from '../tipsyselector/tipsyselector';
77

8+
const DEFAULT_INGREDIENTS = [
9+
"carbonated water",
10+
"coffee",
11+
"cola",
12+
"crushed ice",
13+
"cow's milk",
14+
"drinking water",
15+
"ice",
16+
"sugar",
17+
"milk",
18+
"water",
19+
];
20+
821
function SearchForm({
922
submit
1023
}: {
@@ -14,6 +27,13 @@ function SearchForm({
1427
const [ingredients, setIngredients] = React.useState<IIngredient[]>([]);
1528
const [tipsySelectorEl, promille] = useTipsySelector({ rangeOptions: { min: .3, max: 2 } });
1629

30+
// If the ingredients were loaded before the user started adding some,
31+
// prefill the ingredient list with something basically everyone has
32+
React.useEffect(() => {
33+
loadAllIngredients
34+
.then(result => setIngredients(prev => prev.length ? prev : result.ingredients.filter(it => DEFAULT_INGREDIENTS.includes(it.name))));
35+
}, []);
36+
1737
return (
1838
<>
1939
<div className="segment">

0 commit comments

Comments
 (0)