|
@@ -1,15 +1,22 @@
|
|
|
-import React, { useContext, useState } from 'react';
|
|
|
+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 [selectedItem, setSelectedItem] = useState<CalorieValue>();
|
|
|
+ const {
|
|
|
+ calorieItems = [],
|
|
|
+ setCalorieItems,
|
|
|
+ selectedDate,
|
|
|
+ setSelectedDate,
|
|
|
+ selectedItem,
|
|
|
+ setSelectedItem
|
|
|
+ } = useContext(AppContext);
|
|
|
|
|
|
function addCalories (addedCalories: { title: string, count: number }) {
|
|
|
const updatedCalories = [
|
|
@@ -25,6 +32,7 @@ function CaloriesList () {
|
|
|
|
|
|
function removeItem () {
|
|
|
setCalorieItems(calorieItems.filter((item) => item !== selectedItem));
|
|
|
+ setSelectedItem();
|
|
|
}
|
|
|
|
|
|
function saveCalories (changedCalories: { title: string, count: number }) {
|
|
@@ -38,7 +46,7 @@ function CaloriesList () {
|
|
|
...changedCalories
|
|
|
};
|
|
|
|
|
|
- const updatedItems = [
|
|
|
+ const updatedItems = [
|
|
|
...calorieItems.slice(0, itemIndex),
|
|
|
updatedItem,
|
|
|
...calorieItems.slice(itemIndex + 1)
|
|
@@ -56,19 +64,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 &&
|