From ed1eaeec59cfd98361b0ed0432133ac616de2538 Mon Sep 17 00:00:00 2001 From: Laure STEPHAN Date: Wed, 11 May 2022 16:02:54 +0200 Subject: [PATCH 1/2] estimation --- src/components/DrinksCategory.js | 3 +- src/hooks/useStateWithAsyncStorage.js | 11 ++-- src/scenes/ConsoFollowUp/ConsoFollowUp.js | 2 +- src/scenes/ConsoFollowUp/drinksCatalog.js | 4 +- src/scenes/Gains/Estimation.js | 8 ++- src/scenes/Gains/EstimationConsosList.js | 64 +++++++++++++++++++++++ src/scenes/Gains/MyGoal.js | 22 ++++++-- 7 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 src/scenes/Gains/EstimationConsosList.js diff --git a/src/components/DrinksCategory.js b/src/components/DrinksCategory.js index d23908720..979d8a474 100644 --- a/src/components/DrinksCategory.js +++ b/src/components/DrinksCategory.js @@ -8,8 +8,9 @@ export const getDrinksKeysFromCategory = (categoryKey, catalog) => catalog.filter((drink) => drink.categoryKey === categoryKey).map(({ drinkKey }) => drinkKey); export const getDrinkQuantityFromDrinks = (drinks, drinkKey) => { + if (drinks === undefined) drinks = "Cocktails et spiritueux"; const drink = drinks.find((d) => d.drinkKey === drinkKey); - if (drink) return drink.quantity; + if (drink !== undefined) return drink.quantity; return 0; }; diff --git a/src/hooks/useStateWithAsyncStorage.js b/src/hooks/useStateWithAsyncStorage.js index 29f7c2a36..d9de902cc 100644 --- a/src/hooks/useStateWithAsyncStorage.js +++ b/src/hooks/useStateWithAsyncStorage.js @@ -1,11 +1,12 @@ import React, { useEffect, useMemo, useState } from 'react'; import AsyncStorage from '@react-native-async-storage/async-storage'; -import { useFocusEffect } from '@react-navigation/native'; +import { useFocusEffect, useIsFocused } from '@react-navigation/native'; const useStateWithAsyncStorage = (key, initValue, debug_resetOnInit = false) => { const [value, setValue] = useState(initValue); const valueType = useMemo(() => typeof initValue, [initValue]); + const isFocused = useIsFocused() const getInitItemValue = async () => { if (debug_resetOnInit) return AsyncStorage.removeItem(key); @@ -26,12 +27,8 @@ const useStateWithAsyncStorage = (key, initValue, debug_resetOnInit = false) => }; useEffect(() => { - getInitItemValue(); - }, []); - - useFocusEffect(() => { - getInitItemValue(); - }); + if (isFocused) getInitItemValue(); + }, [isFocused]); return [value, setValueInAsyncStorage]; }; diff --git a/src/scenes/ConsoFollowUp/ConsoFollowUp.js b/src/scenes/ConsoFollowUp/ConsoFollowUp.js index ffa1451e1..a408dc54e 100644 --- a/src/scenes/ConsoFollowUp/ConsoFollowUp.js +++ b/src/scenes/ConsoFollowUp/ConsoFollowUp.js @@ -58,7 +58,7 @@ const ConsoFollowUp = ({ showWelcomeMessage, setModalTimestamp }) => { (q > 1 ? 'flasques' : 'flasque'), - displayDrinkModal: 'Flasque', + displayDrinkModal: 'flasque', displayFormDrink: 'Flasque de spiritueux', volume: '30 cl', doses: 22, icon: Flasque, price: 4, kcal: 750, - }, + } ]; diff --git a/src/scenes/Gains/Estimation.js b/src/scenes/Gains/Estimation.js index d8e1b9af9..2e0f4d7ec 100644 --- a/src/scenes/Gains/Estimation.js +++ b/src/scenes/Gains/Estimation.js @@ -7,6 +7,7 @@ import TextStyled from '../../components/TextStyled'; import useStateWithAsyncStorage from '../../hooks/useStateWithAsyncStorage'; import ButtonPrimary from '../../components/ButtonPrimary'; import { screenHeight } from '../../styles/theme'; +import EstimationConsosList from "./EstimationConsosList"; const Estimation = () => { @@ -14,7 +15,9 @@ const Estimation = () => { const [drinkgoal] = useStateWithAsyncStorage("@GainQuantityDrinkByWeek", 0); - const Complete = () => { + + const complete = () => { + navigation.navigate("GAINS"); } @@ -40,8 +43,9 @@ const Estimation = () => { + - + ) diff --git a/src/scenes/Gains/EstimationConsosList.js b/src/scenes/Gains/EstimationConsosList.js new file mode 100644 index 000000000..75744e62b --- /dev/null +++ b/src/scenes/Gains/EstimationConsosList.js @@ -0,0 +1,64 @@ +import React, { useEffect } from 'react'; +import { v4 as uuidv4 } from 'uuid'; +import DrinksCategory from '../../components/DrinksCategory'; +import { + drinksCatalog, +} from '../ConsoFollowUp/drinksCatalog'; +import { + Container, + ModalContent, + MarginBottom, +} from '../AddDrink/styles'; +import useStateWithAsyncStorage from '../../hooks/useStateWithAsyncStorage'; + +const EstimationConsosList = ({ }) => { + const [estimationDrinksPerWeek, setEstimationDrinksPerWeek] = useStateWithAsyncStorage("@GainEstimationDrinksPerWeek", []); + const scrollRef = React.useRef(null); + + const setDrinkQuantityRequest = (drinkKey, quantity) => { + const oldDrink = estimationDrinksPerWeek.find((drink) => drink.drinkKey === drinkKey); + + if (oldDrink) { + setEstimationDrinksPerWeek([ + ...estimationDrinksPerWeek.filter((drink) => drink.drinkKey !== drinkKey), + { + ...estimationDrinksPerWeek.find((drink) => drink.drinkKey === drinkKey), + quantity, + }, + ]); + } else { + setEstimationDrinksPerWeek([ + ...estimationDrinksPerWeek, + { + drinkKey, + quantity, + id: uuidv4(), + }, + ]); + } + }; + + return ( + + + {drinksCatalog + .map(({ categoryKey }) => categoryKey) + .filter((categoryKey, index, categories) => categories.indexOf(categoryKey) === index) + .map((category, index) => ( + + ))} + + + + ); +}; + + +export default EstimationConsosList; diff --git a/src/scenes/Gains/MyGoal.js b/src/scenes/Gains/MyGoal.js index 6e9da3f2d..c632e340a 100644 --- a/src/scenes/Gains/MyGoal.js +++ b/src/scenes/Gains/MyGoal.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import styled from 'styled-components'; import { useNavigation } from '@react-navigation/native'; @@ -8,6 +8,8 @@ import TextStyled from '../../components/TextStyled'; import Done from '../../components/Illustrations/Done'; import Economy from '../../components/Illustrations/Economy'; import CocktailGlass from '../../components/Illustrations/CocktailGlassTriangle'; +import useStateWithAsyncStorage from '../../hooks/useStateWithAsyncStorage'; +import { drinksCatalog } from '../ConsoFollowUp/drinksCatalog'; const MyGoal = ({ drinkByWeek, dayNoDrink }) => { @@ -21,6 +23,20 @@ const MyGoal = ({ drinkByWeek, dayNoDrink }) => { navigation.navigate("ESTIMATION"); } + const [estimationDrinksPerWeek] = useStateWithAsyncStorage("@GainEstimationDrinksPerWeek", []); + + const price = useMemo(() => { + return estimationDrinksPerWeek.reduce((sum, drink) => + sum + drink.quantity * drinksCatalog.find(drinkCatalog => drinkCatalog.drinkKey === drink.drinkKey).price + , 0) + }, [estimationDrinksPerWeek]) + + const numberOfDrink = useMemo(() => { + return estimationDrinksPerWeek.reduce((sum, drink) => + sum + drink.quantity + , 0) + }, [estimationDrinksPerWeek]) + return ( @@ -45,8 +61,8 @@ const MyGoal = ({ drinkByWeek, dayNoDrink }) => { - } value={` ${dayNoDrink} €`} /> - } value={` ${drinkByWeek} ${drinkByWeek > 1 ? "verres" : "verre"} `} /> + } value={` ${price} €`} /> + } value={` ${numberOfDrink} ${numberOfDrink > 1 ? "verres" : "verre"} `} /> From f8319d5bd915398d6081b2aa4028833cbf6cbc60 Mon Sep 17 00:00:00 2001 From: Laure STEPHAN Date: Wed, 11 May 2022 16:07:39 +0200 Subject: [PATCH 2/2] sonarcloud --- src/scenes/Gains/EstimationConsosList.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scenes/Gains/EstimationConsosList.js b/src/scenes/Gains/EstimationConsosList.js index 75744e62b..eea25c923 100644 --- a/src/scenes/Gains/EstimationConsosList.js +++ b/src/scenes/Gains/EstimationConsosList.js @@ -11,7 +11,7 @@ import { } from '../AddDrink/styles'; import useStateWithAsyncStorage from '../../hooks/useStateWithAsyncStorage'; -const EstimationConsosList = ({ }) => { +const EstimationConsosList = () => { const [estimationDrinksPerWeek, setEstimationDrinksPerWeek] = useStateWithAsyncStorage("@GainEstimationDrinksPerWeek", []); const scrollRef = React.useRef(null);