-
Notifications
You must be signed in to change notification settings - Fork 0
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 #27 from MiguelMRojas/_Favorites
Favorites
- Loading branch information
Showing
10 changed files
with
148 additions
and
10 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
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,11 @@ | ||
/*Cards container*/ | ||
.products { | ||
padding-block-start: 16px; | ||
padding-block-end: 70px; | ||
height: max-content; | ||
display: flex; | ||
justify-content: center; | ||
align-items: stretch; | ||
flex-wrap: wrap; | ||
gap: 24px; | ||
} |
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,91 @@ | ||
import Styles from './Favorites.module.css'; | ||
import { useEffect, useState, useContext } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { SessionContext } from '../../context/SessionContext'; | ||
import { GetDetailedFavoritesService } from '../../services/user.services'; | ||
import { GetProductImageFromEndpointService } from '../../services/products.service'; | ||
import { Iproduct } from '../../interfaces/interfaces'; | ||
import { ProductCard } from '../../components/productCard/ProductCard'; | ||
import { ModalProduct } from '../../components/modalproducts/ModalProducts'; | ||
|
||
import { toast } from 'react-toastify'; | ||
|
||
export function Favorites() { | ||
const { isSessionLoading, isLoggedIn } = useContext(SessionContext); | ||
const [favorites, setFavorites] = useState(Array<Iproduct>); | ||
const [viewModal, setViewModal] = useState(false); | ||
const navigate = useNavigate(); | ||
|
||
// Redirect to home if is not logged in | ||
useEffect(() => { | ||
console.log(isSessionLoading, isLoggedIn); | ||
if (!isSessionLoading && !isLoggedIn) { | ||
// Show an information alert | ||
toast.warn('Please, log in to see your favorites', { | ||
position: 'top-right', | ||
autoClose: 2500, | ||
pauseOnHover: true, | ||
theme: 'light', | ||
}); | ||
|
||
// Redirect to heme because there is an active session | ||
navigate('/login'); | ||
} | ||
}, [isSessionLoading, isLoggedIn]); | ||
|
||
// Datos del producto actual que es mostrado en el modal | ||
const [modalData, setModalData] = useState({ | ||
id: '', | ||
serial: 0, | ||
name: '', | ||
image: '', | ||
units: '', | ||
annotations: '', | ||
discount: 0, | ||
price: 0, | ||
description: '', | ||
}); | ||
|
||
useEffect(() => { | ||
const getCurrentFavorites = async () => { | ||
const response = await GetDetailedFavoritesService(1); | ||
const items = response.data.favorites; | ||
const products: Array<Iproduct> = []; | ||
|
||
if (response.status === 200) { | ||
for (let i = 0; i < items.length; i++) { | ||
const ireply = await GetProductImageFromEndpointService(items[i].image); | ||
products.push({ ...items[i], image: ireply.image }); | ||
} | ||
|
||
setFavorites(products); | ||
} | ||
}; | ||
|
||
getCurrentFavorites(); | ||
}, []); | ||
|
||
// Funcion para abrir el modal | ||
const handleAbrir = (data: Iproduct) => { | ||
// Se cambian los datos del modal con los datos del producto | ||
setModalData({ ...data }); | ||
// Se cambia la vista del modal a true | ||
setViewModal(true); | ||
}; | ||
|
||
// Funcion para cerrar el modal | ||
const handleCerrar = () => { | ||
setViewModal(false); | ||
}; | ||
|
||
return ( | ||
<> | ||
<main className={Styles.products}> | ||
{favorites.map((product, index) => { | ||
return <ProductCard CallBack={handleAbrir} product={product} key={index} />; | ||
})} | ||
</main> | ||
{viewModal ? <ModalProduct CerrarCallBack={handleCerrar} product={modalData} /> : ''} | ||
</> | ||
); | ||
} |
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
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