From 3084810339640ca2790e2b94d6bc84a8557b627f Mon Sep 17 00:00:00 2001 From: diazz Date: Wed, 16 Nov 2022 09:11:22 +0700 Subject: [PATCH] fix: approvals number should increase automatically --- .../__snapshots__/JoinCommunity.jsx.snap | 1 + config/default.js | 1 + .../components/LoadingIndicator/index.jsx | 10 ++++- src/shared/containers/timeline-wall/index.jsx | 39 ++++++++++++++++--- src/shared/reducers/timelineWall.js | 5 +-- 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/__tests__/shared/components/tc-communities/__snapshots__/JoinCommunity.jsx.snap b/__tests__/shared/components/tc-communities/__snapshots__/JoinCommunity.jsx.snap index 0d9dc3b3dd..653f417362 100644 --- a/__tests__/shared/components/tc-communities/__snapshots__/JoinCommunity.jsx.snap +++ b/__tests__/shared/components/tc-communities/__snapshots__/JoinCommunity.jsx.snap @@ -65,6 +65,7 @@ exports[`Matches shallow shapshot 2`] = ` Joining... ( +const LoadingIndicator = ({ theme, className }) => ( ( ); +LoadingIndicator.defaultProps = { + className: '', +}; + LoadingIndicator.propTypes = { theme: PT.shape().isRequired, + className: PT.string, }; export default themr('LoadingIndicator', style)(LoadingIndicator); diff --git a/src/shared/containers/timeline-wall/index.jsx b/src/shared/containers/timeline-wall/index.jsx index c8c829fd05..351d35bb64 100644 --- a/src/shared/containers/timeline-wall/index.jsx +++ b/src/shared/containers/timeline-wall/index.jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import PT from 'prop-types'; import { connect } from 'react-redux'; import TopBanner from 'assets/images/timeline-wall/top-banner.png'; @@ -9,6 +9,7 @@ import cn from 'classnames'; import moment from 'moment'; import { useMediaQuery } from 'react-responsive'; import _ from 'lodash'; +import { config } from 'topcoder-react-utils'; import timelineActions from 'actions/timelineWall'; import LoadingIndicator from 'components/LoadingIndicator'; import TimelineEvents from './timeline-events'; @@ -16,8 +17,11 @@ import PendingApprovals from './pending-approvals'; import './styles.scss'; + +const FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL = _.get(config, 'TIMELINE.FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL', 0); function TimelineWallContainer(props) { const [tab, setTab] = useState(0); + const fetchingApprovalsInterval = useRef(null); const [showRightFilterMobile, setShowRightFilterMobile] = useState(false); const [selectedFilterValue, setSelectedFilterValue] = useState({ year: 0, @@ -36,6 +40,7 @@ function TimelineWallContainer(props) { getAvatar, userAvatars, pendingApprovals, + loadingApprovals, uploading, uploadResult, } = props; @@ -55,9 +60,25 @@ function TimelineWallContainer(props) { }, []); useEffect(() => { - if (authToken && isAdmin && !pendingApprovals.length) { + if (fetchingApprovalsInterval.current) { + clearInterval(fetchingApprovalsInterval.current); + fetchingApprovalsInterval.current = null; + } + if (authToken && isAdmin) { getPendingApprovals(authToken); + if (FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL) { + fetchingApprovalsInterval.current = setInterval(() => { + getPendingApprovals(authToken); + }, FETCHING_PENDING_APPROVAL_EVENTS_INTERVAL); + } } + + return () => { + if (fetchingApprovalsInterval.current) { + clearInterval(fetchingApprovalsInterval.current); + fetchingApprovalsInterval.current = null; + } + }; }, [isAdmin]); useEffect(() => { @@ -72,7 +93,7 @@ function TimelineWallContainer(props) { }, [events]); useEffect(() => { - if ((pendingApprovals || []).length) { + if (pendingApprovals.length) { _.uniqBy(pendingApprovals, 'createdBy').forEach((eventItem) => { const photoURL = _.get(userAvatars, eventItem.createdBy); if (!photoURL) { @@ -205,8 +226,10 @@ function TimelineWallContainer(props) { /> { - loading ? ( - + loadingApprovals ? ( + ) : ( ({ }, isAdmin: state.timelineWall.isAdmin, loading: state.timelineWall.loading, + loadingApprovals: state.timelineWall.loadingApprovals + && !(state.timelineWall.pendingApprovals || []).length, uploading: state.timelineWall.uploading, uploadResult: state.timelineWall.uploadResult, events: state.timelineWall.events, userAvatars: state.timelineWall.userAvatars, - pendingApprovals: state.timelineWall.pendingApprovals, + pendingApprovals: state.timelineWall.pendingApprovals || [], }); function mapDispatchToProps(dispatch) { diff --git a/src/shared/reducers/timelineWall.js b/src/shared/reducers/timelineWall.js index 0658fc73f1..be117c61fe 100644 --- a/src/shared/reducers/timelineWall.js +++ b/src/shared/reducers/timelineWall.js @@ -60,8 +60,7 @@ function onEventsDone(state, { payload }) { function onPendingApprovalInit(state) { return { ...state, - loading: true, - pendingApprovals: [], + loadingApprovals: true, }; } @@ -74,7 +73,7 @@ function onPendingApprovalDone(state, { payload }) { const approvals = _.isArray(payload) ? payload : []; return { ...state, - loading: false, + loadingApprovals: false, pendingApprovals: approvals, }; }