Skip to content

Commit

Permalink
feat: Refactor list view components by adding prompts to the differen…
Browse files Browse the repository at this point in the history
…t forms. Conditionally render buttons to go to and from the "/list" and the "/manage-list" views. Add navigation functionality with useNavigate hook.

Co-authored-by: Falak  <[email protected]>
  • Loading branch information
RossaMania committed Sep 5, 2024
1 parent 47a3e94 commit 62b9b98
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/views/authenticated/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { useState, useMemo } from "react";
import { ListItem as ListItemComponent } from "../../components/ListItem";
import { FilterListInput as FilterListComponent } from "../../components/FilterListInput";
import { ListItem } from "../../api";
import { useNavigate } from "react-router-dom";

interface Props {
data: ListItem[];
}

export function List({ data: unfilteredListItems }: Props) {
const navigate = useNavigate();

const [searchTerm, setSearchTerm] = useState<string>("");

const filteredListItems = useMemo(() => {
Expand All @@ -16,17 +19,47 @@ export function List({ data: unfilteredListItems }: Props) {
);
}, [searchTerm, unfilteredListItems]);

const handleAddItemsClick = () => {
navigate("/manage-list");
};

return (
<>
<p>
Hello from the <code>/list</code> page!
</p>

{unfilteredListItems.length === 0 && (
<div>
<h2>Your list is ready!</h2>
<h3>
It looks like you haven’t added any items yet.
<br />
Let’s get started by adding your first item.
</h3>
<button
onClick={handleAddItemsClick}
aria-label="Navigate to add items to your list"
>
{"Get started!"}
</button>
</div>
)}

{unfilteredListItems.length > 0 && (
<FilterListComponent
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
/>
<div>
<FilterListComponent
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
/>
<h3>Want to add more items to your list?</h3>
<button
onClick={handleAddItemsClick}
aria-label="Navigate to add more items to your list"
>
{"Add items"}
</button>
</div>
)}

<ul>
Expand Down
11 changes: 11 additions & 0 deletions src/views/authenticated/ManageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { addItem } from "../../api";
import { validateTrimmedString } from "../../utils";
import toast from "react-hot-toast";

import { useNavigate } from "react-router-dom";
import ShareListForm from "../../components/forms/ShareListForm";

interface Props {
Expand All @@ -22,6 +23,8 @@ const purchaseTimelines = {
};

export function ManageList({ listPath }: Props) {
const navigate = useNavigate();

const [itemName, setItemName] = useState("");
const [itemNextPurchaseTimeline, setItemNextPurchaseTimeline] = useState(
PurchaseTime.soon,
Expand Down Expand Up @@ -78,6 +81,10 @@ export function ManageList({ listPath }: Props) {
}
};

const navigateToListPage = () => {
navigate("/list");
};

return (
<div>
<p>
Expand All @@ -86,6 +93,7 @@ export function ManageList({ listPath }: Props) {
{listPath && (
<>
<form onSubmit={(e) => handleSubmit(e, listPath)}>
<h3>First, add your item!</h3>
<label htmlFor="item-name">
Item:
<input
Expand All @@ -100,6 +108,7 @@ export function ManageList({ listPath }: Props) {
/>
</label>
<br />
<h3>Next, pick when you plan on buying this item again!</h3>
<fieldset>
<legend>When to buy:</legend>
<label htmlFor={PurchaseTime.soon}>
Expand Down Expand Up @@ -154,6 +163,8 @@ export function ManageList({ listPath }: Props) {
Submit Item
</button>
</form>
<h4>Let&apos;s look at your list!</h4>
<button onClick={navigateToListPage}>{"Let's go!"}</button>
</>
)}
<ShareListForm listPath={listPath} />
Expand Down

0 comments on commit 62b9b98

Please sign in to comment.