feat: add preUnpublish, postUnpublish events (#3196)

This commit is contained in:
Erez Rokah 2020-02-04 13:49:30 +02:00 committed by GitHub
parent 7d792f3005
commit 18e284ece8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 12 deletions

View File

@ -558,7 +558,7 @@ export function unpublishPublishedEntry(collection: Collection, slug: string) {
const entryDraft = (Map().set('entry', entry) as unknown) as EntryDraft; const entryDraft = (Map().set('entry', entry) as unknown) as EntryDraft;
dispatch(unpublishedEntryPersisting(collection, entry, transactionID)); dispatch(unpublishedEntryPersisting(collection, entry, transactionID));
return backend return backend
.deleteEntry(state.config, collection, slug) .deleteEntry(state, collection, slug)
.then(() => .then(() =>
backend.persistEntry({ backend.persistEntry({
config: state.config, config: state.config,

View File

@ -654,7 +654,7 @@ export function deleteEntry(collection: Collection, slug: string) {
dispatch(entryDeleting(collection, slug)); dispatch(entryDeleting(collection, slug));
return backend return backend
.deleteEntry(state.config, collection, slug) .deleteEntry(state, collection, slug)
.then(() => { .then(() => {
return dispatch(entryDeleted(collection, slug)); return dispatch(entryDeleted(collection, slug));
}) })

View File

@ -3,7 +3,7 @@ import { List, Map, fromJS } from 'immutable';
import * as fuzzy from 'fuzzy'; import * as fuzzy from 'fuzzy';
import { resolveFormat } from './formats/formats'; import { resolveFormat } from './formats/formats';
import { selectUseWorkflow } from './reducers/config'; import { selectUseWorkflow } from './reducers/config';
import { selectMediaFilePath, selectMediaFolder } from './reducers/entries'; import { selectMediaFilePath, selectMediaFolder, selectEntry } from './reducers/entries';
import { selectIntegration } from './reducers/integrations'; import { selectIntegration } from './reducers/integrations';
import { import {
selectEntrySlug, selectEntrySlug,
@ -751,14 +751,25 @@ export class Backend {
return entryObj.slug; return entryObj.slug;
} }
async invokePrePublishEvent(entry: EntryMap) { async invokeEventWithEntry(event: string, entry: EntryMap) {
const { login, name } = (await this.currentUser()) as User; const { login, name } = (await this.currentUser()) as User;
await invokeEvent({ name: 'prePublish', data: { entry, author: { login, name } } }); await invokeEvent({ name: event, data: { entry, author: { login, name } } });
}
async invokePrePublishEvent(entry: EntryMap) {
await this.invokeEventWithEntry('prePublish', entry);
} }
async invokePostPublishEvent(entry: EntryMap) { async invokePostPublishEvent(entry: EntryMap) {
const { login, name } = (await this.currentUser()) as User; await this.invokeEventWithEntry('postPublish', entry);
await invokeEvent({ name: 'postPublish', data: { entry, author: { login, name } } }); }
async invokePreUnpublishEvent(entry: EntryMap) {
await this.invokeEventWithEntry('preUnpublish', entry);
}
async invokePostUnpublishEvent(entry: EntryMap) {
await this.invokeEventWithEntry('postUnpublish', entry);
} }
async persistMedia(config: Config, file: AssetProxy) { async persistMedia(config: Config, file: AssetProxy) {
@ -778,13 +789,14 @@ export class Backend {
return this.implementation.persistMedia(file, options); return this.implementation.persistMedia(file, options);
} }
async deleteEntry(config: Config, collection: Collection, slug: string) { async deleteEntry(state: State, collection: Collection, slug: string) {
const path = selectEntryPath(collection, slug) as string; const path = selectEntryPath(collection, slug) as string;
if (!selectAllowDeletion(collection)) { if (!selectAllowDeletion(collection)) {
throw new Error('Not allowed to delete entries in this collection'); throw new Error('Not allowed to delete entries in this collection');
} }
const config = state.config;
const user = (await this.currentUser()) as User; const user = (await this.currentUser()) as User;
const commitMessage = commitMessageFormatter( const commitMessage = commitMessageFormatter(
'delete', 'delete',
@ -798,7 +810,12 @@ export class Backend {
}, },
user.useOpenAuthoring, user.useOpenAuthoring,
); );
return this.implementation.deleteFile(path, commitMessage);
const entry = selectEntry(state.entries, collection.get('name'), slug);
await this.invokePreUnpublishEvent(entry);
const result = await this.implementation.deleteFile(path, commitMessage);
await this.invokePostUnpublishEvent(entry);
return result;
} }
async deleteMedia(config: Config, path: string) { async deleteMedia(config: Config, path: string) {

View File

@ -45,7 +45,7 @@ describe('registry', () => {
}); });
describe('eventHandlers', () => { describe('eventHandlers', () => {
const events = ['prePublish', 'postPublish']; const events = ['prePublish', 'postPublish', 'preUnpublish', 'postUnpublish'];
describe('registerEventListener', () => { describe('registerEventListener', () => {
it('should throw error on invalid event', () => { it('should throw error on invalid event', () => {

View File

@ -3,7 +3,7 @@ import produce from 'immer';
import { oneLine } from 'common-tags'; import { oneLine } from 'common-tags';
import EditorComponent from 'ValueObjects/EditorComponent'; import EditorComponent from 'ValueObjects/EditorComponent';
const allowedEvents = ['prePublish', 'postPublish']; const allowedEvents = ['prePublish', 'postPublish', 'preUnpublish', 'postUnpublish'];
const eventHandlers = {}; const eventHandlers = {};
allowedEvents.forEach(e => { allowedEvents.forEach(e => {
eventHandlers[e] = []; eventHandlers[e] = [];

View File

@ -394,6 +394,16 @@ CMS.registerEventListener({
name: 'postPublish', name: 'postPublish',
handler: ({ author, entry }) => console.log(JSON.stringify({ author, data: entry.get('data') })), handler: ({ author, entry }) => console.log(JSON.stringify({ author, data: entry.get('data') })),
}); });
CMS.registerEventListener({
name: 'preUnpublish',
handler: ({ author, entry }) => console.log(JSON.stringify({ author, data: entry.get('data') })),
});
CMS.registerEventListener({
name: 'postUnpublish',
handler: ({ author, entry }) => console.log(JSON.stringify({ author, data: entry.get('data') })),
});
``` ```
> Supported events are `prePublish` and `postPublish` > Supported events are `prePublish`, `postPublish`, `preUnpublish` and `postUnpublish`