From b0737da689fea10bca30aceed3874051bca8ac74 Mon Sep 17 00:00:00 2001 From: Ahtisham Shahid Date: Thu, 1 Sep 2022 19:58:42 +0500 Subject: [PATCH] feat: added blackout dates implementation (#271) * feat: added blackout dates implementation Co-authored-by: Mehak Nasir --- src/discussions/comments/comment/Comment.jsx | 16 +++++++--- .../comments/comment/ResponseEditor.jsx | 8 ++++- src/discussions/common/ActionsDropdown.jsx | 10 ++++++- .../common/ActionsDropdown.test.jsx | 2 ++ src/discussions/data/selectors.js | 2 ++ .../posts/post-actions-bar/PostActionsBar.jsx | 27 ++++++++++------- src/discussions/utils.js | 30 ++++++++++++++++--- 7 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/discussions/comments/comment/Comment.jsx b/src/discussions/comments/comment/Comment.jsx index df011a0a..1d0cf5e2 100644 --- a/src/discussions/comments/comment/Comment.jsx +++ b/src/discussions/comments/comment/Comment.jsx @@ -10,7 +10,9 @@ import { Button, useToggle } from '@edx/paragon'; import HTMLLoader from '../../../components/HTMLLoader'; import { ContentActions } from '../../../data/constants'; import { AlertBanner, DeleteConfirmation, EndorsedAlertBanner } from '../../common'; +import { selectBlackoutDate } from '../../data/selectors'; import { fetchThread } from '../../posts/data/thunks'; +import { inBlackoutDateRange } from '../../utils'; import CommentIcons from '../comment-icons/CommentIcons'; import { selectCommentCurrentPage, selectCommentHasMorePages, selectCommentResponses } from '../data/selectors'; import { editComment, fetchCommentResponses, removeComment } from '../data/thunks'; @@ -36,6 +38,7 @@ function Comment({ const [isReplying, setReplying] = useState(false); const hasMorePages = useSelector(selectCommentHasMorePages(comment.id)); const currentPage = useSelector(selectCommentCurrentPage(comment.id)); + const blackoutDateRange = useSelector(selectBlackoutDate); useEffect(() => { // If the comment has a parent comment, it won't have any children, so don't fetch them. @@ -124,13 +127,18 @@ function Comment({ /> ) : ( <> - {!isClosedPost + {(!isClosedPost && !inBlackoutDateRange(blackoutDateRange)) && ( - + )} + ) )} diff --git a/src/discussions/comments/comment/ResponseEditor.jsx b/src/discussions/comments/comment/ResponseEditor.jsx index e0f650bd..2035bd80 100644 --- a/src/discussions/comments/comment/ResponseEditor.jsx +++ b/src/discussions/comments/comment/ResponseEditor.jsx @@ -2,10 +2,13 @@ import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; +import { useSelector } from 'react-redux'; import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; import { Button } from '@edx/paragon'; +import { selectBlackoutDate } from '../../data/selectors'; +import { inBlackoutDateRange } from '../../utils'; import messages from '../messages'; import CommentEditor from './CommentEditor'; @@ -20,6 +23,8 @@ function ResponseEditor({ setAddingResponse(false); }, [postId]); + const blackoutDateRange = useSelector(selectBlackoutDate); + return addingResponse ? (
@@ -29,7 +34,8 @@ function ResponseEditor({ onCloseEditor={() => setAddingResponse(false)} />
- ) : ( + ) + : !inBlackoutDateRange(blackoutDateRange) && (
+ { + !inBlackoutDateRange(blackoutDateRange) && ( + + ) + } {inContext && ( <>
diff --git a/src/discussions/utils.js b/src/discussions/utils.js index 28f4b964..f9d77ddb 100644 --- a/src/discussions/utils.js +++ b/src/discussions/utils.js @@ -229,10 +229,10 @@ export function postMessageToParent(type, payload = {}) { export const isPostPreviewAvailable = (htmlNode) => { const containsImage = htmlNode.match(/()/); const isLatex = htmlNode.match(/(\${1,2})((?:\\.|.)*)/) - || htmlNode.match(/(\[mathjax](.+?))+/) - || htmlNode.match(/(\[mathjaxinline](.+?))+/) - || htmlNode.match(/(\\\[(.+?))+/) - || htmlNode.match(/(\\\((.+?))+/); + || htmlNode.match(/(\[mathjax](.+?))+/) + || htmlNode.match(/(\[mathjaxinline](.+?))+/) + || htmlNode.match(/(\\\[(.+?))+/) + || htmlNode.match(/(\\\((.+?))+/); if (containsImage || isLatex || htmlNode === '') { return false; @@ -253,3 +253,25 @@ export const filterPosts = (posts, filterBy, sortBy = 'createdAt', order = 'desc post => (filterBy.startsWith('un') ? !post[filterBy.slice(2)] : post[filterBy]), ), [sortBy], [order], ); + +/** + * Helper function to make a check if date is in given range + * @param {Date} date this date to be checked in range + * @param {Date} start start date + * @param {Date} end end date + */ +export function dateInDateRange(date, start, end) { + return date >= start && date <= end; +} + +/** + * Helper function to make a check if date is in given range + * @param {array} blackoutDateRanges start date + * @return Boolean + */ +export function inBlackoutDateRange(blackoutDateRanges) { + const now = new Date(); + return blackoutDateRanges.some( + (blackoutDateRange) => dateInDateRange(now, new Date(blackoutDateRange.start), new Date(blackoutDateRange.end)), + ); +}