|
1 | 1 | import { remove, attempt, isError } from 'lodash';
|
2 | 2 | import uuid from 'uuid/v4';
|
| 3 | +import { EDITORIAL_WORKFLOW, status } from 'Constants/publishModes'; |
| 4 | +import { EditorialWorkflowError } from 'ValueObjects/errors'; |
3 | 5 | import AuthenticationPage from './AuthenticationPage';
|
4 | 6 |
|
5 | 7 | window.repoFiles = window.repoFiles || {};
|
| 8 | +window.repoFilesUnpublished = window.repoFilesUnpublished || []; |
6 | 9 |
|
7 | 10 | function getFile(path) {
|
8 | 11 | const segments = path.split('/');
|
@@ -78,20 +81,89 @@ export default class TestRepo {
|
78 | 81 | });
|
79 | 82 | }
|
80 | 83 |
|
81 |
| - persistEntry(entry, mediaFiles = [], options) { |
| 84 | + unpublishedEntries() { |
| 85 | + return Promise.resolve(window.repoFilesUnpublished); |
| 86 | + } |
| 87 | + |
| 88 | + unpublishedEntry(collection, slug) { |
| 89 | + const entry = window.repoFilesUnpublished.find(e => ( |
| 90 | + e.metaData.collection === collection.get('name') && e.slug === slug |
| 91 | + )); |
| 92 | + if (!entry) { |
| 93 | + return Promise.reject(new EditorialWorkflowError('content is not under editorial workflow', true)); |
| 94 | + } |
| 95 | + return Promise.resolve(entry); |
| 96 | + } |
| 97 | + |
| 98 | + deleteUnpublishedEntry(collection, slug) { |
| 99 | + const unpubStore = window.repoFilesUnpublished; |
| 100 | + const existingEntryIndex = unpubStore.findIndex(e => ( |
| 101 | + e.metaData.collection === collection && e.slug === slug |
| 102 | + )); |
| 103 | + unpubStore.splice(existingEntryIndex, 1); |
| 104 | + return Promise.resolve() |
| 105 | + } |
| 106 | + |
| 107 | + persistEntry({ path, raw, slug }, mediaFiles = [], options = {}) { |
| 108 | + if (options.mode === EDITORIAL_WORKFLOW) { |
| 109 | + const unpubStore = window.repoFilesUnpublished; |
| 110 | + const existingEntryIndex = unpubStore.findIndex(e => e.file.path === path); |
| 111 | + if (existingEntryIndex >= 0) { |
| 112 | + const unpubEntry = { ...unpubStore[existingEntryIndex], data: raw }; |
| 113 | + unpubEntry.title = options.parsedData && options.parsedData.title; |
| 114 | + unpubEntry.description = options.parsedData && options.parsedData.description; |
| 115 | + unpubStore.splice(existingEntryIndex, 1, unpubEntry); |
| 116 | + } else { |
| 117 | + const unpubEntry = { |
| 118 | + data: raw, |
| 119 | + file: { |
| 120 | + path, |
| 121 | + }, |
| 122 | + metaData: { |
| 123 | + collection: options.collectionName, |
| 124 | + status: status.first(), |
| 125 | + title: options.parsedData && options.parsedData.title, |
| 126 | + description: options.parsedData && options.parsedData.description, |
| 127 | + }, |
| 128 | + slug, |
| 129 | + }; |
| 130 | + unpubStore.push(unpubEntry); |
| 131 | + } |
| 132 | + return Promise.resolve(); |
| 133 | + } |
| 134 | + |
82 | 135 | const newEntry = options.newEntry || false;
|
83 |
| - const folder = entry.path.substring(0, entry.path.lastIndexOf('/')); |
84 |
| - const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1); |
| 136 | + const folder = path.substring(0, path.lastIndexOf('/')); |
| 137 | + const fileName = path.substring(path.lastIndexOf('/') + 1); |
85 | 138 | window.repoFiles[folder] = window.repoFiles[folder] || {};
|
86 | 139 | window.repoFiles[folder][fileName] = window.repoFiles[folder][fileName] || {};
|
87 | 140 | if (newEntry) {
|
88 |
| - window.repoFiles[folder][fileName] = { content: entry.raw }; |
| 141 | + window.repoFiles[folder][fileName] = { content: raw }; |
89 | 142 | } else {
|
90 |
| - window.repoFiles[folder][fileName].content = entry.raw; |
| 143 | + window.repoFiles[folder][fileName].content = raw; |
91 | 144 | }
|
92 | 145 | return Promise.resolve();
|
93 | 146 | }
|
94 | 147 |
|
| 148 | + updateUnpublishedEntryStatus(collection, slug, newStatus) { |
| 149 | + const unpubStore = window.repoFilesUnpublished; |
| 150 | + const entryIndex = unpubStore.findIndex(e => ( |
| 151 | + e.metaData.collection === collection && e.slug === slug |
| 152 | + )); |
| 153 | + unpubStore[entryIndex].metaData.status = newStatus; |
| 154 | + return Promise.resolve(); |
| 155 | + } |
| 156 | + |
| 157 | + publishUnpublishedEntry(collection, slug) { |
| 158 | + const unpubStore = window.repoFilesUnpublished; |
| 159 | + const unpubEntryIndex = unpubStore.findIndex(e => ( |
| 160 | + e.metaData.collection === collection && e.slug === slug |
| 161 | + )); |
| 162 | + const unpubEntry = unpubStore[unpubEntryIndex]; |
| 163 | + const entry = { raw: unpubEntry.data, slug: unpubEntry.slug, path: unpubEntry.file.path }; |
| 164 | + unpubStore.splice(unpubEntryIndex, 1); |
| 165 | + return this.persistEntry(entry); |
| 166 | + } |
95 | 167 | getMedia() {
|
96 | 168 | return Promise.resolve(this.assets);
|
97 | 169 | }
|
|
0 commit comments