Skip to content

Commit

Permalink
refactor: loader added and resolves minor nits
Browse files Browse the repository at this point in the history
  • Loading branch information
ayesha-waris committed Jul 6, 2023
1 parent 39adc34 commit 81f339a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
28 changes: 17 additions & 11 deletions src/Notifications/NotificationSections.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useMemo } from 'react';
import { Button } from '@edx/paragon';
import { Button, Spinner } from '@edx/paragon';
import { useDispatch, useSelector } from 'react-redux';
import { useIntl } from '@edx/frontend-platform/i18n';
import isEmpty from 'lodash/isEmpty';
Expand All @@ -18,7 +18,7 @@ const NotificationSections = () => {
const selectedAppName = useSelector(selectSelectedAppName());
const notificationRequestStatus = useSelector(selectNotificationStatus());
const notifications = useSelector(selectNotificationsByIds(selectedAppName));
const { nextPage } = useSelector(selectPaginationData());
const { hasMorePages } = useSelector(selectPaginationData());
const { today = [], earlier = [] } = useMemo(
() => splitNotificationsByTime(notifications),
[notifications],
Expand Down Expand Up @@ -73,15 +73,21 @@ const NotificationSections = () => {
<div className="mt-4 px-4" data-testid="notification-tray-section">
{renderNotificationSection('today', today)}
{renderNotificationSection('earlier', earlier)}
{nextPage && notificationRequestStatus === RequestStatus.SUCCESSFUL && (
<Button
variant="primary"
className="w-100 bg-primary-500"
onClick={updatePagination}
data-testid="load-more-notifications"
>
{intl.formatMessage(messages.loadMoreNotifications)}
</Button>
{hasMorePages && notificationRequestStatus === RequestStatus.IN_PROGRESS ? (
<div className="d-flex justify-content-center p-4">
<Spinner animation="border" variant="primary" size="lg" />
</div>
) : (
hasMorePages && notificationRequestStatus === RequestStatus.SUCCESSFUL && (
<Button
variant="primary"
className="w-100 bg-primary-500"
onClick={updatePagination}
data-testid="load-more-notifications"
>
{intl.formatMessage(messages.loadMoreNotifications)}
</Button>
)
)}
</div>
);
Expand Down
12 changes: 3 additions & 9 deletions src/Notifications/data/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ const initialState = {
notifications: {},
tabsCount: {},
showNotificationsTray: false,
pagination: {
totalPages: 1,
currentPage: 1,
nextPage: null,
},
pagination: {},
};
const slice = createSlice({
name: 'notifications',
Expand All @@ -38,17 +34,15 @@ const slice = createSlice({
},
fetchNotificationSuccess: (state, { payload }) => {
const {
newNotificationIds, notificationsKeyValuePair, totalPages, currentPage, nextPage,
newNotificationIds, notificationsKeyValuePair, pagination,
} = payload;
const existingNotificationIds = state.apps[state.appName];
state.apps[state.appName] = Array.from(new Set([...existingNotificationIds, ...newNotificationIds]));
state.notifications = { ...state.notifications, ...notificationsKeyValuePair };
state.tabsCount.count -= state.tabsCount[state.appName];
state.tabsCount[state.appName] = 0;
state.notificationStatus = RequestStatus.SUCCESSFUL;
state.pagination.totalPages = totalPages;
state.pagination.currentPage = currentPage;
state.pagination.nextPage = nextPage;
state.pagination = pagination;
},
fetchNotificationsCountDenied: (state) => {
state.notificationStatus = RequestStatus.DENIED;
Expand Down
9 changes: 6 additions & 3 deletions src/Notifications/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ export const fetchNotificationList = ({ appName, page }) => (
dispatch(fetchNotificationRequest({ appName }));
const data = await getNotificationsList(appName, page);
const normalisedData = normalizeNotifications((camelCaseObject(data.results)));
dispatch(fetchNotificationSuccess({
...normalisedData, totalPages: data.num_pages, currentPage: data.current_page, nextPage: data.next,
}));
const pagination = {
numPages: data.num_pages,
currentPage: data.current_page,
hasMorePages: !!data.next,
};
dispatch(fetchNotificationSuccess({ ...normalisedData, pagination }));
} catch (error) {
if (getHttpErrorStatus(error) === 403) {
dispatch(fetchNotificationDenied(appName));
Expand Down

0 comments on commit 81f339a

Please sign in to comment.