Skip to content

Commit

Permalink
fix(recipe): display default servings value
Browse files Browse the repository at this point in the history
  • Loading branch information
stantanasi committed Aug 17, 2024
1 parent df2b436 commit 1a66e65
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions screens/recipe/RecipeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Props = NativeStackScreenProps<RootStackParamList, 'Recipe'>;

export default function RecipeScreen({ navigation, route }: Props) {
const [recipe, setRecipe] = useState<RecipeModel | null>()
const [portionSize, setPortionSize] = useState(recipe?.servings ?? 0)
const [servings, setServings] = useState(recipe?.servings ?? 0)

if (recipe === null) {
navigation.replace('NotFound')
Expand All @@ -21,7 +21,10 @@ export default function RecipeScreen({ navigation, route }: Props) {

useEffect(() => {
RecipeModel.findById(route.params.id)
.then((data) => setRecipe(data))
.then((data) => {
setRecipe(data)
setServings(data?.servings ?? 0)
})
}, [])

return (
Expand Down Expand Up @@ -66,18 +69,18 @@ export default function RecipeScreen({ navigation, route }: Props) {
<Text style={styles.sectionTitle}>Ingrédients</Text>

<View style={styles.servings}>
<Pressable onPress={() => setPortionSize((prev) => prev - 1)}>
<Pressable onPress={() => setServings((prev) => prev - 1)}>
<Text style={[styles.servingsButton, { borderRightColor: '#333', borderRightWidth: 2 }]}>
-
</Text>
</Pressable>
<TextInput
value={portionSize.toString()}
onChangeText={(value) => setPortionSize(+value.replace(/[^0-9]/g, ''))}
value={servings.toString()}
onChangeText={(value) => setServings(+value.replace(/[^0-9]/g, ''))}
keyboardType='numeric'
style={styles.servingsButton}
/>
<Pressable onPress={() => setPortionSize((prev) => prev + 1)}>
<Pressable onPress={() => setServings((prev) => prev + 1)}>
<Text style={[styles.servingsButton, { borderLeftColor: '#333', borderLeftWidth: 2 }]}>
+
</Text>
Expand All @@ -86,7 +89,7 @@ export default function RecipeScreen({ navigation, route }: Props) {

<Ingredients
recipe={recipe}
portionFactor={portionSize / recipe.servings}
portionFactor={servings / recipe.servings}
/>
</View>

Expand Down

0 comments on commit 1a66e65

Please sign in to comment.