Skip to content

Commit 153406e

Browse files
committed
banner: add support for new invenio-banners version and multiple banners
1 parent eddf858 commit 153406e

File tree

10 files changed

+62
-52
lines changed

10 files changed

+62
-52
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: 23 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,48 @@ 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+
const { fetchBanners, resetBanner } = this.props;
4344
resetBanner();
44-
fetchBanner();
45-
this.intervalFetchBannerId = setInterval(
46-
fetchBanner,
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) ? banners.hits.map(banner => (<BannerCmp key={banner.id} {...banner} />)) : null} {children}
6062
</>
6163
);
6264
}
6365
}
6466

65-
Banner.propTypes = {
67+
Banners.propTypes = {
6668
children: PropTypes.node,
6769
/* REDUX */
68-
banner: PropTypes.shape({
69-
message: PropTypes.string,
70-
category: PropTypes.string,
71-
}),
72-
fetchBanner: PropTypes.func.isRequired,
73-
resetBanner: PropTypes.func.isRequired,
70+
banners: PropTypes.arrayOf(
71+
PropTypes.shape({
72+
message: PropTypes.string,
73+
category: PropTypes.string,
74+
})
75+
),
76+
fetchBanners: PropTypes.func.isRequired,
77+
resetBanners: PropTypes.func.isRequired,
7478
};
7579

76-
Banner.defaultProps = {
77-
banner: null,
80+
Banners.defaultProps = {
81+
banners: null,
7882
children: null,
7983
};
8084

81-
export default Overridable.component('Banner', Banner);
85+
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, resetBanner } 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+
resetBanner: () => dispatch(resetBanner()),
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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

77
export const resetBanner = () => {
88
return async (dispatch) =>
@@ -11,7 +11,7 @@ export const resetBanner = () => {
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)