Skip to content

Commit b8d5d5c

Browse files
committed
Fixed the loading state and some minor design adjustments
1 parent d61f50d commit b8d5d5c

File tree

4 files changed

+62
-35
lines changed

4 files changed

+62
-35
lines changed

src/App.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Login from './views/Login';
88

99
export function App() {
1010
const [isModalOpen, setIsModalOpen] = useState(false);
11+
const [isLoading, setIsLoading] = useState(false);
12+
1113
/**
1214
* This custom hook takes the path of a shopping list
1315
* in our database and syncs it with localStorage for later use.
@@ -63,13 +65,20 @@ export function App() {
6365
setListPath={setListPath}
6466
isModalOpen={isModalOpen}
6567
handleShareModalClick={handleShareModalClick}
68+
setIsLoading={setIsLoading}
6669
/>
6770
}
6871
/>
6972
<Route
7073
path="/list"
7174
element={
72-
<List data={data} listPath={listPath} listName={listName} />
75+
<List
76+
data={data}
77+
listPath={listPath}
78+
listName={listName}
79+
isLoading={isLoading}
80+
setIsLoading={setIsLoading}
81+
/>
7382
}
7483
/>
7584
</Route>

src/components/SingleList.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function SingleList({
3131
setListPath,
3232
handleShareModalClick,
3333
setSelectedItem,
34+
setIsLoading,
3435
}) {
3536
const [isAlertOpen, setIsAlertOpen] = useState(false);
3637
const [collectionId, setCollectionId] = useState('');
@@ -44,6 +45,7 @@ export function SingleList({
4445
const navigate = useNavigate();
4546

4647
function handleClick() {
48+
setIsLoading(true);
4749
setListPath(path);
4850
navigate(`/list`);
4951
}

src/views/Home.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function Home({
99
setListPath,
1010
isModalOpen,
1111
handleShareModalClick,
12+
setIsLoading,
1213
}) {
1314
const [selectedItem, setSelectedItem] = useState('');
1415

@@ -54,6 +55,7 @@ export function Home({
5455
setListPath={setListPath}
5556
handleShareModalClick={handleShareModalClick}
5657
setSelectedItem={setSelectedItem}
58+
setIsLoading={setIsLoading}
5759
/>
5860
))}
5961
</ul>

src/views/List.jsx

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ import {
1515
import { Button } from '@/components/ui/button';
1616
import { SquarePlus } from 'lucide-react';
1717

18-
export function List({ data, listPath, listName }) {
18+
export function List({ data, listPath, listName, isLoading, setIsLoading }) {
1919
const [search, setSearch] = useState('');
2020
const [allData, setAllData] = useState([]);
2121
const [displayData, setDisplayData] = useState([]);
2222
const [isOpen, setIsOpen] = useState(false);
2323
const [openItemId, setOpenItemId] = useState(null);
24-
const [isLoading, setIsLoading] = useState(true);
24+
const [noData, setNoData] = useState(false);
2525

2626
useEffect(() => {
27+
setIsLoading(true);
2728
const arrayWithIndicator = data.map((item) => ({
2829
...item,
2930
indicator: getIndicator(item),
@@ -39,6 +40,13 @@ export function List({ data, listPath, listName }) {
3940
}
4041
}, [displayData]);
4142

43+
setTimeout(() => {
44+
if (data.length === 0) {
45+
setIsLoading(false);
46+
setNoData(true);
47+
}
48+
}, 2000);
49+
4250
const handleAddModal = () => {
4351
if (search.length > 0) {
4452
setDisplayData(allData);
@@ -75,9 +83,14 @@ export function List({ data, listPath, listName }) {
7583
<span className="relative inline-block">{listName}</span>
7684
</h1>
7785
<img
78-
src="/img/underline.png"
86+
src="/img/ruby-underline.png"
87+
alt="Description"
88+
className="absolute bottom-[-12px] -right-3 w-14 h-3 dark:hidden"
89+
/>
90+
<img
91+
src="/img/light-pink-underline.png"
7992
alt="Description"
80-
className="absolute bottom-[-12px] -right-3 w-14 h-3"
93+
className="absolute bottom-[-12px] -right-3 w-14 h-3 hidden dark:block"
8194
/>
8295
</div>
8396
</div>
@@ -89,9 +102,9 @@ export function List({ data, listPath, listName }) {
89102
search={search}
90103
/>
91104
<Dialog open={isOpen} onOpenChange={handleAddModal}>
92-
<DialogTrigger asChild>
105+
<DialogTrigger asChild className="items-start mt-[19px]">
93106
<Button className="bg-transparen hover:bg-transparen p-0">
94-
<SquarePlus className="h-7 w-7 text-pink dark:text-green transition-opacity hover:opacity-75" />
107+
<SquarePlus className="h-10 w-10 text-primary-green dark:text-primary-pink transition-opacity hover:opacity-75" />
95108
</Button>
96109
</DialogTrigger>
97110
<DialogContent>
@@ -110,27 +123,7 @@ export function List({ data, listPath, listName }) {
110123
</DialogContent>
111124
</Dialog>
112125
</div>
113-
<ul className="flex flex-col justify-center space-y-4 w-full max-w-md">
114-
{displayData.map((item) => (
115-
<ListItem
116-
key={item.id}
117-
name={item.name}
118-
listPath={listPath}
119-
id={item.id}
120-
quantity={item.quantity}
121-
isChecked={item.checked}
122-
dateLastPurchased={item.dateLastPurchased}
123-
totalPurchases={item.totalPurchases}
124-
dayInterval={item.dayInterval}
125-
dateCreated={item.dateCreated}
126-
dateNextPurchased={item.dateNextPurchased}
127-
indicator={item.indicator}
128-
isOpen={openItemId === item.id}
129-
handleOpenModal={handleEditModal}
130-
/>
131-
))}
132-
</ul>
133-
{data.length === 0 && isLoading && (
126+
{isLoading ? (
134127
<div role="status">
135128
<svg
136129
aria-hidden="true"
@@ -150,26 +143,47 @@ export function List({ data, listPath, listName }) {
150143
</svg>
151144
<span className="sr-only">Loading...</span>
152145
</div>
146+
) : (
147+
<ul className="flex flex-col justify-center space-y-4 w-full max-w-md">
148+
{displayData.map((item) => (
149+
<ListItem
150+
key={item.id}
151+
name={item.name}
152+
listPath={listPath}
153+
id={item.id}
154+
quantity={item.quantity}
155+
isChecked={item.checked}
156+
dateLastPurchased={item.dateLastPurchased}
157+
totalPurchases={item.totalPurchases}
158+
dayInterval={item.dayInterval}
159+
dateCreated={item.dateCreated}
160+
dateNextPurchased={item.dateNextPurchased}
161+
indicator={item.indicator}
162+
isOpen={openItemId === item.id}
163+
handleOpenModal={handleEditModal}
164+
/>
165+
))}
166+
</ul>
153167
)}
154-
{displayData.length === 0 && search.length > 0 && (
155-
<div className="flex flex-col items-center">
156-
<p>No items found. Try searching for a different item!</p>
157-
</div>
158-
)}
159-
{data.length === 0 && !isLoading && (
168+
{!isLoading && noData && displayData.length === 0 && (
160169
<div className="flex flex-col justify-center items-center gap-4 w-full mx-auto">
161170
<p className="text-grey text-center">
162171
Your list is empty. Start adding some items now!
163172
</p>
164173
<Button
165-
className="bg-pink text-white rounded-xl w-full hover:bg-pink hover:bg-opacity-75 text-sm font-semibold max-w-[150px]"
174+
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]"
166175
id="addFirstItem"
167176
onClick={() => setIsOpen((prev) => !prev)}
168177
>
169178
Add Item
170179
</Button>
171180
</div>
172181
)}
182+
{displayData.length === 0 && search.length > 0 && (
183+
<div className="flex flex-col items-center">
184+
<p>No items found. Try searching for a different item!</p>
185+
</div>
186+
)}
173187
</div>
174188
)}
175189
</>

0 commit comments

Comments
 (0)