Skip to content

Commit bb86052

Browse files
JakobMiesnerkpsherva
authored andcommitted
banner: add support for new invenio-banners version and multiple banners
1 parent b40e875 commit bb86052

File tree

10 files changed

+71
-53
lines changed

10 files changed

+71
-53
lines changed

src/lib/api/banners/banner.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { http } from '@api/base';
2+
import { getSearchTotal } from '@api/utils';
23

34
const bannerURL = '/banners/';
45

56
const getActive = async () => {
67
const URLPath = window.location.pathname;
7-
return await http.get(`${bannerURL}active`, {
8-
params: { url_path: URLPath },
8+
const response = await http.get(`${bannerURL}`, {
9+
params: { url_path: URLPath, active: true },
910
});
11+
12+
response.data.total = getSearchTotal(response.data.hits);
13+
response.data.hits = response.data.hits.hits;
14+
15+
return response;
1016
};
1117

1218
export const bannerApi = {

src/lib/authentication/pages/Login/Login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parseParams } from '@authentication/utils';
22
import { EnvironmentLabel } from '@components/EnvironmentLabel';
33
import { Media } from '@components/Media';
4-
import { Banner } from '@components/Banner';
4+
import { Banners } from '@components/Banners';
55
import { Notifications } from '@components/Notifications';
66
import { invenioConfig } from '@config';
77
import { goTo } from '@history';
@@ -248,7 +248,7 @@ class Login extends Component {
248248
return (
249249
<>
250250
<Overridable id="Login.extras">
251-
<Banner />
251+
<Banners />
252252
</Overridable>
253253
<LoginLayout
254254
hasError={hasError}

src/lib/components/Banner/index.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ const BannerCmp = ({ message, category }) => {
1818
default:
1919
break;
2020
}
21+
2122
return (
2223
<Overridable
23-
id="Banner.layout"
24+
id="Banners.layout"
2425
message={message}
2526
category={category}
2627
colorProp={colorProp}
@@ -37,45 +38,56 @@ BannerCmp.propTypes = {
3738
category: PropTypes.string.isRequired,
3839
};
3940

40-
class Banner extends Component {
41+
class Banners extends Component {
4142
componentDidMount() {
42-
const { fetchBanner, resetBanner } = this.props;
43-
resetBanner();
44-
fetchBanner();
45-
this.intervalFetchBannerId = setInterval(
46-
fetchBanner,
43+
const { fetchBanners, resetBanners } = this.props;
44+
resetBanners();
45+
fetchBanners();
46+
this.intervalfetchBannersId = setInterval(
47+
fetchBanners,
4748
FETCH_BANNER_EVERY_SECS * 1000
4849
);
4950
}
5051

5152
componentWillUnmount() {
52-
this.intervalFetchBannerId && clearInterval(this.intervalFetchBannerId);
53+
this.intervalfetchBannersId && clearInterval(this.intervalfetchBannersId);
5354
}
5455

5556
render() {
56-
const { banner, children } = this.props;
57+
const { banners, children } = this.props;
58+
5759
return (
5860
<>
59-
{!_isEmpty(banner) ? <BannerCmp {...banner} /> : null} {children}
61+
{!_isEmpty(banners)
62+
? banners.hits.map((banner) => (
63+
<BannerCmp key={banner.id} {...banner} />
64+
))
65+
: null}{' '}
66+
{children}
6067
</>
6168
);
6269
}
6370
}
6471

65-
Banner.propTypes = {
72+
Banners.propTypes = {
6673
children: PropTypes.node,
6774
/* REDUX */
68-
banner: PropTypes.shape({
69-
message: PropTypes.string,
70-
category: PropTypes.string,
75+
banners: PropTypes.shape({
76+
hits: PropTypes.arrayOf(
77+
PropTypes.shape({
78+
id: PropTypes.string.isRequired,
79+
message: PropTypes.string.isRequired,
80+
category: PropTypes.string.isRequired,
81+
})
82+
),
7183
}),
72-
fetchBanner: PropTypes.func.isRequired,
73-
resetBanner: PropTypes.func.isRequired,
84+
fetchBanners: PropTypes.func.isRequired,
85+
resetBanners: PropTypes.func.isRequired,
7486
};
7587

76-
Banner.defaultProps = {
77-
banner: null,
88+
Banners.defaultProps = {
89+
banners: null,
7890
children: null,
7991
};
8092

81-
export default Overridable.component('Banner', Banner);
93+
export default Overridable.component('Banners', Banners);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { connect } from 'react-redux';
2+
import BannerComponent from './Banners';
3+
import { fetchBanners, resetBanners } from './state/actions';
4+
5+
const mapStateToProps = (state) => ({
6+
banners: state.banners.data,
7+
error: state.banners.error,
8+
});
9+
10+
const mapDispatchToProps = (dispatch) => ({
11+
fetchBanners: () => dispatch(fetchBanners()),
12+
resetBanners: () => dispatch(resetBanners()),
13+
});
14+
15+
export const Banners = connect(
16+
mapStateToProps,
17+
mapDispatchToProps
18+
)(BannerComponent);

src/lib/components/Banner/state/actions.js renamed to src/lib/components/Banners/state/actions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { bannerApi } from '@api/banners';
22

3-
export const BANNER_RESET = 'fetchBanner/RESET';
4-
export const BANNER_SUCCESS = 'fetchBanner/SUCCESS';
5-
export const BANNER_HAS_ERROR = 'fetchBanner/HAS_ERROR';
3+
export const BANNER_RESET = 'fetchBanners/RESET';
4+
export const BANNER_SUCCESS = 'fetchBanners/SUCCESS';
5+
export const BANNER_HAS_ERROR = 'fetchBanners/HAS_ERROR';
66

7-
export const resetBanner = () => {
7+
export const resetBanners = () => {
88
return async (dispatch) =>
99
dispatch({
1010
type: BANNER_RESET,
1111
});
1212
};
1313

14-
export const fetchBanner = () => {
14+
export const fetchBanners = () => {
1515
return async (dispatch) => {
1616
try {
1717
const response = await bannerApi.getActive();

src/lib/components/Banner/state/reducer.js renamed to src/lib/components/Banners/state/reducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const initialState = {
55
error: {},
66
};
77

8-
export const fetchBannerReducer = (state = initialState, action) => {
8+
export const fetchBannersReducer = (state = initialState, action) => {
99
switch (action.type) {
1010
case BANNER_RESET:
1111
return {

src/lib/reducers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { authenticationReducer } from '@authentication/reducer';
22
import { deleteRecordModalReducer } from '@components/backoffice/DeleteRecordModal/reducer';
3-
import { fetchBannerReducer } from '@components/Banner/state/reducer';
3+
import { fetchBannersReducer } from '@components/Banners/state/reducer';
44
import { notificationsReducer } from '@components/Notifications/reducer';
55
import { overdueLoanSendNotificationModalReducer } from '@modules/Loan/backoffice/OverdueLoanSendNotificationModal/reducer';
66
import { loanActionReducer, loanDetailsReducer } from '@modules/Loan/reducer';
@@ -123,7 +123,7 @@ export default function createILSReducer(asyncReducers) {
123123
borrowingRequestLoanExtension: borrowingRequestLoanExtensionReducer,
124124
itemsCheckIn: itemsCheckInReducer,
125125
checkOut: checkOutReducer,
126-
banner: fetchBannerReducer,
126+
banners: fetchBannersReducer,
127127
bulkLoanExtend: patronBulkExtendLoans,
128128
...asyncReducers,
129129
});

src/lib/routes/backoffice/BackOffice.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Sidebar } from '@components/backoffice/Sidebar';
2-
import { Banner } from '@components/Banner';
2+
import { Banners } from '@components/Banners';
33
import { Notifications } from '@components/Notifications';
44
import PropTypes from 'prop-types';
55
import React, { Component } from 'react';
@@ -12,7 +12,7 @@ export class BackOffice extends Component {
1212
return (
1313
<>
1414
<Overridable id="BackOffice.extras">
15-
<Banner />
15+
<Banners />
1616
</Overridable>
1717
<div className="backoffice">
1818
<div className="bo-sidebar">

src/lib/routes/frontsite/Frontsite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Banner } from '@components/Banner';
1+
import { Banners } from '@components/Banners';
22
import { NotFound } from '@components/HttpErrors';
33
import { ILSFooter } from '@components/ILSFooter';
44
import { ILSMenu } from '@components/ILSMenu';
@@ -38,7 +38,7 @@ export default class FrontSite extends Component {
3838
<Notifications className="compact" />
3939
<Container fluid className="fs-content">
4040
<Overridable id="FrontSite.extras">
41-
<Banner />
41+
<Banners />
4242
</Overridable>
4343
<Switch>
4444
<Route

0 commit comments

Comments
 (0)