Skip to content

Commit

Permalink
only show ongoing/future pause notifications info (#988)
Browse files Browse the repository at this point in the history
* only show ongoing/future pause notifications info

CPCN-844

* compare timestamps not strings

* add helper func
  • Loading branch information
petrjasek authored Jul 11, 2024
1 parent 8d01c54 commit c2b39a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
15 changes: 5 additions & 10 deletions assets/components/NotificationPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {formatDate, gettext} from 'utils';
import {formatDate, gettext, notificationsArePaused, notificationsWillBePaused, parseISODate} from 'utils';
import {IArticle, IUser} from 'interfaces';

import NotificationListItem from './NotificationListItem';
Expand All @@ -19,15 +19,10 @@ export interface IProps {
}

export const NotificationPopup = (props: IProps) => {
const today = (new Date()).toISOString().substring(0, 10);
const pausedFrom = props.fullUser.notification_schedule?.pause_from ?? '';
const pausedTo = props.fullUser.notification_schedule?.pause_to ?? '';
const notificationsArePaused = (
pausedFrom != '' && pausedFrom <= today &&
pausedTo != '' && pausedTo >= today
);
const pausedFrom = parseISODate(props.fullUser.notification_schedule?.pause_from);
const pausedTo = parseISODate(props.fullUser.notification_schedule?.pause_to);

if (notificationsArePaused) {
if (notificationsArePaused(pausedFrom, pausedTo)) {
return (
<div className="notif__list dropdown-menu dropdown-menu-right show">
<div className='notif__list__header d-flex'>
Expand Down Expand Up @@ -78,7 +73,7 @@ export const NotificationPopup = (props: IProps) => {
</button>
</div>

{(pausedFrom != '' && pausedTo != '' && notificationsArePaused === false) && (
{(notificationsWillBePaused(pausedFrom, pausedTo)) && (
<div className='p-3'>
<div className='nh-container nh-container__text--info p-2'>
{gettext('All notifications are set to be paused from {{dateFrom}} to {{dateTo}}', {dateFrom: formatDate(pausedFrom), dateTo: formatDate(pausedTo)})}
Expand Down
10 changes: 6 additions & 4 deletions assets/user-profile/components/profile/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {connect} from 'react-redux';

import {IUser} from 'interfaces';
import {gettext, getSubscriptionTimesString, formatDate} from 'utils';
import {gettext, getSubscriptionTimesString, formatDate, parseISODate, notificationsArePaused, notificationsWillBePaused} from 'utils';

import TextInput from 'components/TextInput';
import SelectInput from 'components/SelectInput';
Expand Down Expand Up @@ -73,6 +73,8 @@ class UserProfile extends React.PureComponent<IProps> {
const {user, onChange, errors, authProviderFeatures} = this.props;
const onCancel = () => this.props.fetchUser(this.props.user._id);
const localeOptions = getLocaleInputOptions();
const pausedFrom = parseISODate(this.props.user.notification_schedule?.pause_from);
const pausedTo = parseISODate(this.props.user.notification_schedule?.pause_to);
return (
<form className="profile-content profile-content--user">
<div className="profile-content__main">
Expand Down Expand Up @@ -187,12 +189,12 @@ class UserProfile extends React.PureComponent<IProps> {

<div className='row'>
<div className="col-lg-6">
{this.props.user.notification_schedule && this.props.user.notification_schedule.pause_from != '' && this.props.user.notification_schedule.pause_to != ''
? (
{(notificationsArePaused(pausedFrom, pausedTo) || notificationsWillBePaused(pausedFrom, pausedTo)) ?
(
<div className="nh-container nh-container__text--alert">
<div className='d-flex flex-column gap-3 p-3'>
<div>
{gettext('All notifications will be paused from {{dateFrom}} to {{dateTo}}', {dateFrom: formatDate(this.props.user.notification_schedule.pause_from), dateTo: formatDate(this.props.user.notification_schedule.pause_to)})}
{gettext('All notifications will be paused from {{dateFrom}} to {{dateTo}}', {dateFrom: formatDate(pausedFrom), dateTo: formatDate(pausedTo)})}
</div>
<div>
<button
Expand Down
22 changes: 22 additions & 0 deletions assets/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,25 @@ export function getSubscriptionTimesString(user: IUser): string {
timezone: timezoneAbbreviation,
});
}

// Returns the number of seconds since epoch for the given ISO date string
export function parseISODate(date?: string): number {
return date ? Date.parse(date) : NaN;
}

// Will return number of seconds since epoch for today at 00:00:00
function getTodayTimestamp(): number {
return (new Date()).setUTCHours(0, 0, 0, 0);
}

export function notificationsArePaused(pausedFrom: number, pausedTo: number): boolean {
const today = getTodayTimestamp();

return pausedFrom <= today && pausedTo >= today;
}

export function notificationsWillBePaused(pausedFrom: number, pausedTo: number): boolean {
const today = getTodayTimestamp();

return pausedFrom > today && pausedTo > today;
}

0 comments on commit c2b39a7

Please sign in to comment.