- added pagination for notifications (non beneficiary);

This commit is contained in:
Vitalii Kiiko
2025-01-15 11:50:35 +01:00
parent e0e65aa21c
commit 5d740c1069
20 changed files with 259 additions and 78 deletions

View File

@@ -17,12 +17,13 @@ const NotificationItemChosen = ({ item, closeFn, markReadFn }) => {
<span>{getDateFromISOstring(item.createdDate)}</span>
{item.message}
<Button
{item.status === 'UNREAD'
? <Button
style={{marginTop: '20px'}}
type="button"
outlined
onClick={() => markReadFn(item.id)}
label={__('Letto', 'gepafin')}/>
label={__('Letto', 'gepafin')}/> : null}
</div>
)
}

View File

@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { __ } from '@wordpress/i18n';
import { head, isEmpty, pathOr } from 'ramda';
import SockJS from 'sockjs-client';
@@ -19,6 +19,7 @@ import { Sidebar } from 'primereact/sidebar';
import { TabPanel, TabView } from 'primereact/tabview';
import NotificationItem from './components/NotificationItem';
import NotificationItemChosen from './components/NotificationItemChosen';
import PaginatorBasic from '../PaginatorBasic';
const socketUrl = process.env.REACT_APP_API_ADDRESS_WS;
@@ -35,22 +36,22 @@ const NotificationsSidebar = () => {
const stomp = useRef(null);
const [currentSubscription, setCurrentSubscription] = useState(null);
const [isConnected, setIsConnected] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [totalRecordsNum, setTotalRecordsNum] = useState(0);
const [totalPagesNum, setTotalPagesNum] = useState(0);
const perPage = 10;
// Handle tab change
const handleTabChange = (e) => {
setActiveIndex(e.index);
fetchTabData(e.index);
};
const fetchTabData = (index) => {
setChosenMsg({});
if (0 === index) {
fetchMessages();
} else {
fetchMessages('READ');
if (e.index === activeIndex) {
return
}
}
setTotalRecordsNum(0);
setTotalPagesNum(0);
setChosenMsg({});
setActiveIndex(e.index);
setCurrentPage(1);
};
const chooseNotification = (id) => {
const properItems = activeIndex === 0 ? notifications : notificationsRead;
@@ -64,10 +65,27 @@ const NotificationsSidebar = () => {
setChosenMsg({});
}
const fetchMessages = (status = 'UNREAD') => {
const getPaginationQuery = (status = 'UNREAD', curPage = 1) => {
return {
'globalFilters': {
'page': curPage,
'limit': perPage,
'sortBy': {
'columnName': 'id',
'sortDesc': true
}
},
'status': [
status
]
}
}
const fetchMessages = useCallback((status = 'UNREAD') => {
const chosenCompanyId = storeGet.main.chosenCompanyId();
const userData = storeGet.main.userData();
const role = pathOr('', ['role', 'roleType'], userData);
const bodyParams = getPaginationQuery(status, currentPage);
if (currentSubscription) {
//console.log('UNsubscribed')
@@ -77,37 +95,72 @@ const NotificationsSidebar = () => {
if (userData.id && chosenCompanyId !== 0 && role === 'ROLE_BENEFICIARY') {
setLoading(true);
NotificationService.getNotifications(
userData.id,
status === 'UNREAD' ? getNotifications : getNotificationsRead,
errGetNotifications,
[
['status', status],
['companyId', chosenCompanyId]
]
);
if (isConnected && socket.current) {
subscribeTo(`/topic/notifications_user_${userData.id}_company_${chosenCompanyId}`)
}
} else if (userData.id && role !== 'ROLE_BENEFICIARY') {
setLoading(true);
NotificationService.getNotifications(
NotificationService.getNotificationsByCompanyId(
userData.id,
chosenCompanyId,
status === 'UNREAD' ? getNotifications : getNotificationsRead,
errGetNotifications,
[
['status', status]
]
);
if (isConnected && socket.current) {
subscribeTo(`/topic/notifications_user_${userData.id}_company_${chosenCompanyId}`)
}
} else if (userData.id && role !== 'ROLE_BENEFICIARY') {
setLoading(true);
/*NotificationService.getNotifications(
userData.id,
status === 'UNREAD' ? getNotifications : getNotificationsRead,
errGetNotifications,
[
['status', status]
]
);*/
NotificationService.getNotificationsPagination(
userData.id,
bodyParams,
status === 'UNREAD' ? getNotificationsPagi : getNotificationsReadPagi,
errGetNotifications
);
if (isConnected && socket.current) {
subscribeTo(`/topic/notifications_user_${userData.id}`)
}
}
}, [currentPage]);
const getNotificationsPagi = (resp) => {
if (resp.status === 'SUCCESS') {
const { body, totalRecords, currentPage, totalPages } = resp.data;
setNotifications(body);
setTotalRecordsNum(totalRecords);
setTotalPagesNum(totalPages);
if (currentPage > totalPages) {
setCurrentPage(totalPages);
}
}
set404FromErrorResponse(resp);
setLoading(false);
}
const getNotificationsReadPagi = (resp) => {
if (resp.status === 'SUCCESS') {
const { body, totalRecords, currentPage, totalPages } = resp.data;
setNotificationsRead(body);
setTotalRecordsNum(totalRecords);
setTotalPagesNum(totalPages);
if (currentPage > totalPages) {
setCurrentPage(totalPages);
}
}
set404FromErrorResponse(resp);
setLoading(false);
}
const getNotifications = (resp) => {
if (resp.status === 'SUCCESS') {
setNotifications(resp.data);
setTotalRecordsNum(resp.data.length);
}
set404FromErrorResponse(resp);
setLoading(false);
@@ -116,6 +169,7 @@ const NotificationsSidebar = () => {
const getNotificationsRead = (resp) => {
if (resp.status === 'SUCCESS') {
setNotificationsRead(resp.data);
setTotalRecordsNum(resp.data.length);
}
set404FromErrorResponse(resp);
setLoading(false);
@@ -139,6 +193,7 @@ const NotificationsSidebar = () => {
const msgs = notificationsRead.map(o => o.id === resp.data.id ? resp.data : o);
setNotificationsRead(msgs);
}
setTotalRecordsNum(totalRecordsNum - 1);
}
set404FromErrorResponse(resp);
}
@@ -152,7 +207,7 @@ const NotificationsSidebar = () => {
stomp.current = Stomp.over(socket.current);
stomp.current.configure({
debug: function(str) {
debug: function (str) {
//console.log(str);
},
reconnectDelay: 5000,
@@ -190,10 +245,22 @@ const NotificationsSidebar = () => {
setCurrentSubscription(subscription);
}
const onPageChange = (num) => {
setCurrentPage(num);
};
useEffect(() => {
fetchMessages();
}, [chosenCompanyId, userData.id, isConnected]);
useEffect(() => {
if (0 === activeIndex) {
fetchMessages();
} else {
fetchMessages('READ');
}
}, [currentPage, activeIndex]);
useEffect(() => {
connectWebSocket();
@@ -215,7 +282,7 @@ const NotificationsSidebar = () => {
<>
<i className="pi pi-bell p-overlay-badge topBar__icon notificationsIcon"
onClick={() => setNotificationsVisible(true)}>
<Badge value={notifications.filter(o => o.status === 'UNREAD').length}></Badge>
<Badge value={totalRecordsNum}></Badge>
</i>
<Sidebar
className="notificationsSidebar"
@@ -234,12 +301,19 @@ const NotificationsSidebar = () => {
closeFn={closeChosenMsg}
markReadFn={makeNotificationRead}/>
: (notifications.length > 0
? <ul className="notificationsSidebar__list">
{notifications.map(o => <NotificationItem
key={o.id}
item={o}
clickFn={chooseNotification}/>)}
</ul>
? <>
<ul className="notificationsSidebar__list">
{notifications.map(o => <NotificationItem
key={o.id}
item={o}
clickFn={chooseNotification}/>)}
</ul>
<PaginatorBasic
totalPages={totalPagesNum}
currentPage={currentPage}
clickFn={onPageChange}
/>
</>
: <div className="notificationsSidebar__loading">
<i className="pi pi-megaphone" style={{ fontSize: '2rem' }}></i>
{__('Vuoto', 'gepafin')}
@@ -256,17 +330,24 @@ const NotificationsSidebar = () => {
closeFn={closeChosenMsg}
markReadFn={makeNotificationRead}/>
: (notificationsRead.length > 0
? <ul className="notificationsSidebar__list">
{notificationsRead.map(o => <NotificationItem
key={o.id}
item={o}
clickFn={chooseNotification}/>)}
</ul>
:
<div className="notificationsSidebar__loading">
<i className="pi pi-megaphone" style={{ fontSize: '2rem' }}></i>
{__('Vuoto', 'gepafin')}
</div>)}
? <>
<ul className="notificationsSidebar__list">
{notificationsRead.map(o => <NotificationItem
key={o.id}
item={o}
clickFn={chooseNotification}/>)}
</ul>
<PaginatorBasic
totalPages={totalPagesNum}
currentPage={currentPage}
clickFn={onPageChange}
/>
</>
:
<div className="notificationsSidebar__loading">
<i className="pi pi-megaphone" style={{ fontSize: '2rem' }}></i>
{__('Vuoto', 'gepafin')}
</div>)}
</TabPanel>
</TabView>
</Sidebar>

View File

@@ -0,0 +1,59 @@
import React from 'react';
import { __ } from '@wordpress/i18n';
const PaginatorBasic = ({
currentPage = 0,
totalPages = 0,
clickFn = () => {
}
}) => {
const handleClick = (num) => {
const newNum = num < 0
? 0
: num > totalPages ? totalPages : num;
clickFn(newNum);
}
const prevDisabled = currentPage <= 1;
const nextDisabled = currentPage >= totalPages
return (
totalPages !== 0
? <div className="p-paginator p-component" data-pc-name="paginator" data-pc-section="root">
<button
type="button"
className={`p-paginator-prev p-paginator-element p-link${prevDisabled ? ' p-disabled' : ''}`}
disabled={prevDisabled}
onClick={prevDisabled ? () => {
} : () => handleClick(currentPage - 1)}
aria-label={__('Pagina precedente', 'gepafin')}
data-pc-section="prevpagebutton">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg"
className="p-icon p-paginator-icon" aria-hidden="true" data-pc-section="prevpageicon">
<path
d="M8.75 11.185C8.65146 11.1854 8.55381 11.1662 8.4628 11.1284C8.37179 11.0906 8.28924 11.0351 8.22 10.965L4.72 7.46496C4.57955 7.32433 4.50066 7.13371 4.50066 6.93496C4.50066 6.73621 4.57955 6.54558 4.72 6.40496L8.22 2.93496C8.36095 2.84357 8.52851 2.80215 8.69582 2.81733C8.86312 2.83252 9.02048 2.90344 9.14268 3.01872C9.26487 3.134 9.34483 3.28696 9.36973 3.4531C9.39463 3.61924 9.36303 3.78892 9.28 3.93496L6.28 6.93496L9.28 9.93496C9.42045 10.0756 9.49934 10.2662 9.49934 10.465C9.49934 10.6637 9.42045 10.8543 9.28 10.995C9.13526 11.1257 8.9448 11.1939 8.75 11.185Z"
fill="currentColor"></path>
</svg>
</button>
<span aria-live="polite" className="p-paginator-current" data-pc-section="current">
({currentPage} {__('di', 'gepafin')} {totalPages})
</span>
<button
type="button"
disabled={nextDisabled}
onClick={nextDisabled ? () => {
} : () => handleClick(currentPage + 1)}
className={`p-paginator-next p-paginator-element p-link${nextDisabled ? ' p-disabled' : ''}`}
aria-label={__('Pagina successiva', 'gepafin')}
data-pc-section="nextpagebutton">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg"
className="p-icon p-paginator-icon" aria-hidden="true" data-pc-section="nextpageicon">
<path
d="M5.25 11.1728C5.14929 11.1694 5.05033 11.1455 4.9592 11.1025C4.86806 11.0595 4.78666 10.9984 4.72 10.9228C4.57955 10.7822 4.50066 10.5916 4.50066 10.3928C4.50066 10.1941 4.57955 10.0035 4.72 9.86283L7.72 6.86283L4.72 3.86283C4.66067 3.71882 4.64765 3.55991 4.68275 3.40816C4.71785 3.25642 4.79932 3.11936 4.91585 3.01602C5.03238 2.91268 5.17819 2.84819 5.33305 2.83149C5.4879 2.81479 5.64411 2.84671 5.78 2.92283L9.28 6.42283C9.42045 6.56346 9.49934 6.75408 9.49934 6.95283C9.49934 7.15158 9.42045 7.34221 9.28 7.48283L5.78 10.9228C5.71333 10.9984 5.63193 11.0595 5.5408 11.1025C5.44966 11.1455 5.35071 11.1694 5.25 11.1728Z"
fill="currentColor"></path>
</svg>
</button>
</div> : null
)
}
export default PaginatorBasic;