Skip to content

Commit

Permalink
feat(): toast warnings for duplicate keys when loading entrties - wip…
Browse files Browse the repository at this point in the history
… suggestion
  • Loading branch information
demshy committed Sep 4, 2024
1 parent 6110971 commit 8229ed7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
16 changes: 15 additions & 1 deletion packages/decap-cms-core/src/actions/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,10 @@ export function loadEntries(collection: Collection, page = 0) {
cursor: Cursor;
pagination: number;
entries: EntryValue[];
errors?: string[];
} = await (loadAllEntries
? // nested collections require all entries to construct the tree
provider.listAllEntries(collection).then((entries: EntryValue[]) => ({ entries }))
provider.listAllEntries(collection)
: provider.listEntries(collection, page));
response = {
...response,
Expand All @@ -616,6 +617,19 @@ export function loadEntries(collection: Collection, page = 0) {
: Cursor.create(response.cursor),
};

response.errors?.forEach(error => {
dispatch(
addNotification({
message: {
details: error,
key: 'ui.toast.duplicateFrontmatterKey',
},
type: 'warning',
dismissAfter: 8000,
}),
);
});

dispatch(
entriesLoaded(
collection,
Expand Down
24 changes: 18 additions & 6 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatten, isError, uniq, trim, sortBy, get, set } from 'lodash';
import { flatten, isError, uniq, trim, sortBy, get, set, attempt } from 'lodash';
import { List, fromJS, Set } from 'immutable';
import * as fuzzy from 'fuzzy';
import {
Expand Down Expand Up @@ -515,6 +515,7 @@ export class Backend {
),
);
const formattedEntries = entries.map(this.entryWithFormat(collection));
// todo: find error somewhere here and put it to errors array
// If this collection has a "filter" property, filter entries accordingly
const collectionFilter = collection.get('filter');
const filteredEntries = collectionFilter
Expand All @@ -526,7 +527,7 @@ export class Backend {
const groupedEntries = groupEntries(collection, extension, filteredEntries);
return groupedEntries;
}

// todo: return object { entries, errors } + change all other processEntries uses to just take entries
return filteredEntries;
}

Expand Down Expand Up @@ -567,10 +568,14 @@ export class Backend {
cursorType: 'collectionEntries',
collection,
});

return {
entries: this.processEntries(loadedEntries, collection),
pagination: cursor.meta?.get('page'),
cursor,
errors: [
'error found in process entries'
]
};
}

Expand All @@ -594,14 +599,17 @@ export class Backend {
}

const response = await this.listEntries(collection);
const { entries } = response;
const { entries, errors } = response;
let { cursor } = response;
while (cursor && cursor.actions!.includes('next')) {
const { entries: newEntries, cursor: newCursor } = await this.traverseCursor(cursor, 'next');
entries.push(...newEntries);
cursor = newCursor;
}
return entries;
return {
entries,
errors,
};
}

async search(collections: Collection[], searchTerm: string) {
Expand Down Expand Up @@ -869,8 +877,12 @@ export class Backend {
return (entry: EntryValue): EntryValue => {
const format = resolveFormat(collection, entry);
if (entry && entry.raw !== undefined) {
const data = (format && format.fromFile.bind(format, entry.raw)()) || {};
if (isError(data)) console.error(data);
const data = (format && attempt(format.fromFile.bind(format, entry.raw))) || {};
// const data = (format && format.fromFile.bind(format, entry.raw)()) || {};
if (isError(data)) {
entry = Object.assign(entry, { parseError: data.message });
}

return Object.assign(entry, { data: isError(data) ? {} : data });
}
return format.fromFile(entry);
Expand Down
1 change: 1 addition & 0 deletions packages/decap-cms-core/src/valueObjects/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface EntryValue {
updatedOn: string;
status?: string;
meta: { path?: string };
parseError?: string;
i18n?: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[locale: string]: any;
Expand Down
1 change: 1 addition & 0 deletions packages/decap-cms-locales/src/en/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ const en = {
onFailToDelete: 'Failed to delete entry: %{details}',
onFailToUpdateStatus: 'Failed to update status: %{details}',
missingRequiredField: "Oops, you've missed a required field. Please complete before saving.",
duplicateFrontmatterKey: 'Duplicate key found in frontmatter %{details}',
entrySaved: 'Entry saved',
entryPublished: 'Entry published',
entryUnpublished: 'Entry unpublished',
Expand Down

0 comments on commit 8229ed7

Please sign in to comment.