Skip to content

Commit 11ff565

Browse files
committed
Fixed Eslint Error and added precommit to husky
1 parent 9edc76f commit 11ff565

10 files changed

Lines changed: 84 additions & 75 deletions

File tree

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
bun run lint
12
bun run prettier

app/(dashboard)/admin/products/[id]/page.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use client';
2-
import { CustomButton, DashboardSidebar, SectionTitle } from '@/components';
2+
import { DashboardSidebar } from '@/components';
33
import Image from 'next/image';
44
import { useRouter } from 'next/navigation';
5-
import React, { useEffect, useState } from 'react';
5+
import React, { useCallback, useEffect, useState } from 'react';
66
import toast from 'react-hot-toast';
77
import {
88
convertCategoryNameToURLFriendly as convertSlugToURLFriendly,
@@ -102,7 +102,7 @@ const DashboardProductDetails = ({
102102
};
103103

104104
// fetching main product data including other product images
105-
const fetchProductData = async () => {
105+
const fetchProductData = useCallback(async () => {
106106
fetch(`http://localhost:3001/api/products/${id}`)
107107
.then((res) => {
108108
return res.json();
@@ -116,7 +116,8 @@ const DashboardProductDetails = ({
116116
});
117117
const images = await imagesData.json();
118118
setOtherImages((currentImages) => images);
119-
};
119+
120+
}, [id])
120121

121122
// fetching all product categories. It will be used for displaying categories in select category input
122123
const fetchCategories = async () => {
@@ -132,7 +133,7 @@ const DashboardProductDetails = ({
132133
useEffect(() => {
133134
fetchCategories();
134135
fetchProductData();
135-
}, [id]);
136+
}, [fetchProductData]);
136137

137138
return (
138139
<div className="bg-white flex justify-start max-w-screen-2xl mx-auto xl:h-full max-xl:flex-col max-xl:gap-y-5">
@@ -265,7 +266,7 @@ const DashboardProductDetails = ({
265266
type="file"
266267
className="file-input file-input-bordered file-input-lg w-full max-w-sm"
267268
onChange={(e) => {
268-
const selectedFile = e.target.files[0];
269+
const selectedFile = e?.target?.files[0];
269270

270271
if (selectedFile) {
271272
uploadFile(selectedFile);

app/checkout/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const CheckoutPage = () => {
171171
toast.error("You don't have items in your cart");
172172
router.push('/cart');
173173
}
174-
}, []);
174+
}, [products.length, router]);
175175

176176
return (
177177
<div className="bg-white">

components/AddToWishlistBtn.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import { useWishlistStore } from '@/app/_zustand/wishlistStore';
1414
import { useSession } from 'next-auth/react';
15-
import React, { useEffect, useState } from 'react';
15+
import React, { useCallback, useEffect, useState } from 'react';
1616
import toast from 'react-hot-toast';
1717
import { FaHeartCrack } from 'react-icons/fa6';
1818
import { FaHeart } from 'react-icons/fa6';
@@ -84,7 +84,7 @@ const AddToWishlistBtn = ({ product, slug }: AddToWishlistBtnProps) => {
8484
}
8585
};
8686

87-
const isInWishlist = async () => {
87+
const isInWishlist = useCallback(() => {
8888
// sending fetch request to get user id because we will need it for cheching whether the product is in wishlist
8989
if (session?.user?.email) {
9090
fetch(`http://localhost:3001/api/users/email/${session?.user?.email}`, {
@@ -106,11 +106,12 @@ const AddToWishlistBtn = ({ product, slug }: AddToWishlistBtnProps) => {
106106
}
107107
});
108108
}
109-
};
109+
110+
}, [session?.user?.email, product?.id])
110111

111112
useEffect(() => {
112113
isInWishlist();
113-
}, [session?.user?.email, wishlist]);
114+
}, [isInWishlist]);
114115

115116
return (
116117
<>

components/Filters.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const Filters = () => {
4747
params.set('sort', sortBy);
4848
params.set('page', page.toString());
4949
replace(`${pathname}?${params}`);
50-
}, [inputCategory, sortBy, page]);
50+
}, [inputCategory, sortBy, page, pathname, replace]);
5151

5252
return (
5353
<div>

components/Header.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
'use client';
1212
import { usePathname } from 'next/navigation';
13-
import React, { useEffect, useState } from 'react';
13+
import React, { useCallback, useEffect, useState } from 'react';
1414
import HeaderTop from './HeaderTop';
1515
import Image from 'next/image';
1616
import SearchInput from './SearchInput';
@@ -34,7 +34,8 @@ const Header = () => {
3434
};
3535

3636
// getting all wishlist items by user id
37-
const getWishlistByUserId = async (id: string) => {
37+
const getWishlistByUserId = useCallback(async (id: string) => {
38+
3839
const response = await fetch(`http://localhost:3001/api/wishlist/${id}`, {
3940
cache: 'no-store',
4041
});
@@ -60,10 +61,11 @@ const Header = () => {
6061
);
6162

6263
setWishlist(productArray);
63-
};
64+
65+
}, [setWishlist])
6466

6567
// getting user by email so I can get his user id
66-
const getUserByEmail = async () => {
68+
const getUserByEmail = useCallback(() => {
6769
if (session?.user?.email) {
6870
fetch(`http://localhost:3001/api/users/email/${session?.user?.email}`, {
6971
cache: 'no-store',
@@ -73,19 +75,20 @@ const Header = () => {
7375
getWishlistByUserId(data?.id);
7476
});
7577
}
76-
};
78+
79+
}, [session?.user?.email, getWishlistByUserId])
7780

7881
useEffect(() => {
7982
getUserByEmail();
80-
}, [session?.user?.email, wishlist.length]);
83+
}, [getUserByEmail, getWishlistByUserId]);
8184

8285
return (
8386
<header className="bg-white">
8487
<HeaderTop />
8588
{pathname.startsWith('/admin') === false && (
8689
<div className="h-32 bg-white flex items-center justify-between px-16 max-[1320px]:px-16 max-md:px-6 max-lg:flex-col max-lg:gap-y-7 max-lg:justify-center max-lg:h-60 max-w-screen-2xl mx-auto">
8790
<Link href="/">
88-
<img
91+
<Image
8992
src="/logo v1 svg.svg"
9093
width={300}
9194
height={300}

components/SimpleSlider.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Slider from 'react-slick';
1414
import 'slick-carousel/slick/slick.css';
1515
import 'slick-carousel/slick/slick-theme.css';
1616
import Link from 'next/link';
17+
import Image from "next/image"
1718

1819
function SimpleSlider() {
1920
const settings = {
@@ -45,7 +46,10 @@ function SimpleSlider() {
4546
Shop now
4647
</Link>
4748
</div>
48-
<img src="/slider image 1.webp" alt="slider 1" className="h-full" />
49+
<Image
50+
width={300}
51+
height={300}
52+
src="/slider image 1.webp" alt="slider 1" className="h-full" />
4953
</div>
5054
<div className="h-[500px] max-lg:h-[400px] max-md:h-[250px] max-[500px]:h-[200px] max-[400px]:h-[150px] relative">
5155
<div className="absolute left-[50%] translate-x-[-50%] translate-y-[30%] h-full text-center max-lg:translate-y-[25%] max-md:translate-y-[20%] max-sm:hidden">
@@ -66,7 +70,10 @@ function SimpleSlider() {
6670
Shop now
6771
</Link>
6872
</div>
69-
<img src="/slider image 2.webp" alt="slider 1" className="h-full" />
73+
<Image
74+
width={400}
75+
height={300}
76+
src="/slider image 2.webp" alt="slider 1" className="h-full" />
7077
</div>
7178
</Slider>
7279
</div>

components/WishItem.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { useWishlistStore } from '@/app/_zustand/wishlistStore';
1313
import { revalidatePath } from 'next/cache';
1414
import Image from 'next/image';
1515
import { useRouter } from 'next/navigation';
16-
import React, { useEffect, useState } from 'react';
16+
import React, { useCallback, useEffect, useState } from 'react';
1717
import toast from 'react-hot-toast';
1818
import { FaHeartCrack } from 'react-icons/fa6';
1919
import { deleteWishItem } from '@/app/actions';
@@ -41,17 +41,7 @@ const WishItem = ({
4141
router.push(`/product/${slug}`);
4242
};
4343

44-
const getUserByEmail = async () => {
45-
if (session?.user?.email) {
46-
fetch(`http://localhost:3001/api/users/email/${session?.user?.email}`, {
47-
cache: 'no-store',
48-
})
49-
.then((response) => response.json())
50-
.then((data) => {
51-
setUserId(data?.id);
52-
});
53-
}
54-
};
44+
5545

5646
const deleteItemFromWishlist = async (productId: string) => {
5747
if (userId) {
@@ -66,9 +56,21 @@ const WishItem = ({
6656
}
6757
};
6858

59+
const getUserByEmail = useCallback(() => {
60+
if (session?.user?.email) {
61+
fetch(`http://localhost:3001/api/users/email/${session?.user?.email}`, {
62+
cache: 'no-store',
63+
})
64+
.then((response) => response.json())
65+
.then((data) => {
66+
setUserId(data?.id);
67+
});
68+
}
69+
}, [session?.user?.email])
70+
6971
useEffect(() => {
7072
getUserByEmail();
71-
}, [session?.user?.email]);
73+
}, [getUserByEmail]);
7274

7375
return (
7476
<tr className="hover:bg-gray-100 cursor-pointer">

components/modules/cart/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { useProductStore } from '@/app/_zustand/store';
44
import toast from 'react-hot-toast';
55
import Image from 'next/image';
66
import Link from 'next/link';
7-
import { FaCircleQuestion } from 'react-icons/fa6';
7+
import { FaCheck, FaCircleQuestion, FaClock, FaXmark } from 'react-icons/fa6';
8+
import QuantityInputCart from '@/components/QuantityInputCart';
89

910
export const CartModule = () => {
1011
const { products, removeFromCart, calculateTotals, total } =

components/modules/wishlist/index.tsx

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,52 @@
11
'use client';
22
import { useWishlistStore } from '@/app/_zustand/wishlistStore';
33
import WishItem from '@/components/WishItem';
4-
import { nanoid } from 'nanoid';
54
import { useSession } from 'next-auth/react';
6-
import { useEffect } from 'react';
5+
import { useCallback, useEffect } from 'react';
76

87
export const WishlistModule = () => {
9-
const { data: session, status } = useSession();
8+
const { data: session } = useSession();
109
const { wishlist, setWishlist } = useWishlistStore();
1110

12-
const getWishlistByUserId = async (id: string) => {
13-
const response = await fetch(`http://localhost:3001/api/wishlist/${id}`, {
14-
cache: 'no-store',
15-
});
16-
const wishlist = await response.json();
17-
18-
const productArray: {
19-
id: string;
20-
title: string;
21-
price: number;
22-
image: string;
23-
slug: string;
24-
stockAvailabillity: number;
25-
}[] = [];
11+
const getWishlistByUserId = useCallback(
12+
async (id: string) => {
13+
const response = await fetch(`http://localhost:3001/api/wishlist/${id}`, {
14+
cache: 'no-store',
15+
});
16+
const wishlistData = await response.json();
2617

27-
wishlist.map((item: any) =>
28-
productArray.push({
18+
const productArray = wishlistData.map((item: any) => ({
2919
id: item?.product?.id,
3020
title: item?.product?.title,
3121
price: item?.product?.price,
3222
image: item?.product?.mainImage,
3323
slug: item?.product?.slug,
3424
stockAvailabillity: item?.product?.inStock,
35-
}),
36-
);
25+
}));
3726

38-
setWishlist(productArray);
39-
};
27+
setWishlist(productArray);
28+
},
29+
[setWishlist],
30+
);
4031

41-
const getUserByEmail = async () => {
32+
const getUserByEmail = useCallback(() => {
4233
if (session?.user?.email) {
43-
fetch(`http://localhost:3001/api/users/email/${session?.user?.email}`, {
34+
fetch(`http://localhost:3001/api/users/email/${session.user.email}`, {
4435
cache: 'no-store',
4536
})
4637
.then((response) => response.json())
4738
.then((data) => {
48-
getWishlistByUserId(data?.id);
39+
if (data?.id) {
40+
getWishlistByUserId(data.id);
41+
}
4942
});
5043
}
51-
};
44+
}, [session?.user?.email, getWishlistByUserId]);
5245

5346
useEffect(() => {
5447
getUserByEmail();
55-
}, [session?.user?.email, wishlist.length]);
48+
}, [getUserByEmail]);
49+
5650
return (
5751
<>
5852
{wishlist && wishlist.length === 0 ? (
@@ -73,18 +67,17 @@ export const WishlistModule = () => {
7367
</tr>
7468
</thead>
7569
<tbody>
76-
{wishlist &&
77-
wishlist?.map((item) => (
78-
<WishItem
79-
id={item?.id}
80-
title={item?.title}
81-
price={item?.price}
82-
image={item?.image}
83-
slug={item?.slug}
84-
stockAvailabillity={item?.stockAvailabillity}
85-
key={nanoid()}
86-
/>
87-
))}
70+
{wishlist?.map((item) => (
71+
<WishItem
72+
key={item.id} // ✅ stable key
73+
id={item.id}
74+
title={item.title}
75+
price={item.price}
76+
image={item.image}
77+
slug={item.slug}
78+
stockAvailabillity={item.stockAvailabillity}
79+
/>
80+
))}
8881
</tbody>
8982
</table>
9083
</div>

0 commit comments

Comments
 (0)