Skip to content

Commit

Permalink
fix: humanize-date fixes: #3556 (#3565)
Browse files Browse the repository at this point in the history
* fix: humanize-date
* fix: improve notification handling and enhance date validation
  • Loading branch information
Pragadesh-45 authored Nov 30, 2024
1 parent f2cfcab commit 2f75208
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const getReadNotificationIds = () => {
return readNotificationIds;
} catch (err) {
toast.error('An error occurred while fetching read notifications');
return [];
}
};

Expand Down Expand Up @@ -58,14 +59,16 @@ export const notificationSlice = createSlice({
});
},
markNotificationAsRead: (state, action) => {
if (state.readNotificationIds.includes(action.payload.notificationId)) return;
const { notificationId } = action.payload;

if (state.readNotificationIds.includes(notificationId)) return;

const notification = state.notifications.find(
(notification) => notification.id === action.payload.notificationId
(notification) => notification.id === notificationId
);
if (!notification) return;

state.readNotificationIds.push(action.payload.notificationId);
state.readNotificationIds.push(notificationId);
setReadNotificationsIds(state.readNotificationIds);
notification.read = true;
},
Expand Down
10 changes: 9 additions & 1 deletion packages/bruno-app/src/utils/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ export const relativeDate = (dateString) => {
export const humanizeDate = (dateString) => {
// See this discussion for why .split is necessary
// https://stackoverflow.com/questions/7556591/is-the-javascript-date-object-always-one-day-off
const date = new Date(dateString.split('-'));

if (!dateString || typeof dateString !== 'string') {
return 'Invalid Date';
}
const date = new Date(dateString);
if (isNaN(date.getTime())) {
return 'Invalid Date';
}

return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
Expand Down
12 changes: 12 additions & 0 deletions packages/bruno-app/src/utils/common/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ describe('common utils', () => {
it('should return invalid date if the date is invalid', () => {
expect(humanizeDate('9999-99-99')).toBe('Invalid Date');
});

it('should return "Invalid Date" if the date is null', () => {
expect(humanizeDate(null)).toBe('Invalid Date');
});

it('should return a humanized date for a valid date in ISO format', () => {
expect(humanizeDate('2024-11-28T00:00:00Z')).toBe('November 28, 2024');
});

it('should return "Invalid Date" for a non-date string', () => {
expect(humanizeDate('some random text')).toBe('Invalid Date');
});
});

describe('relativeDate', () => {
Expand Down

0 comments on commit 2f75208

Please sign in to comment.