fix: humanize-date fixes: #3556 (#3565)

* fix: humanize-date
* fix: improve notification handling and enhance date validation
This commit is contained in:
Pragadesh-45 2024-11-30 18:51:44 +05:30 committed by GitHub
parent f2cfcab091
commit 2f752085f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 4 deletions

View File

@ -9,6 +9,7 @@ const getReadNotificationIds = () => {
return readNotificationIds; return readNotificationIds;
} catch (err) { } catch (err) {
toast.error('An error occurred while fetching read notifications'); toast.error('An error occurred while fetching read notifications');
return [];
} }
}; };
@ -58,14 +59,16 @@ export const notificationSlice = createSlice({
}); });
}, },
markNotificationAsRead: (state, action) => { 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( const notification = state.notifications.find(
(notification) => notification.id === action.payload.notificationId (notification) => notification.id === notificationId
); );
if (!notification) return; if (!notification) return;
state.readNotificationIds.push(action.payload.notificationId); state.readNotificationIds.push(notificationId);
setReadNotificationsIds(state.readNotificationIds); setReadNotificationsIds(state.readNotificationIds);
notification.read = true; notification.read = true;
}, },

View File

@ -151,7 +151,15 @@ export const relativeDate = (dateString) => {
export const humanizeDate = (dateString) => { export const humanizeDate = (dateString) => {
// See this discussion for why .split is necessary // See this discussion for why .split is necessary
// https://stackoverflow.com/questions/7556591/is-the-javascript-date-object-always-one-day-off // 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', { return date.toLocaleDateString('en-US', {
year: 'numeric', year: 'numeric',
month: 'long', month: 'long',

View File

@ -58,6 +58,18 @@ describe('common utils', () => {
it('should return invalid date if the date is invalid', () => { it('should return invalid date if the date is invalid', () => {
expect(humanizeDate('9999-99-99')).toBe('Invalid Date'); 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', () => { describe('relativeDate', () => {