Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e84c286
some
pratyush1712 Dec 9, 2022
7a9d7f2
Merge branch 'master' of https://github.com/cornell-dti/carriage-web
pratyush1712 Jan 29, 2023
b8f3211
Merge branch 'master' of https://github.com/cornell-dti/carriage-web
pratyush1712 Feb 6, 2023
8ac410a
switched to crypto-js as crypto is a node module
pratyush1712 Feb 6, 2023
1af1aa2
Merge branch 'master' of https://github.com/cornell-dti/carriage-web
pratyush1712 Feb 18, 2023
758bba0
Merge branch 'master' of https://github.com/cornell-dti/carriage-web
pratyush1712 Mar 4, 2023
26220c3
Merge branch 'master' of https://github.com/cornell-dti/carriage-web
pratyush1712 Mar 7, 2023
0bf9ccb
fixed removing aria-descibedby in production
pratyush1712 Mar 7, 2023
d2d16dc
remove console.log
pratyush1712 Mar 7, 2023
746e675
fixed notification design
pratyush1712 Mar 8, 2023
6090796
reverting back to usage of Carriage initials
pratyush1712 Mar 8, 2023
e01d2dc
delete mock test data
pratyush1712 Mar 8, 2023
5cc9d41
compress
pratyush1712 Mar 8, 2023
a6f78be
remoce unused useId
pratyush1712 Mar 8, 2023
154e934
Merge branch 'master' into pratyush/notification-design
pratyush1712 May 18, 2023
cdffcce
save
pratyush1712 Aug 16, 2023
9103855
Merge branch 'pratyush/notification-design' of https://github.com/cor…
pratyush1712 Oct 1, 2023
ee8d640
Merge branch 'master' of https://github.com/cornell-dti/carriage-web
pratyush1712 Oct 22, 2023
133c8a5
resolve issues
pratyush1712 Oct 24, 2023
568a2e3
add documentation for truncate function
pratyush1712 Oct 29, 2023
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
15 changes: 11 additions & 4 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions frontend/src/components/Notification/Message.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react';
import styles from './notification.module.css';
import moment from 'moment';
import { useState } from 'react';
import { notificationBadge } from '../../icons/other';

type Message = {
key: number;
time: Date;
title: string;
body: string;
read: boolean;
markAsRead: (key: number) => void;
};

/**
* Truncates a string to a maximum length of 30 characters.
* If the string is longer than 30 characters, it appends '...' to the end.
*
* @param {string} str - The string to be truncated.
* @returns {string} - The truncated string. If the original string is 30 characters or fewer, it returns the original string.
*/
function truncate(str: string) {
if (str.length <= 30) {
return str;
}
return `${str.slice(0, 30)}...`;
}

const DisplayMessage = ({
key,
time,
title,
body,
read,
markAsRead,
}: Message) => {
const [trunc, setTrunc] = useState<boolean>(body.length > 30);
const [viewed, setViewed] = useState<boolean>(read);
const onViewClick = () => {
setViewed(true);
markAsRead(key);
setTrunc((prev) => !prev);
};
return (
<div key={key} className={styles.body}>
{viewed ? null : (
<img
src={notificationBadge}
className={styles.messageBadge}
alt="notification badge"
/>
)}
<div className={styles.user}>
<div className={styles.avatar}>
<span className={styles.initials}>{title.slice(0, 1)}</span>
</div>
</div>
<div className={styles.msg}>
<p className={styles.date}>{moment(time).format('MMMM Do')}</p>
<p>{trunc ? truncate(body) : body}</p>
</div>
<div className={styles.link} onClick={onViewClick}>
{trunc ? 'View More' : 'Less'}
</div>
</div>
);
};

export default DisplayMessage;
34 changes: 14 additions & 20 deletions frontend/src/components/Notification/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import styles from './notification.module.css';
import 'reactjs-popup/dist/index.css';
import { notificationBadge, notificationBell } from '../../icons/other';
import { Ride } from '../../types';
import DisplayMessage from './Message';

type Message = {
key: number;
time: Date;
title: string;
body: string;
read: boolean;
};

type NotificationData = {
Expand All @@ -21,47 +24,38 @@ type NotificationData = {
sentTime: string;
};

const truncate = (str: string, num: number) => {
if (str.length <= num) {
return str;
}
return `${str.slice(0, num)}...`;
};

const Notification = () => {
const [newMessages, setNewMessages] = useState<Message[]>([]);
const [messages, setMessages] = useState<Message[]>([]);
const [notify, setNotify] = useState(false);
const popupId = useId();
const markAsRead = (key: number) => {
setMessages((prevMessages) =>
prevMessages.map((message) =>
message.key === key ? { ...message, read: true } : message
)
);
};

useEffect(() => {
navigator.serviceWorker.addEventListener('message', (event) => {
const { body, ride, sentTime, title }: NotificationData = event.data;
const newMsg = {
key: newMessages.length + 1,
time: new Date(sentTime),
title,
body,
day: ride.startTime,
read: false,
};
setNewMessages([newMsg, ...newMessages]);
setNotify(true);
});
}, []);

const mapMessages = (msgs: Message[]) =>
msgs.map(({ time, title, body }, i) => (
<div key={i} className={styles.body}>
<div className={styles.user}>
<div className={styles.avatar}>
<span className={styles.initials}>C</span>
</div>
</div>
<div className={styles.msg}>
<p className={styles.date}>{moment(time).format('MMMM Do')}</p>
<p>{body}</p>
</div>
<div className={styles.link}>View</div>
</div>
msgs.map((message) => (
<DisplayMessage {...message} key={message.key} markAsRead={markAsRead} />
));

useEffect(() => {
Expand Down
15 changes: 13 additions & 2 deletions frontend/src/components/Notification/notification.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@
}

.badge {
width: 44px;
height: 44px;
position: absolute;
right: 6px;
top: 3px;
width: 10px;
height: 10px;
}

.messageBadge {
position: absolute;
left: 15px;
width: 10px;
height: 10px;
}

.body {
Expand Down Expand Up @@ -66,6 +76,7 @@
}

.link {
cursor: pointer;
font-size: 10px;
color: #0084f4;
}
8 changes: 3 additions & 5 deletions frontend/src/components/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const Sidebar = ({ type, children }: SidebarProps) => {
key={path}
onClick={() => setSelected(path)}
className={styles.icon}
aria-current={path === selected ? 'page' : undefined}
aria-label={caption}
to={path}
>
<div
Expand All @@ -98,11 +100,7 @@ const Sidebar = ({ type, children }: SidebarProps) => {
: styles.circle
}
>
<a
href={path}
aria-current={path === selected ? 'page' : undefined}
></a>
<img alt={''} src={icon} />
<img alt={caption} src={icon} />
</div>
</Link>
{caption}
Expand Down