Skip to content

Commit f76759b

Browse files
committed
Merge branch 'feat/db-cache-poc' into moc-db-cache-poc
2 parents d60aabe + 378f96f commit f76759b

File tree

7 files changed

+287
-6
lines changed

7 files changed

+287
-6
lines changed

packages/decap-cms-core/src/actions/entries.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export const CHANGE_VIEW_STYLE = 'CHANGE_VIEW_STYLE';
109109
export const SET_ENTRIES_PAGE_SIZE = 'SET_ENTRIES_PAGE_SIZE';
110110
export const LOAD_ENTRIES_PAGE = 'LOAD_ENTRIES_PAGE';
111111
export const SET_ENTRIES_PAGE = 'SET_ENTRIES_PAGE';
112+
export const SYNC_ENTRIES = 'SYNC_ENTRIES';
112113

113114
/*
114115
* Simple Action Creators (Internal)
@@ -864,7 +865,7 @@ function addAppendActionsToCursor(cursor: Cursor) {
864865
return updatedCursor;
865866
}
866867

867-
export function loadEntries(collection: Collection, page = 0) {
868+
export function loadEntries(collection: Collection, page = 0, sync = false) {
868869
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
869870
if (collection.get('isFetching')) {
870871
return;
@@ -1511,3 +1512,10 @@ export function validateMetaField(
15111512
}
15121513
return { error: false };
15131514
}
1515+
1516+
export function syncEntries(collection: Collection) {
1517+
return (dispatch: ThunkDispatch<State, {}, AnyAction>) => {
1518+
dispatch({ type: SYNC_ENTRIES, payload: { collection: collection.get('name') } });
1519+
return dispatch(loadEntries(collection, 0, true)); // Pass sync=true
1520+
};
1521+
}

packages/decap-cms-core/src/backend.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ import {
5959
getI18nInfo,
6060
I18N_STRUCTURE,
6161
} from './lib/i18n';
62+
import {
63+
getDataFilesByCollection,
64+
saveDataFilesByCollection,
65+
saveDataFileToDbCache,
66+
} from './dbcache';
6267

6368
import type { I18nInfo } from './lib/i18n';
6469
import type AssetProxy from './valueObjects/AssetProxy';
@@ -676,7 +681,14 @@ export class Backend {
676681
totalCount: number;
677682
entries: EntryValue[];
678683
}) => void,
684+
sync = false,
679685
) {
686+
const cacheFiles = await getDataFilesByCollection(collection.get('name'));
687+
688+
if (cacheFiles.length > 0 && !sync) {
689+
return cacheFiles;
690+
}
691+
680692
if (collection.get('folder') && this.implementation.allEntriesByFolder) {
681693
const depth = collectionDepth(collection);
682694
const extension = selectFolderEntryExtension(collection);
@@ -708,11 +720,23 @@ export class Backend {
708720
collectionRegex(collection),
709721
wrappedOnProgress,
710722
)
711-
.then(entries => {
723+
.then(async entries => {
712724
console.log(
713725
`[backend.listAllEntries] allEntriesByFolder returned ${entries.length} entries`,
714726
);
715-
return this.processEntries(entries, collection);
727+
728+
const all = this.processEntries(entries, collection);
729+
730+
await saveDataFilesByCollection(
731+
collection.get('name'),
732+
all.map(entry => ({
733+
path: entry.path,
734+
raw: entry.raw,
735+
json: entry,
736+
})),
737+
);
738+
739+
return all;
716740
});
717741
}
718742

@@ -724,6 +748,9 @@ export class Backend {
724748
entries.push(...newEntries);
725749
cursor = newCursor;
726750
}
751+
752+
console.log('list all entries', entries);
753+
727754
return entries;
728755
}
729756

@@ -1276,6 +1303,7 @@ export class Backend {
12761303
raw: this.entryToRaw(collection, entryDraft.get('entry')),
12771304
newPath: customPath === path ? undefined : customPath,
12781305
isFolder,
1306+
json: entryDraft.get('entry').toJS(),
12791307
};
12801308
}
12811309

@@ -1332,6 +1360,7 @@ export class Backend {
13321360
},
13331361
opts,
13341362
);
1363+
await saveDataFileToDbCache(collectionName, dataFile);
13351364

13361365
await this.invokePostSaveEvent(entryDraft.get('entry'));
13371366

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import CollectionTop from './CollectionTop';
1212
import EntriesCollection from './Entries/EntriesCollection';
1313
import EntriesSearch from './Entries/EntriesSearch';
1414
import CollectionControls from './CollectionControls';
15-
import { sortByField, filterByField, changeViewStyle, groupByField } from '../../actions/entries';
15+
import {
16+
sortByField,
17+
filterByField,
18+
changeViewStyle,
19+
groupByField,
20+
syncEntries,
21+
} from '../../actions/entries';
1622
import {
1723
selectSortableFields,
1824
selectViewFilters,
@@ -100,6 +106,7 @@ export class Collection extends React.Component {
100106
onChangeViewStyle,
101107
viewStyle,
102108
isFetchingEntries,
109+
onSyncClick,
103110
} = this.props;
104111

105112
let newEntryUrl = collection.get('create') ? getNewEntryUrl(collectionName) : '';
@@ -131,7 +138,11 @@ export class Collection extends React.Component {
131138
</SearchResultContainer>
132139
) : (
133140
<>
134-
<CollectionTop collection={collection} newEntryUrl={newEntryUrl} />
141+
<CollectionTop
142+
collection={collection}
143+
newEntryUrl={newEntryUrl}
144+
onSyncClick={onSyncClick}
145+
/>
135146
<CollectionControls
136147
viewStyle={viewStyle}
137148
onChangeViewStyle={onChangeViewStyle}
@@ -195,6 +206,7 @@ const mapDispatchToProps = {
195206
filterByField,
196207
changeViewStyle,
197208
groupByField,
209+
syncEntries,
198210
};
199211

200212
function mergeProps(stateProps, dispatchProps, ownProps) {
@@ -206,6 +218,7 @@ function mergeProps(stateProps, dispatchProps, ownProps) {
206218
onFilterClick: filter => dispatchProps.filterByField(stateProps.collection, filter),
207219
onGroupClick: group => dispatchProps.groupByField(stateProps.collection, group),
208220
onChangeViewStyle: viewStyle => dispatchProps.changeViewStyle(viewStyle),
221+
onSyncClick: () => dispatchProps.syncEntries(stateProps.collection),
209222
};
210223
}
211224

packages/decap-cms-core/src/components/Collection/CollectionTop.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function getCollectionProps(collection) {
6666

6767
const history = createHashHistory();
6868

69-
function CollectionTop({ collection, newEntryUrl, t }) {
69+
function CollectionTop({ collection, newEntryUrl, onSyncClick, t }) {
7070
const { collectionLabel, collectionLabelSingular, collectionDescription } = getCollectionProps(
7171
collection,
7272
t,
@@ -82,10 +82,17 @@ function CollectionTop({ collection, newEntryUrl, t }) {
8282

8383
// TODO: find some nice names for the path types, translate and consolidate
8484

85+
function handleSync() {
86+
if (onSyncClick) {
87+
onSyncClick();
88+
}
89+
}
90+
8591
return (
8692
<CollectionTopContainer>
8793
<CollectionTopRow>
8894
<CollectionTopHeading>{collectionLabel}</CollectionTopHeading>
95+
<button onClick={handleSync}>Sync</button>
8996
{indexFileConfig && collection.get('nested') ? (
9097
<Dropdown
9198
renderButton={() => (
@@ -128,6 +135,7 @@ function CollectionTop({ collection, newEntryUrl, t }) {
128135
CollectionTop.propTypes = {
129136
collection: ImmutablePropTypes.map.isRequired,
130137
newEntryUrl: PropTypes.string,
138+
onSyncClick: PropTypes.func,
131139
t: PropTypes.func.isRequired,
132140
};
133141

0 commit comments

Comments
 (0)