Skip to content

Commit 76272a7

Browse files
committed
Updates dashboard announcement to use ContentfulLoader
1 parent c6dc3c7 commit 76272a7

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

src/shared/components/Dashboard/Announcement/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ export default function Announcement({
153153
}
154154

155155
Announcement.defaultProps = {
156+
assets: {},
156157
preview: false,
157158
};
158159

159160
Announcement.propTypes = {
160-
assets: PT.shape.isRequired,
161+
assets: PT.shape(),
161162
announcement: PT.shape({
162163
fields: PT.shape({
163164
backgroundImage: PT.shape({

src/shared/components/sandbox/cms/Testing.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _ from 'lodash';
1+
// import _ from 'lodash';
22
import ContentfulLoader from 'containers/ContentfulLoader';
33
// import PT from 'prop-types';
44
import React from 'react';
@@ -22,7 +22,7 @@ export default class Testing extends React.Component {
2222
setTimeout(() => this.setState({
2323
entryQueries: {
2424
content_type: 'viewport',
25-
}
25+
},
2626
}), 9000);
2727
}
2828

src/shared/containers/ContentfulLoader.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ class ContentfulLoader extends React.Component {
182182
entryIds,
183183
entryQueries,
184184
maxage,
185+
preview,
185186
} = this.props;
186187
const minTimestamp = Date.now() - maxage;
187-
const res = {};
188+
const res = { preview: Boolean(preview) };
188189

189190
res.assets = findData(assets, assetIds, assetQueries, minTimestamp);
190191
if (!res.assets) return null;
@@ -313,14 +314,13 @@ class ContentfulLoader extends React.Component {
313314
}
314315

315316
render() {
316-
const { render, renderPlaceholder } = this.props;
317+
const { render, renderPlaceholder: Placeholder } = this.props;
317318
const data = this.findRequestedData();
318319

319320
/* Some of the required data still pending to load: render a placeholder,
320321
* or nothing. */
321322
if (!data) {
322-
return _.isFunction(renderPlaceholder)
323-
? renderPlaceholder() : renderPlaceholder;
323+
return _.isFunction(Placeholder) ? <Placeholder /> : Placeholder;
324324
}
325325

326326
/* Bingo: render the child component with requested data. */

src/shared/containers/Dashboard/Announcement.jsx

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import _ from 'lodash';
66
import actions from 'actions/cms/dashboard/announcements';
77
import Announcement from 'components/Dashboard/Announcement';
8+
import ContentfulLoader from 'containers/ContentfulLoader';
89
import cookies from 'browser-cookies';
10+
import LoadingIndicator from 'components/LoadingIndicator';
11+
import moment from 'moment';
912
import PT from 'prop-types';
1013
import React from 'react';
1114
import shortId from 'shortid';
@@ -16,6 +19,11 @@ const COOKIE = 'lastSeenDashboardAnnouncement';
1619
const MAXAGE = 10 * 60 * 1000;
1720

1821
class AnnouncementContainer extends React.Component {
22+
constructor(props) {
23+
super(props);
24+
this.now = moment().format();
25+
}
26+
1927
componentDidMount() {
2028
const {
2129
activeLoading,
@@ -58,27 +66,52 @@ class AnnouncementContainer extends React.Component {
5866

5967
render() {
6068
const {
61-
active,
62-
activeAssets,
63-
activeLoading,
6469
hidePreviewMetaData,
65-
preview,
66-
previewAssets,
67-
previewLoading,
6870
previewId,
6971
show,
7072
switchShowAnnouncement,
7173
} = this.props;
7274

7375
return (
74-
<Announcement
75-
assets={previewId ? previewAssets : activeAssets}
76-
announcement={previewId ? preview : active}
77-
hidePreviewMetaData={hidePreviewMetaData}
78-
loading={previewId ? previewLoading : activeLoading}
76+
<ContentfulLoader
77+
entryQueries={{
78+
content_type: 'dashboardAnnouncement',
79+
'fields.startDate': {
80+
lt: this.now,
81+
},
82+
'fields.endDate': {
83+
gt: this.now,
84+
},
85+
limit: 1,
86+
order: '-fields.startDate',
87+
}}
7988
preview={Boolean(previewId)}
80-
show={show}
81-
switchShow={switchShowAnnouncement}
89+
render={(data) => {
90+
let announcement = data.entries.matches[0].items[0];
91+
if (!announcement) return null;
92+
announcement = data.entries.items[announcement];
93+
const backgroundAssetId =
94+
_.get(announcement.fields.backgroundImage, 'sys.id');
95+
return (
96+
<ContentfulLoader
97+
assetIds={backgroundAssetId}
98+
preview={data.preview}
99+
render={data2 => (
100+
<Announcement
101+
assets={data2.assets.items}
102+
announcement={announcement}
103+
hidePreviewMetaData={hidePreviewMetaData}
104+
loading={false}
105+
preview={data.preview}
106+
show={show}
107+
switchShow={switchShowAnnouncement}
108+
/>
109+
)}
110+
renderPlaceholder={LoadingIndicator}
111+
/>
112+
);
113+
}}
114+
renderPlaceholder={LoadingIndicator}
82115
/>
83116
);
84117
}
@@ -91,16 +124,12 @@ AnnouncementContainer.defaultProps = {
91124

92125
AnnouncementContainer.propTypes = {
93126
active: PT.shape().isRequired,
94-
activeAssets: PT.shape().isRequired,
95127
activeLoading: PT.bool.isRequired,
96128
activeTimestamp: PT.number.isRequired,
97129
hidePreviewMetaData: PT.bool,
98130
getActive: PT.func.isRequired,
99131
getPreview: PT.func.isRequired,
100-
preview: PT.shape().isRequired,
101-
previewAssets: PT.shape().isRequired,
102132
previewId: PT.string,
103-
previewLoading: PT.func.isRequired,
104133
show: PT.bool.isRequired,
105134
switchShowAnnouncement: PT.func.isRequired,
106135
};

0 commit comments

Comments
 (0)