Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…t-shopping-list into jvb-dk-sort-by-timeframe
  • Loading branch information
dkimlim committed Apr 21, 2020
2 parents 62e5127 + cbd9027 commit 21ead4b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 36 deletions.
7 changes: 7 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ input {
width: fit-content;
}

.deleteItemButton{
width: 45px;
height: 45px;
margin-left: 5px;
background:black;
};

1 change: 1 addition & 0 deletions src/components/AddItemForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const Form = ({ token }) => {
let normalizeItemName = normalizeString(itemName);
if (!shoppingListCollection.includes(normalizeItemName)) {
let data = {
numOfPurchases: 0,
itemName,
timeFrame: parseInt(timeFrame),
lastPurchaseDate: lastPurchaseDate
Expand Down
14 changes: 14 additions & 0 deletions src/components/Modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import '../App.css';

const Modal = props => {
return (
<div className="Modal">
<p> Are you sure you want to delete {props.item.itemName}?</p>
<button onClick={() => props.delete(props.item)}>YES</button>
<button onClick={() => props.cancel()}>No</button>
</div>
);
};

export default Modal;
13 changes: 10 additions & 3 deletions src/components/ShoppingListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ const generateClass = number =>
// return "error"
// }



const ShoppingListItem = ({item,handleCheck}) => {
const ShoppingListItem = ({item, handleCheck, setCurrentItem, setModal}) => {
const ariaString = `${item.itemName} to be bought in ${timeFrameString(item.timeFrame)}`;
return (
<tr className={generateClass(item.timeFrame)}>
Expand All @@ -61,6 +59,15 @@ const ShoppingListItem = ({item,handleCheck}) => {
</td>
<td>{item.itemName}</td>
<td>{timeFrameString(item.timeFrame)}</td>
<td><button
className="deleteItemButton"
onClick={() => {
setCurrentItem(item);
setModal(true);
}}
>
&#128465;
</button></td>
</tr>
);
}
Expand Down
89 changes: 56 additions & 33 deletions src/pages/ShoppingList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { useHistory } from 'react-router-dom';
import fb from '../lib/firebase';
import moment from 'moment';
import calculateEstimate from '../lib/estimates';
import Modal from '../components/Modal';
import ShoppingListItem from '../components/ShoppingListItem';
import normalizeString from '../lib/normalizeString';
import '../css/ShoppingList.css';

const ShoppingList = ({ token }) => {
const [shoppingListItems, setShoppingListItems] = useState([]);
const [filterString, setFilterString] = useState('');
const [modal, setModal] = useState(false);
const [currentItem, setCurrentItem] = useState(null);
const userToken = token;
let history = useHistory();

Expand Down Expand Up @@ -105,61 +108,81 @@ const ShoppingList = ({ token }) => {
const lastPurchase = moment(formattedDate);
return lastPurchase.diff(newDate, 'hours') < 24;
};

const handleCheck = (e,item) => {
const numberOfPurchases = item.isChecked === false ? (item.numOfPurchases || 0) + 1 : item.numOfPurchases

const handleCheck = (e, item) => {
const numberOfPurchases = !item.isChecked
? (item.numOfPurchases || 0) + 1
: item.numOfPurchases;

if (!(item.lastPurchaseDate == null)) {
let lastEstimate;
item.nextPurchaseDate
? (lastEstimate = item.nextPurchaseDate)
: (lastEstimate = item.timeFrame);
? (lastEstimate = item.nextPurchaseDate)
: (lastEstimate = item.timeFrame);
let lastPurchaseDate = item.lastPurchaseDate;
let today = moment(Date.now())
let today = moment(Date.now());
let lastPurchase = moment(lastPurchaseDate);
let latestInterval = today.diff(lastPurchase, 'days');

let db = fb.firestore();
let nextPurchaseDate = calculateEstimate(
item.lastEstimate,
latestInterval,
item.numOfPurchases
item.lastEstimate,
latestInterval,
item.numOfPurchases
);
db.collection(userToken)
.doc(e.target.name)
.update({
lastPurchaseDate,
numOfPurchases: numberOfPurchases,
latestInterval,
lastEstimate,
nextPurchaseDate,
isChecked: e.target.checked
})
.then(function() {
getShoppingList();
});
} else {
.doc(e.target.name)
.update({
lastPurchaseDate,
numOfPurchases: numberOfPurchases,
latestInterval,
lastEstimate,
nextPurchaseDate,
isChecked: e.target.checked,
})
.then(function() {
getShoppingList();
});
} else {
let lastPurchaseDate = moment(Date.now()).format();
let db = fb.firestore();
db.collection(userToken)
.doc(e.target.name)
.update({
isChecked: e.target.checked,
lastPurchaseDate,
numOfPurchases: numberOfPurchases
.doc(e.target.name)
.update({
isChecked: e.target.checked,
lastPurchaseDate,
numOfPurchases: numberOfPurchases,
})
.then(function() {
.then(function() {
getShoppingList();
});
});
}
};

const deleteItem = item => {
let db = fb.firestore();
db.collection(userToken)
.doc(item.id)
.delete()
.then(() => (getShoppingList(), setModal(false)));
};

const filteredList = shoppingListItems.filter(item => {
return item.itemName.toLowerCase().includes(filterString.toLowerCase());
});

return (
<div>
<div>
{modal ? (
<Modal
item={currentItem}
delete={deleteItem}
cancel={() => {
setModal(false);
}}
/>
) : null}
</div>
<label>Search for an item</label>
<input
type="text"
Expand All @@ -171,13 +194,13 @@ const ShoppingList = ({ token }) => {
<table>
{filterString
? filteredList.map(item => {
return <ShoppingListItem item={item} handleCheck={handleCheck} />;
return <ShoppingListItem item={item} handleCheck={handleCheck} setCurrentItem={setCurrentItem} setModal={setModal} />;
})
: shoppingListItems.length > 0
? shoppingListItems.map(item => <ShoppingListItem item={item} handleCheck={handleCheck}/>)
? shoppingListItems.map(item => <ShoppingListItem item={item} handleCheck={handleCheck} setCurrentItem={setCurrentItem} setModal={setModal} />)
: welcomeInstructions()}
</table>
</div>
);
};
export default ShoppingList;
export default ShoppingList;

0 comments on commit 21ead4b

Please sign in to comment.