Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions backend/src/routes/notif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ router.get("/get/:id", async (req: Req, res: Response) => {
});

// Create a new notification
router.post("/notifications", async (req: Req, res: Response) => {
const { user_id, message, sender, date_sent, user_id_user_id } = req.body;
try {
const newNotification = await pool.query('INSERT INTO notifications (user_id, message, sender, date_sent, user_id_user_id) VALUES ($1, $2, $3, $4, $5) RETURNING *',
[user_id, message, sender, date_sent, user_id_user_id]);
res.status(201).json(newNotification.rows[0]);
} catch (err) {
console.error("ERROR", err);
res.status(500).json({ error: 'An error occurred while creating notification.' });
}
});
// router.post("/notifications", async (req: Req, res: Response) => {
// const { user_id, message, sender, date_sent, user_id_user_id } = req.body;
// try {
// const newNotification = await pool.query('INSERT INTO notifications (user_id, message, sender, date_sent, user_id_user_id) VALUES ($1, $2, $3, $4, $5) RETURNING *',
// [user_id, message, sender, date_sent, user_id_user_id]);
// res.status(201).json(newNotification.rows[0]);
// } catch (err) {
// console.error("ERROR", err);
// res.status(500).json({ error: 'An error occurred while creating notification.' });
// }
// });

// Update a notification
router.put("/notifications/:id", async (req: Req, res: Response) => {
Expand Down
11 changes: 11 additions & 0 deletions backend/src/types/datatypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface RegisterUser {
confirm_password?: string;
}

<<<<<<< Updated upstream
export interface Cars {
id?: string;
driver_id?: string;
Expand All @@ -78,3 +79,13 @@ export interface Cars {
license_number?: string;
brand?: string;
}
=======
export interface Notification {
id: string;
user_id: number;
message: string;
sender: "Admin" | "User";
date_sent: string;
}

>>>>>>> Stashed changes
9 changes: 9 additions & 0 deletions backend/types/datatypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ export interface RegisterUser {
violation_date: string;
paid_status: boolean;
}

export interface Notification {
id: string;
user_id: number;
message: string;
sender: "Admin" | "User";
date_sent: string;
}

31 changes: 31 additions & 0 deletions frontend/src/components/NotificationComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import useNotif from './useNotif.ts';

const NotificationsComponent: React.FC = () => {
const {
notifications,
loading,
error,
deleteNotification,
} = useNotif();

if (loading) return <div>Loading notifications...</div>;
if (error) return <div>Error: {error}</div>;

return (
<div>
<h2>Notifications</h2>
<ul>
{notifications.map((notifs) => (
<li key={notifs.id}>
{notifs.message}
<button onClick={() => deleteNotification(notifs.id)}>Delete</button>
</li>
))}
</ul>
{/* Add functionality to create or update notifications as needed */}
</div>
);
};

export default NotificationsComponent;
40 changes: 40 additions & 0 deletions frontend/src/hooks/useNotif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useEffect, useState } from "react";
import { BackendError } from "../types/error.types";
import { Notification } from "../types/datatypes";
import useFetch from "./useFetch.ts";


export const useNotif = () => {
const [notifs, setNotifs] = useState<Notification[] | null>(null); // Store fetched notifs
const [error, setError] = useState<BackendError | null>(null); // Handle errors
const [loading, setLoading] = useState<boolean>(true); // Track loading state

const { fetchWithAuth } = useFetch();

const fetchNotif = async () => {
setLoading(true);
try {
const response = await fetchWithAuth('notifications', 'GET');

if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
setNotifs(data.data);
} catch (error) {
console.error("Unexpected error:", error);
setError({ message: "An unexpected error occurred" } as BackendError);
} finally {
setLoading(false);
}
};

useEffect(() => {
fetchNotif();
}, []);

Check warning on line 34 in frontend/src/hooks/useNotif.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

React Hook useEffect has a missing dependency: 'fetchNotif'. Either include it or remove the dependency array

Check warning on line 34 in frontend/src/hooks/useNotif.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

React Hook useEffect has a missing dependency: 'fetchNotif'. Either include it or remove the dependency array

Check warning on line 34 in frontend/src/hooks/useNotif.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

React Hook useEffect has a missing dependency: 'fetchNotif'. Either include it or remove the dependency array

return { notifs, error, loading, fetchNotif};

};
export default useNotif;

Loading