Skip to content

Commit

Permalink
Fix some nulls....
Browse files Browse the repository at this point in the history
  • Loading branch information
andreu-vall committed Dec 15, 2023
1 parent cd4a3e7 commit 836f078
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/components/NotificationBell.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//He de fer un fetch a les notificacions de l'usuari i mostrar-les en NotificationsCard cadascuna
//React
import React, { useState, useEffect } from 'react'
import { Container, Row, Col } from "react-bootstrap"
import NotificationCard from "./NotificationCard"
import Dropdown from 'react-bootstrap/Dropdown';
import { BellFill } from 'react-bootstrap-icons';
Expand Down Expand Up @@ -39,13 +38,19 @@ export default function NotificationBell() {
const currentUserUsername = localStorage.getItem('currentUser')

const handleStatusChange = (id, newStatus) => {
setNotifications(notifications.map(notification =>
if (notifications === null) {
return;
}
setNotifications(notifications?.map(notification =>
notification.id === id ? { ...notification, status: newStatus } : notification
));
};

const getUnreadNotifications = () => {
return notifications.filter(notification => notification.status === "unread").length;
if (notifications === null || notifications.length === 0 || !Array.isArray(notifications)) {
return 0;
}
return notifications?.filter(notification => notification.status === "unread").length;
}

const getRecipes = () => {
Expand All @@ -57,13 +62,17 @@ export default function NotificationBell() {
})
.catch((error) => {
console.error("Error al obtenir notificacions:", error)
setNotifications([])
});
};

useEffect(() => {
getRecipes()
}, [])

if (currentUserUsername === null || notifications === null || notifications.length === 0) {
return null;
}
return (
<>

Expand All @@ -76,7 +85,7 @@ export default function NotificationBell() {
</CustomToggle>

<Dropdown.Menu style={{ maxHeight: '50vh', overflowY: 'auto', overflowX: 'hidden', backgroundColor: '#ffe7df' }}>
{notifications.map(notification => (
{notifications.length > 0 && notifications?.map(notification => (
<Dropdown.Item
className="my-dropdown-item"
onClick={(e) => e.stopPropagation()}
Expand Down
4 changes: 2 additions & 2 deletions src/components/NotificationCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export default function NotificationCard({ notification, username, onStatusChang
<Card.Img variant="top" src={notification.image} />
</div>
<div className="col-md-8">
<Card.Title>Card title</Card.Title>
<Card.Title>{notification.type}</Card.Title>
<Card.Text>
<Link to={`/users/${notification.username}`}>{notification.username}</Link> ha fet {notification.text}
<Link to={`/users/${notification.username}`}>{notification.username}</Link> {notification.text}
</Card.Text>
<Card.Text>
<small className="text-muted">{moment.utc(notification.date).fromNow()}</small>
Expand Down
10 changes: 5 additions & 5 deletions src/components/Notifications.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export default function Notifications() {
const currentUserUsername = localStorage.getItem('currentUser')

const handleStatusChange = (id, newStatus) => {
setNotifications(notifications.map(notification =>
setNotifications(notifications?.map(notification =>
notification.id === id ? { ...notification, status: newStatus } : notification
));
};

// const getUnreadNotifications = () => {
// return notifications.filter(notification => notification.status === "unread").length;
// }
const getUnreadNotifications = () => {
return notifications.filter(notification => notification.status === "unread").length;
}

const getRecipes = () => {
console.log("Fetching notifications...");
Expand All @@ -57,7 +57,7 @@ export default function Notifications() {
<Timer method={getRecipes} />

<Container>
{notifications.map(notification => (
{notifications?.map(notification => (
<Row key={notification.id}>
<Col xs={12}>
<NotificationCard
Expand Down

0 comments on commit 836f078

Please sign in to comment.