Skip to content

Commit 8229ed7

Browse files
committed
feat(): toast warnings for duplicate keys when loading entrties - wip suggestion
1 parent 6110971 commit 8229ed7

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,10 @@ export function loadEntries(collection: Collection, page = 0) {
595595
cursor: Cursor;
596596
pagination: number;
597597
entries: EntryValue[];
598+
errors?: string[];
598599
} = await (loadAllEntries
599600
? // nested collections require all entries to construct the tree
600-
provider.listAllEntries(collection).then((entries: EntryValue[]) => ({ entries }))
601+
provider.listAllEntries(collection)
601602
: provider.listEntries(collection, page));
602603
response = {
603604
...response,
@@ -616,6 +617,19 @@ export function loadEntries(collection: Collection, page = 0) {
616617
: Cursor.create(response.cursor),
617618
};
618619

620+
response.errors?.forEach(error => {
621+
dispatch(
622+
addNotification({
623+
message: {
624+
details: error,
625+
key: 'ui.toast.duplicateFrontmatterKey',
626+
},
627+
type: 'warning',
628+
dismissAfter: 8000,
629+
}),
630+
);
631+
});
632+
619633
dispatch(
620634
entriesLoaded(
621635
collection,

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { flatten, isError, uniq, trim, sortBy, get, set } from 'lodash';
1+
import { flatten, isError, uniq, trim, sortBy, get, set, attempt } from 'lodash';
22
import { List, fromJS, Set } from 'immutable';
33
import * as fuzzy from 'fuzzy';
44
import {
@@ -515,6 +515,7 @@ export class Backend {
515515
),
516516
);
517517
const formattedEntries = entries.map(this.entryWithFormat(collection));
518+
// todo: find error somewhere here and put it to errors array
518519
// If this collection has a "filter" property, filter entries accordingly
519520
const collectionFilter = collection.get('filter');
520521
const filteredEntries = collectionFilter
@@ -526,7 +527,7 @@ export class Backend {
526527
const groupedEntries = groupEntries(collection, extension, filteredEntries);
527528
return groupedEntries;
528529
}
529-
530+
// todo: return object { entries, errors } + change all other processEntries uses to just take entries
530531
return filteredEntries;
531532
}
532533

@@ -567,10 +568,14 @@ export class Backend {
567568
cursorType: 'collectionEntries',
568569
collection,
569570
});
571+
570572
return {
571573
entries: this.processEntries(loadedEntries, collection),
572574
pagination: cursor.meta?.get('page'),
573575
cursor,
576+
errors: [
577+
'error found in process entries'
578+
]
574579
};
575580
}
576581

@@ -594,14 +599,17 @@ export class Backend {
594599
}
595600

596601
const response = await this.listEntries(collection);
597-
const { entries } = response;
602+
const { entries, errors } = response;
598603
let { cursor } = response;
599604
while (cursor && cursor.actions!.includes('next')) {
600605
const { entries: newEntries, cursor: newCursor } = await this.traverseCursor(cursor, 'next');
601606
entries.push(...newEntries);
602607
cursor = newCursor;
603608
}
604-
return entries;
609+
return {
610+
entries,
611+
errors,
612+
};
605613
}
606614

607615
async search(collections: Collection[], searchTerm: string) {
@@ -869,8 +877,12 @@ export class Backend {
869877
return (entry: EntryValue): EntryValue => {
870878
const format = resolveFormat(collection, entry);
871879
if (entry && entry.raw !== undefined) {
872-
const data = (format && format.fromFile.bind(format, entry.raw)()) || {};
873-
if (isError(data)) console.error(data);
880+
const data = (format && attempt(format.fromFile.bind(format, entry.raw))) || {};
881+
// const data = (format && format.fromFile.bind(format, entry.raw)()) || {};
882+
if (isError(data)) {
883+
entry = Object.assign(entry, { parseError: data.message });
884+
}
885+
874886
return Object.assign(entry, { data: isError(data) ? {} : data });
875887
}
876888
return format.fromFile(entry);

packages/decap-cms-core/src/valueObjects/Entry.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface EntryValue {
3535
updatedOn: string;
3636
status?: string;
3737
meta: { path?: string };
38+
parseError?: string;
3839
i18n?: {
3940
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4041
[locale: string]: any;

packages/decap-cms-locales/src/en/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ const en = {
271271
onFailToDelete: 'Failed to delete entry: %{details}',
272272
onFailToUpdateStatus: 'Failed to update status: %{details}',
273273
missingRequiredField: "Oops, you've missed a required field. Please complete before saving.",
274+
duplicateFrontmatterKey: 'Duplicate key found in frontmatter %{details}',
274275
entrySaved: 'Entry saved',
275276
entryPublished: 'Entry published',
276277
entryUnpublished: 'Entry unpublished',

0 commit comments

Comments
 (0)