|
@@ -1,14 +1,15 @@
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
|
|
+import ReactSwipe from 'react-swipe';
|
|
|
import { AppContext } from '../../layout/AppContext';
|
|
|
import { haveSameDay } from '../../utils/date';
|
|
|
import { CaloriesInput } from '../caloriesInput/CaloriesInput';
|
|
|
-import { CalorieRow } from './CalorieRow';
|
|
|
-import styles from './caloriesList.css';
|
|
|
import { ItemActions } from './ItemActions';
|
|
|
+import { ItemList } from './ItemList';
|
|
|
import { ListEditBox } from './ListEditBox';
|
|
|
+import styles from './caloriesList.css';
|
|
|
|
|
|
function CaloriesList () {
|
|
|
- const {calorieItems = [], setCalorieItems, selectedDate} = useContext(AppContext);
|
|
|
+ const {calorieItems = [], setCalorieItems, selectedDate, setSelectedDate} = useContext(AppContext);
|
|
|
const [selectedItem, setSelectedItem] = useState<CalorieValue>();
|
|
|
|
|
|
useEffect(() => {
|
|
@@ -42,7 +43,7 @@ function CaloriesList () {
|
|
|
...changedCalories
|
|
|
};
|
|
|
|
|
|
- const updatedItems = [
|
|
|
+ const updatedItems = [
|
|
|
...calorieItems.slice(0, itemIndex),
|
|
|
updatedItem,
|
|
|
...calorieItems.slice(itemIndex + 1)
|
|
@@ -60,19 +61,49 @@ function CaloriesList () {
|
|
|
return setSelectedItem(item);
|
|
|
}
|
|
|
|
|
|
- const calories = calorieItems.filter(({timestamp}) => haveSameDay(timestamp, selectedDate));
|
|
|
+ const swipeItem = (date: number, items: CalorieValue[]) => ({ date, items });
|
|
|
+
|
|
|
+ const dayCalories = [
|
|
|
+ selectedDate - 3600 * 24 * 1000,
|
|
|
+ selectedDate,
|
|
|
+ selectedDate + 3600 * 24 * 1000
|
|
|
+ ].map((date) => {
|
|
|
+ return swipeItem(date, calorieItems.filter(({timestamp}) => haveSameDay(timestamp, date)));
|
|
|
+ });
|
|
|
|
|
|
return (
|
|
|
<section className={styles.caloriesList}>
|
|
|
- <section className={styles.caloriesItems}>
|
|
|
- {calories.map((item: CalorieValue, index: number) => {
|
|
|
- // todo: use better key
|
|
|
- const isSelected = item === selectedItem;
|
|
|
+ <ReactSwipe swipeOptions={{
|
|
|
+ continuous: false,
|
|
|
+ speed: 250,
|
|
|
+ startSlide: 1,
|
|
|
+ transitionEnd (slide) {
|
|
|
+ const newDate = slide === 0
|
|
|
+ ? selectedDate - 3600 * 24 * 1000
|
|
|
+ : slide === 2
|
|
|
+ ? selectedDate + 3600 * 24 * 1000
|
|
|
+ : selectedDate;
|
|
|
+
|
|
|
+ setSelectedDate(newDate);
|
|
|
+ }
|
|
|
+ }} style={{
|
|
|
+ child: {
|
|
|
+ float: 'left',
|
|
|
+ position: 'relative'
|
|
|
+ },
|
|
|
+ container: {
|
|
|
+ flex: 1
|
|
|
+ },
|
|
|
+ wrapper: {
|
|
|
+ height: '100%'
|
|
|
+ }
|
|
|
+ }}>
|
|
|
+ {dayCalories.map(({ date, items }) => {
|
|
|
return (
|
|
|
- <CalorieRow key={index} {...{item, onRowClick, isSelected}} />
|
|
|
+ <ItemList key={date} {...{items, selectedItem, onRowClick}} />
|
|
|
);
|
|
|
})}
|
|
|
- </section>
|
|
|
+ </ReactSwipe>
|
|
|
|
|
|
<div className={styles.editBoxes}>
|
|
|
{selectedItem &&
|