Skip to content

Commit

Permalink
Fixed the loading state and some minor design adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Hudamabkhoot committed Oct 13, 2024
1 parent d61f50d commit b8d5d5c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 35 deletions.
11 changes: 10 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Login from './views/Login';

export function App() {
const [isModalOpen, setIsModalOpen] = useState(false);
const [isLoading, setIsLoading] = useState(false);

/**
* This custom hook takes the path of a shopping list
* in our database and syncs it with localStorage for later use.
Expand Down Expand Up @@ -63,13 +65,20 @@ export function App() {
setListPath={setListPath}
isModalOpen={isModalOpen}
handleShareModalClick={handleShareModalClick}
setIsLoading={setIsLoading}
/>
}
/>
<Route
path="/list"
element={
<List data={data} listPath={listPath} listName={listName} />
<List
data={data}
listPath={listPath}
listName={listName}
isLoading={isLoading}
setIsLoading={setIsLoading}
/>
}
/>
</Route>
Expand Down
2 changes: 2 additions & 0 deletions src/components/SingleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function SingleList({
setListPath,
handleShareModalClick,
setSelectedItem,
setIsLoading,
}) {
const [isAlertOpen, setIsAlertOpen] = useState(false);
const [collectionId, setCollectionId] = useState('');
Expand All @@ -44,6 +45,7 @@ export function SingleList({
const navigate = useNavigate();

function handleClick() {
setIsLoading(true);
setListPath(path);
navigate(`/list`);
}
Expand Down
2 changes: 2 additions & 0 deletions src/views/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function Home({
setListPath,
isModalOpen,
handleShareModalClick,
setIsLoading,
}) {
const [selectedItem, setSelectedItem] = useState('');

Expand Down Expand Up @@ -54,6 +55,7 @@ export function Home({
setListPath={setListPath}
handleShareModalClick={handleShareModalClick}
setSelectedItem={setSelectedItem}
setIsLoading={setIsLoading}
/>
))}
</ul>
Expand Down
82 changes: 48 additions & 34 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import {
import { Button } from '@/components/ui/button';
import { SquarePlus } from 'lucide-react';

export function List({ data, listPath, listName }) {
export function List({ data, listPath, listName, isLoading, setIsLoading }) {
const [search, setSearch] = useState('');
const [allData, setAllData] = useState([]);
const [displayData, setDisplayData] = useState([]);
const [isOpen, setIsOpen] = useState(false);
const [openItemId, setOpenItemId] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [noData, setNoData] = useState(false);

useEffect(() => {
setIsLoading(true);
const arrayWithIndicator = data.map((item) => ({
...item,
indicator: getIndicator(item),
Expand All @@ -39,6 +40,13 @@ export function List({ data, listPath, listName }) {
}
}, [displayData]);

setTimeout(() => {
if (data.length === 0) {
setIsLoading(false);
setNoData(true);
}
}, 2000);

const handleAddModal = () => {
if (search.length > 0) {
setDisplayData(allData);
Expand Down Expand Up @@ -75,9 +83,14 @@ export function List({ data, listPath, listName }) {
<span className="relative inline-block">{listName}</span>
</h1>
<img
src="/img/underline.png"
src="/img/ruby-underline.png"
alt="Description"
className="absolute bottom-[-12px] -right-3 w-14 h-3 dark:hidden"
/>
<img
src="/img/light-pink-underline.png"
alt="Description"
className="absolute bottom-[-12px] -right-3 w-14 h-3"
className="absolute bottom-[-12px] -right-3 w-14 h-3 hidden dark:block"
/>
</div>
</div>
Expand All @@ -89,9 +102,9 @@ export function List({ data, listPath, listName }) {
search={search}
/>
<Dialog open={isOpen} onOpenChange={handleAddModal}>
<DialogTrigger asChild>
<DialogTrigger asChild className="items-start mt-[19px]">
<Button className="bg-transparen hover:bg-transparen p-0">
<SquarePlus className="h-7 w-7 text-pink dark:text-green transition-opacity hover:opacity-75" />
<SquarePlus className="h-10 w-10 text-primary-green dark:text-primary-pink transition-opacity hover:opacity-75" />
</Button>
</DialogTrigger>
<DialogContent>
Expand All @@ -110,27 +123,7 @@ export function List({ data, listPath, listName }) {
</DialogContent>
</Dialog>
</div>
<ul className="flex flex-col justify-center space-y-4 w-full max-w-md">
{displayData.map((item) => (
<ListItem
key={item.id}
name={item.name}
listPath={listPath}
id={item.id}
quantity={item.quantity}
isChecked={item.checked}
dateLastPurchased={item.dateLastPurchased}
totalPurchases={item.totalPurchases}
dayInterval={item.dayInterval}
dateCreated={item.dateCreated}
dateNextPurchased={item.dateNextPurchased}
indicator={item.indicator}
isOpen={openItemId === item.id}
handleOpenModal={handleEditModal}
/>
))}
</ul>
{data.length === 0 && isLoading && (
{isLoading ? (
<div role="status">
<svg
aria-hidden="true"
Expand All @@ -150,26 +143,47 @@ export function List({ data, listPath, listName }) {
</svg>
<span className="sr-only">Loading...</span>
</div>
) : (
<ul className="flex flex-col justify-center space-y-4 w-full max-w-md">
{displayData.map((item) => (
<ListItem
key={item.id}
name={item.name}
listPath={listPath}
id={item.id}
quantity={item.quantity}
isChecked={item.checked}
dateLastPurchased={item.dateLastPurchased}
totalPurchases={item.totalPurchases}
dayInterval={item.dayInterval}
dateCreated={item.dateCreated}
dateNextPurchased={item.dateNextPurchased}
indicator={item.indicator}
isOpen={openItemId === item.id}
handleOpenModal={handleEditModal}
/>
))}
</ul>
)}
{displayData.length === 0 && search.length > 0 && (
<div className="flex flex-col items-center">
<p>No items found. Try searching for a different item!</p>
</div>
)}
{data.length === 0 && !isLoading && (
{!isLoading && noData && displayData.length === 0 && (
<div className="flex flex-col justify-center items-center gap-4 w-full mx-auto">
<p className="text-grey text-center">
Your list is empty. Start adding some items now!
</p>
<Button
className="bg-pink text-white rounded-xl w-full hover:bg-pink hover:bg-opacity-75 text-sm font-semibold max-w-[150px]"
className="bg-primary-pink text-white rounded-xl dark:bg-primary-green dark:text-black w-full hover:bg-primary-pink hover:bg-opacity-75 text-sm font-semibold max-w-[150px]"
id="addFirstItem"
onClick={() => setIsOpen((prev) => !prev)}
>
Add Item
</Button>
</div>
)}
{displayData.length === 0 && search.length > 0 && (
<div className="flex flex-col items-center">
<p>No items found. Try searching for a different item!</p>
</div>
)}
</div>
)}
</>
Expand Down

0 comments on commit b8d5d5c

Please sign in to comment.