Skip to content

Commit

Permalink
fix: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
barthc committed Jan 28, 2020
1 parent 06b5f7e commit 8049a38
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 29 deletions.
4 changes: 2 additions & 2 deletions packages/netlify-cms-backend-bitbucket/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export default class BitbucketBackend implements Implementation {
async unpublishedEntry(
collection: string,
slug: string,
combineKey: string,
combineKey: string | undefined,
{
loadEntryMediaFiles = (branch: string, files: UnpublishedEntryMediaFile[]) =>
this.loadEntryMediaFiles(branch, files),
Expand All @@ -461,7 +461,7 @@ export default class BitbucketBackend implements Implementation {
return await this.api!.readUnpublishedBranchFile(contentKey, loadEntryMediaFiles);
}

async unpublishedCombineEntry(combineKey, path) {
async unpublishedCombineEntry(combineKey: string, path: string) {
return await this.unpublishedEntry('', '', combineKey).then(entries =>
entries.find(entry => entry.file.path === path),
);
Expand Down
10 changes: 8 additions & 2 deletions packages/netlify-cms-backend-git-gateway/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ export default class GitGateway implements Implementation {
deleteFile(path: string, commitMessage: string) {
return this.backend!.deleteFile(path, commitMessage);
}
async getDeployPreview(collection: string, slug: string) {
return this.backend!.getDeployPreview(collection, slug);
async getDeployPreview(collection: string, slug: string, combineKey: string | undefined) {
return this.backend!.getDeployPreview(collection, slug, combineKey);
}
unpublishedEntries() {
return this.backend!.unpublishedEntries();
Expand All @@ -501,6 +501,12 @@ export default class GitGateway implements Implementation {
loadEntryMediaFiles: (branch, files) => this.loadEntryMediaFiles(branch, files),
});
}
unpublishedCombineEntry(combineKey: string, path: string) {
return this.backend!.unpublishedCombineEntry(combineKey, path);
}
combineColletionEntry(combineArgs, entries) {
return this.backend!.combineColletionEntry(combineArgs, entries);
}
updateUnpublishedEntryStatus(collection: string, slug: string, newStatus: string) {
return this.backend!.updateUnpublishedEntryStatus(collection, slug, newStatus);
}
Expand Down
13 changes: 11 additions & 2 deletions packages/netlify-cms-backend-github/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ export default class API {
.catch(replace404WithEmptyArray);
}

async readUnpublishedBranchFile(contentKey: string) {
async readUnpublishedBranchFile(contentKey: string, loadEntryMediaFiles) {
try {
const metaData = await this.retrieveMetadata(contentKey).then(data =>
data.objects.entry.path ? data : Promise.reject(null),
Expand All @@ -507,12 +507,21 @@ export default class API {
}) as Promise<string>,
this.isUnpublishedEntryModification(metaData.objects.entry.path),
]);
const files = metaData.objects.files || [];
const loadedMediaFiles =
loadEntryMediaFiles &&
(await loadEntryMediaFiles(
metaData.branch,
files.map(({ sha: id, path }) => ({ id, path })),
));

return {
metaData,
fileData,
file: { path: metaData.objects.entry.path, id: null },
data: fileData,
isModification,
slug: this.slugFromContentKey(contentKey, metaData.collection),
...(loadedMediaFiles && { mediaFiles: loadedMediaFiles }),
};
} catch (e) {
throw new EditorialWorkflowError('content is not under editorial workflow', true);
Expand Down
15 changes: 1 addition & 14 deletions packages/netlify-cms-backend-github/src/implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,7 @@ export default class GitHub implements Implementation {
} = {},
) {
const contentKey = this.api!.generateContentKey(collection, slug);
const data = await this.api!.readUnpublishedBranchFile(contentKey);
const files = data.metaData.objects.files || [];
const mediaFiles = await loadEntryMediaFiles(
data.metaData.branch,
files.map(({ sha: id, path }) => ({ id, path })),
);
return {
slug,
file: { path: data.metaData.objects.entry.path, id: null },
data: data.fileData as string,
metaData: data.metaData,
mediaFiles,
isModification: data.isModification,
};
return await this.api!.readUnpublishedBranchFile(contentKey, loadEntryMediaFiles);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/netlify-cms-backend-gitlab/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export default class GitLab implements Implementation {
async unpublishedEntry(
collection: string,
slug: string,
combineKey: string,
combineKey: string | undefined,
{
loadEntryMediaFiles = (branch: string, files: UnpublishedEntryMediaFile[]) =>
this.loadEntryMediaFiles(branch, files),
Expand All @@ -309,7 +309,7 @@ export default class GitLab implements Implementation {
return await this.api!.readUnpublishedBranchFile(contentKey, loadEntryMediaFiles);
}

async unpublishedCombineEntry(combineKey, path) {
async unpublishedCombineEntry(combineKey: string, path: string) {
return await this.unpublishedEntry('', '', combineKey).then(entries =>
entries.find(entry => entry.file.path === path),
);
Expand Down
30 changes: 27 additions & 3 deletions packages/netlify-cms-backend-test/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ export default class TestBackend implements Implementation {
return Promise.resolve(entry);
}

unpublishedCombineEntry(combineKey: string, path: string) {
const entry = window.repoFilesUnpublished.find(
e => e.combineKey === combineKey && e.file.path === path,
);

entry.mediaFiles = this.getMediaFiles(entry);

return Promise.resolve(entry);
}

deleteUnpublishedEntry(collection: string, slug: string) {
const unpubStore = window.repoFilesUnpublished;
let entryIndexes = [
Expand All @@ -227,11 +237,8 @@ export default class TestBackend implements Implementation {

const existingEntryIndex = unpubStore.findIndex(e => e.file.path === path);
if (existingEntryIndex >= 0) {
const collectionName = options.collectionName;
const combineKey = `${options.collectionName}/${slug}`;
const unpubEntry = {
...unpubStore[existingEntryIndex],
...(isCombineKey(collectionName, slug) && { combineKey }),
data: raw,
title: options.parsedData && options.parsedData.title,
description: options.parsedData && options.parsedData.description,
Expand Down Expand Up @@ -277,6 +284,23 @@ export default class TestBackend implements Implementation {
return Promise.resolve();
}

combineColletionEntry(combineArgs, entries) {
const unpubStore = window.repoFilesUnpublished;
entries.map(entry => {
const existingEntryIndex = unpubStore.findIndex(
e => e.slug === entry.slug && e.metaData.collection == entry.collection,
);
const unpubEntry = {
...unpubStore[existingEntryIndex],
combineKey: `${combineArgs.collection}/${combineArgs.slug}`,
};

unpubStore.splice(existingEntryIndex, 1, unpubEntry);
});

return Promise.resolve();
}

updateUnpublishedEntryStatus(collection: string, slug: string, newStatus: string) {
const unpubStore = window.repoFilesUnpublished;
let entryIndexes = [
Expand Down
1 change: 1 addition & 0 deletions packages/netlify-cms-core/src/actions/editorialWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ export function combineColletionEntry(parentArgs, childArgs) {
export function deleteUnpublishedEntry(collection: string, slug: string) {
return (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
const state = getState();
console.log(collection);
const backend = currentBackend(state.config);
const transactionID = uuid();
dispatch(unpublishedEntryDeleteRequest(collection, slug, transactionID));
Expand Down
9 changes: 7 additions & 2 deletions packages/netlify-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,12 @@ export class Backend {
.then(this.entryWithFormat(collection));
}

unpublishedCombineEntry(collection, combineKey, path, collections) {
unpublishedCombineEntry(
collection: Collection,
combineKey: string,
path: string,
collections: Collections,
) {
const collectionsNameFolder = selectCollectionsNameFolder(collections);
return this.implementation!.unpublishedCombineEntry!(combineKey, path)
.then(loadedEntry => this.inferCollection(loadedEntry, collectionsNameFolder))
Expand Down Expand Up @@ -860,7 +865,7 @@ export class Backend {
}

combineColletionEntry(combineArgs, entries) {
return this.implementation.combineColletionEntry(combineArgs, entries);
return this.implementation.combineColletionEntry!(combineArgs, entries);
}

publishUnpublishedEntry(collection: string, slug: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/netlify-cms-core/src/reducers/editorialWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ const unpublishedEntries = (state = Map(), action: EditorialWorkflowAction) => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const entryKeys = selectUnpublishedKeysByCombineKey(
map,
action.payload!.collection,
action.payload!.slug,
action.payload!.combineCollection,
action.payload!.combineSlug,
);
entryKeys.forEach(key => {
map.deleteIn(['entities', key, 'isPersisting']);
Expand Down

0 comments on commit 8049a38

Please sign in to comment.