generated from 202306-NEA-DZ-FEW/capstone-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from 202306-NEA-DZ-FEW/119-create-my-listings…
…-page Create my listings page
- Loading branch information
Showing
6 changed files
with
339 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import Image from "next/image"; | ||
import Link from "next/link"; | ||
import React from "react"; | ||
import { FaTrash } from "react-icons/fa"; | ||
import { useState } from "react"; | ||
import { toast } from "react-toastify"; | ||
import "react-toastify/dist/ReactToastify.css"; | ||
import { deleteDoc, doc } from "firebase/firestore"; | ||
import { db } from "@/util/firebase"; | ||
|
||
function ListingCard({ product }) { | ||
const [isDeleting, setIsDeleting] = useState(false); | ||
|
||
const handleDelete = async () => { | ||
const confirmDelete = window.confirm( | ||
"Are you sure you want to delete this product?" | ||
); | ||
|
||
if (confirmDelete) { | ||
try { | ||
setIsDeleting(true); | ||
|
||
const productRef = doc(db, "products", product.id); | ||
await deleteDoc(productRef); | ||
|
||
toast.success("Product deleted successfully!"); | ||
} catch (error) { | ||
console.error("Error deleting product:", error); | ||
toast.error(`Failed to delete product: ${error.message}`); | ||
} finally { | ||
setIsDeleting(false); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div className='w-56 bg-white shadow-md rounded-xl duration-500 hover:shadow-xl'> | ||
<Link href='/'> | ||
<div className='relative border-b-2 border-gray-300'> | ||
<Image | ||
src={product?.pictures[1]} | ||
height={364} | ||
width={288} | ||
alt='Product' | ||
className='h-64 w-56 object-cover rounded-t-xl' | ||
loading='lazy' | ||
/> | ||
<div className='absolute top-0 m-2 left-0 rounded-full '> | ||
<p | ||
className={`${ | ||
product?.type === "sale" | ||
? "bg-[#1B96EF]" | ||
: "bg-[#FF8A57]" | ||
} rounded-full px-2 p-1 text-[12px] font-bold capitalize tracking-wide text-white sm:py-1 sm:px-3`} | ||
> | ||
{product?.type} | ||
</p> | ||
</div> | ||
</div> | ||
</Link> | ||
<div className='px-4 py-3 w-56'> | ||
<div className='flex justify-between'> | ||
<span className='text-gray-400 mr-3 uppercase text-xs'> | ||
{product?.category} | ||
</span> | ||
<span className='text-gray-400 ml-3 capitalize font-semibold text-xs'> | ||
{product?.condition} | ||
</span> | ||
</div> | ||
<Link href='/'> | ||
<p className='text-lg font-bold mt-2 text-black truncate block capitalize'> | ||
{product?.title} | ||
</p> | ||
</Link> | ||
<div className='flex items-center'> | ||
<p className='text-lg font-semibold text-black cursor-auto my-3'> | ||
{product?.price}$ | ||
</p> | ||
<div className='ml-auto'> | ||
<button onClick={handleDelete} className='text-red-500'> | ||
<FaTrash /> | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default ListingCard; |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import renderer from "react-test-renderer"; | ||
|
||
import ListingCard from "../ListingCard"; | ||
|
||
jest.mock("../../../util/firebase", () => { | ||
return { | ||
initializeApp: jest.fn(), | ||
}; | ||
}); | ||
|
||
it("renders correctly", () => { | ||
const tree = renderer.create(<ListingCard />).toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); |
103 changes: 103 additions & 0 deletions
103
src/components/listingcard/__test__/__snapshots__/ListingCard.test.js.snap
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders correctly 1`] = ` | ||
<div | ||
className="w-56 bg-white shadow-md rounded-xl duration-500 hover:shadow-xl" | ||
> | ||
<a | ||
href="/" | ||
onClick={[Function]} | ||
onMouseEnter={[Function]} | ||
onTouchStart={[Function]} | ||
> | ||
<div | ||
className="relative border-b-2 border-gray-300" | ||
> | ||
<img | ||
alt="Product" | ||
className="h-64 w-56 object-cover rounded-t-xl" | ||
data-nimg="1" | ||
decoding="async" | ||
height={364} | ||
loading="lazy" | ||
onError={[Function]} | ||
onLoad={[Function]} | ||
src="" | ||
style={ | ||
Object { | ||
"color": "transparent", | ||
} | ||
} | ||
width={288} | ||
/> | ||
<div | ||
className="absolute top-0 m-2 left-0 rounded-full " | ||
> | ||
<p | ||
className="bg-[#FF8A57] rounded-full px-2 p-1 text-[12px] font-bold capitalize tracking-wide text-white sm:py-1 sm:px-3" | ||
/> | ||
</div> | ||
</div> | ||
</a> | ||
<div | ||
className="px-4 py-3 w-56" | ||
> | ||
<div | ||
className="flex justify-between" | ||
> | ||
<span | ||
className="text-gray-400 mr-3 uppercase text-xs" | ||
/> | ||
<span | ||
className="text-gray-400 ml-3 capitalize font-semibold text-xs" | ||
/> | ||
</div> | ||
<a | ||
href="/" | ||
onClick={[Function]} | ||
onMouseEnter={[Function]} | ||
onTouchStart={[Function]} | ||
> | ||
<p | ||
className="text-lg font-bold mt-2 text-black truncate block capitalize" | ||
/> | ||
</a> | ||
<div | ||
className="flex items-center" | ||
> | ||
<p | ||
className="text-lg font-semibold text-black cursor-auto my-3" | ||
> | ||
$ | ||
</p> | ||
<div | ||
className="ml-auto" | ||
> | ||
<button | ||
className="text-red-500" | ||
onClick={[Function]} | ||
> | ||
<svg | ||
fill="currentColor" | ||
height="1em" | ||
stroke="currentColor" | ||
strokeWidth="0" | ||
style={ | ||
Object { | ||
"color": undefined, | ||
} | ||
} | ||
viewBox="0 0 448 512" | ||
width="1em" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M432 32H312l-9.4-18.7A24 24 0 0 0 281.1 0H166.8a23.72 23.72 0 0 0-21.4 13.3L136 32H16A16 16 0 0 0 0 48v32a16 16 0 0 0 16 16h416a16 16 0 0 0 16-16V48a16 16 0 0 0-16-16zM53.2 467a48 48 0 0 0 47.9 45h245.8a48 48 0 0 0 47.9-45L416 128H32z" | ||
/> | ||
</svg> | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`; |
Oops, something went wrong.