Skip to content

Commit bc04e24

Browse files
committed
feat: get unpublished entries
1 parent 761f94a commit bc04e24

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

packages/decap-cms-core/src/components/Collection/Entries/EntryListing.js

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import React from 'react';
33
import ImmutablePropTypes from 'react-immutable-proptypes';
44
import styled from '@emotion/styled';
55
import { Waypoint } from 'react-waypoint';
6-
import { Map } from 'immutable';
6+
import { Map, List } from 'immutable';
77
import { connect } from 'react-redux';
88

9-
import { selectUnpublishedEntry } from '../../../reducers';
9+
import { selectUnpublishedEntry, selectUnpublishedEntriesByStatus } from '../../../reducers';
1010
import { selectFields, selectInferredField } from '../../../reducers/collections';
1111
import EntryCard from './EntryCard';
1212

@@ -27,6 +27,7 @@ class EntryListing extends React.Component {
2727
cursor: PropTypes.any.isRequired,
2828
handleCursorActions: PropTypes.func.isRequired,
2929
page: PropTypes.number,
30+
getUnpublishedEntries: PropTypes.func.isRequired,
3031
};
3132

3233
hasMore = () => {
@@ -51,20 +52,46 @@ class EntryListing extends React.Component {
5152
return { titleField, descriptionField, imageField, remainingFields };
5253
};
5354

55+
getAllEntries = () => {
56+
const { entries, collections } = this.props;
57+
const collectionName = Map.isMap(collections) ? collections.get('name') : null;
58+
59+
if (!collectionName) {
60+
return entries;
61+
}
62+
63+
const unpublishedEntries = this.props.getUnpublishedEntries(collectionName);
64+
65+
if (!unpublishedEntries || unpublishedEntries.length === 0) {
66+
return entries;
67+
}
68+
69+
const unpublishedList = List(unpublishedEntries.map(entry => entry));
70+
71+
const publishedSlugs = entries.map(entry => entry.get('slug')).toSet();
72+
const uniqueUnpublished = unpublishedList.filterNot(entry =>
73+
publishedSlugs.has(entry.get('slug'))
74+
);
75+
76+
return entries.concat(uniqueUnpublished);
77+
};
78+
5479
renderCardsForSingleCollection = () => {
55-
const { collections, entries, viewStyle } = this.props;
80+
const { collections, viewStyle } = this.props;
81+
const allEntries = this.getAllEntries();
5682
const inferredFields = this.inferFields(collections);
5783
const entryCardProps = { collection: collections, inferredFields, viewStyle };
5884

59-
return entries.map((entry, idx) => {
85+
return allEntries.map((entry, idx) => {
6086
const workflowStatus = this.props.getWorkflowStatus(collections.get('name'), entry.get('slug'));
61-
console.log('workflowStatus', workflowStatus);
87+
const isUnpublished = !!workflowStatus;
6288

6389
return (
6490
<EntryCard
6591
{...entryCardProps}
6692
entry={entry}
6793
workflowStatus={workflowStatus}
94+
isUnpublished={isUnpublished}
6895
key={idx}
6996
/>
7097
);
@@ -79,7 +106,14 @@ class EntryListing extends React.Component {
79106
const collection = collections.find(coll => coll.get('name') === collectionName);
80107
const collectionLabel = !isSingleCollectionInList && collection.get('label');
81108
const inferredFields = this.inferFields(collection);
82-
const entryCardProps = { collection, entry, inferredFields, collectionLabel };
109+
const workflowStatus = this.props.getWorkflowStatus(collectionName, entry.get('slug'));
110+
const entryCardProps = {
111+
collection,
112+
entry,
113+
inferredFields,
114+
collectionLabel,
115+
workflowStatus
116+
};
83117
return <EntryCard {...entryCardProps} key={idx} />;
84118
});
85119
};
@@ -103,10 +137,26 @@ class EntryListing extends React.Component {
103137
function mapStateToProps(state) {
104138
return {
105139
getWorkflowStatus: (collectionName, slug) => {
106-
107140
const unpublishedEntry = selectUnpublishedEntry(state, collectionName, slug);
108-
console.log('getWorkflowStatus', unpublishedEntry);
109141
return unpublishedEntry ? unpublishedEntry.get('status') : null;
142+
},
143+
getUnpublishedEntries: (collectionName) => {
144+
const allStatuses = ['draft', 'pending_review', 'pending_publish'];
145+
const unpublishedEntries = [];
146+
147+
allStatuses.forEach(statusKey => {
148+
const entriesForStatus = selectUnpublishedEntriesByStatus(state, statusKey);
149+
if (entriesForStatus) {
150+
entriesForStatus.forEach(entry => {
151+
if (entry.get('collection') === collectionName) {
152+
const entryWithCollection = entry.set('collection', collectionName);
153+
unpublishedEntries.push(entryWithCollection);
154+
}
155+
});
156+
}
157+
});
158+
159+
return unpublishedEntries;
110160
}
111161
};
112162
}

0 commit comments

Comments
 (0)