|
@@ -0,0 +1,52 @@
|
|
|
|
+import React, { useContext, useState } from 'react';
|
|
|
|
+import { AppContext } from '../../layout/AppContext';
|
|
|
|
+import { haveSameDay } from '../../utils/date';
|
|
|
|
+import { CaloriesInput } from '../caloriesInput/CaloriesInput';
|
|
|
|
+import { CalorieRow } from './CalorieRow';
|
|
|
|
+import styles from './caloriesList.css';
|
|
|
|
+
|
|
|
|
+function CaloriesList () {
|
|
|
|
+ const {calorieItems = [], setCalorieItems, selectedDate} = useContext(AppContext);
|
|
|
|
+ const [selectedItem, setSelectedItem] = useState<CalorieValue>();
|
|
|
|
+
|
|
|
|
+ function addCalories (addedCalories: { title: string, count: number }) {
|
|
|
|
+ const updatedCalories = [
|
|
|
|
+ ...calorieItems,
|
|
|
|
+ {
|
|
|
|
+ ...addedCalories,
|
|
|
|
+ timestamp: selectedDate
|
|
|
|
+ }
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ setCalorieItems(updatedCalories);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function onRowClick (item: CalorieValue) {
|
|
|
|
+ if (selectedItem === item) {
|
|
|
|
+ return setSelectedItem(undefined);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return setSelectedItem(item);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const calories = calorieItems.filter(({timestamp}) => haveSameDay(timestamp, selectedDate));
|
|
|
|
+
|
|
|
|
+ return (
|
|
|
|
+ <section className={styles.caloriesList}>
|
|
|
|
+ <section className={styles.caloriesItems}>
|
|
|
|
+ {calories.map((item: CalorieValue, index: number) => {
|
|
|
|
+ // todo: use better key
|
|
|
|
+ return (
|
|
|
|
+ <CalorieRow key={index} {...{item, onRowClick, isSelected: item === selectedItem}} />
|
|
|
|
+ );
|
|
|
|
+ })}
|
|
|
|
+ </section>
|
|
|
|
+
|
|
|
|
+ <CaloriesInput {...{addCalories}} />
|
|
|
|
+ </section>
|
|
|
|
+ );
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export {
|
|
|
|
+ CaloriesList
|
|
|
|
+};
|