From 1ec51a5672a00615f17a59d2ac0b61f86d743b48 Mon Sep 17 00:00:00 2001 From: Rui Shan Date: Sun, 8 Oct 2023 09:38:56 +0800 Subject: [PATCH] Add no ingredients found --- pages/Recipe.js | 155 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 108 insertions(+), 47 deletions(-) diff --git a/pages/Recipe.js b/pages/Recipe.js index 89bde12..2af2c67 100644 --- a/pages/Recipe.js +++ b/pages/Recipe.js @@ -7,6 +7,7 @@ import { Formik, FieldArray } from "formik"; import { useAxios, useNotification } from "../providers/hooks"; import { IngredientInput } from "../components/IngredientInput"; import Layout from "../components/Layout"; +import { SPOON_API_URL } from "../config/config"; function getIngredients(src) { return src?.params?.ingredients ? src?.params?.ingredients : []; @@ -16,40 +17,57 @@ export function Recipe({ navigation, route }) { const theme = useTheme(); const { publicAxios } = useAxios(); const { showNotification } = useNotification(); + const [isLoading, setIsLoading] = React.useState(false); + + React.useEffect(() => { + if (route?.params?.ingredients && route?.params?.ingredients.length === 0) { + showNotification({ + title: "No ingredients found", + description: "Please try again.", + type: "warn", + }); + } + }, [route]); return ( - navigation.navigate("Camera")} - iconName="camera" + { + const filtered = values.ingredients.filter((i) => i); + if (filtered.length === 0) { + return showNotification({ + title: "", + description: "Please add at least 1 ingredient.", + type: "error", + }); + } + const queryString = filtered.toString(); + setIsLoading(true); + await publicAxios + .get( + `${SPOON_API_URL}/recipes/complexSearch?includeIngredients=${queryString}&instructionsRequired=true&fillIngredients=true&addRecipeInformation=true&addRecipeNutrition=false&number=30` + ) + .then((res) => + navigation.navigate("RecipeList", { listings: res.results }) + ) + .catch((error) => {}) + .finally(() => setIsLoading(false)); + }} > - { - const queryString = values.ingredients.filter((i) => i).toString(); - await publicAxios - .get( - `/recipes/complexSearch?includeIngredients=${queryString}&instructionsRequired=true&fillIngredients=true&addRecipeInformation=true&addRecipeNutrition=false&number=30` - ) - .then((res) => - navigation.navigate("RecipeList", { listings: res.results }) - ) - .catch((error) => {}); - }} - > - {({ - handleSubmit, - handleChange, - resetForm, - handleBlur, - values, - touched, - errors, - isSubmitting, - }) => { - return ( + {({ handleSubmit, values, isSubmitting, resetForm, setFieldValue }) => { + return ( + + navigation.navigate("Camera", { + currIngredients: values.ingredients, + }) + } + actionDisabled={isLoading} + iconName="camera" + > ( @@ -83,6 +101,7 @@ export function Recipe({ navigation, route }) { arrayHelpers.remove(index)} + disabled={isSubmitting} /> )) @@ -94,40 +113,82 @@ export function Recipe({ navigation, route }) { height: "80%", }} > - + + + + + Get started! )} - + + + )} /> - ); - }} - - + + ); + }} + ); }