Skip to content

Commit

Permalink
Merge pull request #49 from DevsDomain/favoritos
Browse files Browse the repository at this point in the history
corrigindo favoritos
  • Loading branch information
itsmorais authored Oct 25, 2024
2 parents a11b51d + b562eb2 commit b1fb016
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 103 deletions.
15 changes: 6 additions & 9 deletions backend/src/routes/alimento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import AlimentoController from "../controllers/alimentosController";
const FoodRouter = Router();

FoodRouter.post("/alimentos", AlimentoController.createAlimento);
FoodRouter.get("/alimentos",AlimentoController.buscarAlimentos);
FoodRouter.get("/findAlimento/:barcode",AlimentoController.findAlimento);
FoodRouter.post("/addAlimento/",AlimentoController.addAlimento);
FoodRouter.post("/consumidos",AlimentoController.alimentosConsumidos);
FoodRouter.post("/favoritos/:id",AlimentoController.favoritosAlimentos);
FoodRouter.post("/addFavorito",AlimentoController.adicionarRemoverFavorito);



FoodRouter.get("/alimentos", AlimentoController.buscarAlimentos);
FoodRouter.get("/findAlimento/:barcode", AlimentoController.findAlimento);
FoodRouter.post("/addAlimento/", AlimentoController.addAlimento);
FoodRouter.post("/consumidos", AlimentoController.alimentosConsumidos);
FoodRouter.get("/favoritos/:id", AlimentoController.favoritosAlimentos);
FoodRouter.post("/addFavorito", AlimentoController.adicionarRemoverFavorito);

export default FoodRouter;
59 changes: 29 additions & 30 deletions frontend/src/screens/ProductPG/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ import Ionicons from "react-native-vector-icons/Ionicons";
import { IAlimentos } from "../../types/AlimentosPG";
import { IuserLogin } from "../../types/user";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { StackNavigationProp } from '@react-navigation/stack';
import { StackNavigationProp } from "@react-navigation/stack";
import { RootStackParamList } from "../../types";
type ProductScreenNavigationProp = StackNavigationProp<RootStackParamList, "Profile">;
type ProductScreenNavigationProp = StackNavigationProp<
RootStackParamList,
"Profile"
>;

export default function ProductDetailsScreenPG() {
const route = useRoute();
const { barcode, meal } = route.params as { barcode: string, meal: string };
const { barcode, meal } = route.params as { barcode: string; meal: string };
const [product, setProduct] = useState<IAlimentos | null>(null);
const [quantity, setQuantity] = useState(1);
const [weight, setWeight] = useState(100);
const [user, setUser] = useState<IuserLogin>();
const [myDate, setData] = useState("");
const navigation = useNavigation<ProductScreenNavigationProp>();


const loadUserFromStorage = async () => {
try {
const storedUser = await AsyncStorage.getItem("user")
const dataSelected = await AsyncStorage.getItem("date")
const storedUser = await AsyncStorage.getItem("user");
const dataSelected = await AsyncStorage.getItem("date");

if (storedUser) {
setUser(JSON.parse(storedUser));
console.log("Usuário do AsyncStorage:", storedUser);
}

if (dataSelected) {
setData(dataSelected)
setData(dataSelected);
console.log("DATA RECEBIDA PELO STORAGE", dataSelected);
}

} catch (error) {
console.error("Erro ao obter dados do AsyncStorage:", error);
}
};


const handleIncreaseQuantity = () => setQuantity((prev) => prev + 1);
const handleDecreaseQuantity = () =>
setQuantity((prev) => (prev > 1 ? prev - 1 : 1));
Expand Down Expand Up @@ -89,6 +89,8 @@ export default function ProductDetailsScreenPG() {
gordura: productData.nutriments.fat_100g?.toString() || "0",
sodio: productData.nutriments.sodium_100g?.toString() || "0",
acucar: productData.nutriments.sugars_100g?.toString() || "0",
tiporefeicao: meal,
quantidade: quantity.toString(),
});
} else {
Alert.alert("Erro", "Produto não encontrado.");
Expand All @@ -101,7 +103,7 @@ export default function ProductDetailsScreenPG() {

useEffect(() => {
if (barcode) {
loadUserFromStorage()
loadUserFromStorage();
fetchProductFromBackend();
}
}, [barcode]);
Expand All @@ -115,14 +117,14 @@ export default function ProductDetailsScreenPG() {
const data = formatResponse();

const response = await axios.post(`${BACKEND_API_URL}/addAlimento`, {
data
})
data,
});

if (response.status === 201) {
Alert.alert("Tudo certo!", "Alimento adicionado a dieta do dia.")
navigation.navigate("SelectAlimento")
Alert.alert("Tudo certo!", "Alimento adicionado a dieta do dia.");
navigation.navigate("SelectAlimento");
}
}
};

const formatResponse = () => {
if (product) {
Expand All @@ -140,35 +142,32 @@ export default function ProductDetailsScreenPG() {
idUser: user?.id,
date: myDate,
meal: meal,
quantidade: quantity
}
return data

quantidade: quantity,
};
return data;
}
const data = {}
return data
}
const data = {};
return data;
};

const handleRegister = async () => {
if (!product) return;

try {

const findAlimento = await axios.get(`${BACKEND_API_URL}/findAlimento/${product.barcode}`) // VERIFICA SE O ALIMENTO JÁ EXISTE NO BANCO
const findAlimento = await axios.get(
`${BACKEND_API_URL}/findAlimento/${product.barcode}`
); // VERIFICA SE O ALIMENTO JÁ EXISTE NO BANCO

if (findAlimento.status !== 201) {

try {
const data = formatResponse();
const response = await axios.post(`${BACKEND_API_URL}/alimentos`, {
data
data,
});

addAlimentoToUserDiary();
return;

}
catch (error) {
} catch (error) {
console.error("Erro ao registrar o produto:", error);
Alert.alert("Erro", "Falha ao registrar o produto.");
}
Expand Down Expand Up @@ -345,4 +344,4 @@ const styles = StyleSheet.create({
fontSize: 16,
fontWeight: "bold",
},
});
});
Loading

0 comments on commit b1fb016

Please sign in to comment.