Skip to content

Commit

Permalink
Merge pull request #27 from the-collab-lab/dc-tc-alert-on-repeated-item
Browse files Browse the repository at this point in the history
Alert on repeated item
  • Loading branch information
tataw-cl authored Sep 15, 2024
2 parents 3958b50 + cf0fcc0 commit a895d04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export function App() {
/>
<Route
path="/manage-list"
element={<ManageList listPath={listPath} userId={userId} />}
element={
<ManageList listPath={listPath} userId={userId} data={data} />
}
/>
</Route>
</Routes>
Expand Down
7 changes: 6 additions & 1 deletion src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import './ListItem.css';
export function ListItem({ name, dateLastPurchased, onCheck }) {
const [isChecked, setIsChecked] = useState(false);

// Update `isChecked` based on the `dateLastPurchased` value
useEffect(() => {
const checkStatus = () => {
if (dateLastPurchased) {
const purchaseDate = dateLastPurchased.toDate();
const timeSinceLastPurchase = new Date() - purchaseDate;
const hasBeenPurchasedRecently =
timeSinceLastPurchase < 24 * 60 * 60 * 1000;

timeSinceLastPurchase < 24 * 60 * 60 * 1000; // 24 hours
setIsChecked(hasBeenPurchasedRecently);
} else {
setIsChecked(false);
Expand All @@ -21,6 +22,10 @@ export function ListItem({ name, dateLastPurchased, onCheck }) {
checkStatus();
}, [dateLastPurchased]);

const handleChecked = () => {
onCheck(id);
};

return (
<li className="ListItem">
<label>
Expand Down
21 changes: 20 additions & 1 deletion src/views/ManageList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState } from 'react';
import { addItem } from '../api';
import { shareList } from '../api/firebase';

export function ManageList({ listPath, userId }) {
export function ManageList({ listPath, userId, data }) {
const [formNewItem, setFormNewItem] = useState({
name: '',
nextPurchase: 0,
Expand Down Expand Up @@ -31,6 +31,25 @@ export function ManageList({ listPath, userId }) {
return;
}
try {
//Function to normalize the item name and convert to lowercase goes here
const normalizedName = (name) => {
return name
.toLowerCase()
.replace(/[^\w\s]|_/g, '') // for punctuation
.replace(/\s+/g, ''); // for spaces
};

// check if the item already exists
const itemExists = data.some(
(item) => normalizedName(item.name) === normalizedName(name),
);

// if the item already exists, show an error message
if (itemExists) {
setMessageItem(`${normalizedName(name)} is already in the list`);
return;
}
// if the item does not exist, add it to the list
await addItem(listPath, {
itemName: name,
daysUntilNextPurchase: nextPurchase,
Expand Down

0 comments on commit a895d04

Please sign in to comment.