generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mark item as purchased & update DB (#31)
* updated the updateItem func in api to take parameters & use firebase func * skeleton of updateItem if purchased * updateItem changed to increment purchases and update dateLastPurchased * update the checkbox input to have my dynamic prop values * added toast and the updateItem call using the inputs checked status to update the purchase info Co-authored-by: Maha Ahmed <[email protected]> * removed the <ul> tags list items don't have <li> * added a 24 hrs later check function in date utils * added useEffect to determine if 24hrs has passed * added toast that let's user know know item already marked as purchased in last 24 hrs Co-authored-by: Maha Ahmed <[email protected]> * refactoring conditonal check of purchase date to render a marked purchase * refactored the dependency in useEffect for better accuracy * update to use Pick Utility Type, added a try...catch block, directly set updates fields * correct the math of the 24hr check and update func name * moved the setChecked for marking as purchased into success toast, removed redundant checks for under 24hr purchase error * update the shopping list api doc for the updateItem change * updated purchaseDate with more concise logic, added disabled to checkbox, removed toast.error for recheck in 24hr * updated ListItem to use tagged unions Co-authored-by: Maha Ahmed <[email protected]> * fixed the merge conflict issue Co-authored-by: Maha Ahmed <[email protected]> * updateItem rethrows error it catches so that toast of ListItem can trigger the error correctly --------- Co-authored-by: Maha <[email protected]> Co-authored-by: Maha Ahmed <[email protected]>
- Loading branch information
1 parent
6dcf2e7
commit 7bacfe6
Showing
6 changed files
with
139 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,72 @@ | ||
import * as api from '../api'; | ||
import './ListItem.css'; | ||
import "./ListItem.css"; | ||
import { updateItem, ListItem } from "../api"; | ||
import { useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
import { moreThan24HoursPassed } from "../utils"; | ||
|
||
type Props = Pick<api.ListItem, 'name'>; | ||
interface Props { | ||
item: ListItem; | ||
listPath: string | null; | ||
} | ||
interface None { | ||
kind: "none"; | ||
} | ||
|
||
interface Set { | ||
kind: "set"; | ||
value: boolean; | ||
} | ||
|
||
export function ListItemCheckBox({ item, listPath }: Props) { | ||
const [updatedCheckState, setUpdatedCheckState] = useState<None | Set>({ | ||
kind: "none", | ||
}); | ||
|
||
const isChecked = | ||
updatedCheckState.kind === "set" | ||
? updatedCheckState.value | ||
: item.dateLastPurchased | ||
? !moreThan24HoursPassed(item.dateLastPurchased.toDate()) | ||
: false; | ||
|
||
const handleCheckChange = async (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const newCheckedState = e.target.checked; | ||
|
||
// Temporarily store the updated check state | ||
setUpdatedCheckState({ kind: "set", value: newCheckedState }); | ||
|
||
if (!listPath) { | ||
toast.error("Error: listPath is missing or invalid."); | ||
return; | ||
} | ||
|
||
try { | ||
await toast.promise(updateItem(listPath, item), { | ||
loading: `Marking ${item.name} as purchased!`, | ||
success: `${item.name} is now marked as purchased!`, | ||
error: `${item.name} failed to add to your list of purchases. Please try again!`, | ||
}); | ||
} finally { | ||
// reset local state | ||
setUpdatedCheckState({ kind: "none" }); | ||
} | ||
}; | ||
|
||
export function ListItem({ name }: Props) { | ||
return <li className="ListItem">{name}</li>; | ||
return ( | ||
<div className="ListItem"> | ||
<label htmlFor={`checkbox-${item.id}`}> | ||
<input | ||
type="checkbox" | ||
id={`checkbox-${item.id}`} | ||
aria-label={`Mark ${item.name} as purchased.`} | ||
value={item.id} | ||
checked={isChecked} | ||
onChange={handleCheckChange} | ||
aria-checked={isChecked} | ||
disabled={isChecked} | ||
/> | ||
{item.name} | ||
</label> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters