Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(netlify-cms-core) add slug customization support #1931

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ collections: # A list of collections the CMS should be able to edit
- name: 'faq' # Used in routes, ie.: /admin/collections/:slug/edit
label: 'FAQ' # Used in the UI
folder: '_faqs'
slug_field: 'slug'
create: true # Allow users to create new documents in this collection
fields: # The fields each document in this collection have
- { label: 'Question', name: 'title', widget: 'string', tagname: 'h1' }
- { label: 'Slug', name: 'slug', widget: 'string', tagname: 'h1' }
- { label: 'Answer', name: 'body', widget: 'markdown' }

- name: 'settings'
Expand Down
18 changes: 16 additions & 2 deletions packages/netlify-cms-backend-github/src/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ export default class API {
const contentKey = entry.slug;
const branchName = this.generateBranchName(contentKey);
const unpublished = options.unpublished || false;
if (!unpublished) {
const slugChanged = entry.slugChanged;
if (!unpublished || slugChanged) {
// Open new editorial review workflow for this entry - Create new metadata and commit to new branch`
let prResponse;

Expand All @@ -385,7 +386,18 @@ export default class API {
prResponse = pr;
return this.user();
})
.then(user => {
.then(async user => {
// Keep track of the entry initial parent slug
let parentSlug = entry.oldSlug || entry.slug;
let parentPath = entry.oldPath || entry.path;
if (slugChanged) {
await this.retrieveMetadata(entry.oldSlug).then(metaData => {
if (metaData) {
parentSlug = metaData.parentSlug;
parentPath = metaData.parentPath;
}
});
}
return this.storeMetadata(contentKey, {
type: 'PR',
pr: {
Expand All @@ -398,6 +410,8 @@ export default class API {
collection: options.collectionName,
title: options.parsedData && options.parsedData.title,
description: options.parsedData && options.parsedData.description,
parentSlug,
parentPath,
objects: {
entry: {
path: entry.path,
Expand Down
5 changes: 4 additions & 1 deletion packages/netlify-cms-backend-gitlab/src/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ export default class API {
persistFiles = (files, { commitMessage, newEntry }) =>
Promise.all(
files.map(file =>
this.uploadAndCommit(file, { commitMessage, updateFile: newEntry === false }),
this.uploadAndCommit(file, {
commitMessage,
updateFile: newEntry === false && file.slugChanged === false,
}),
),
);

Expand Down
18 changes: 13 additions & 5 deletions packages/netlify-cms-backend-test/src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,24 @@ export default class TestRepo {
const existingEntryIndex = unpubStore.findIndex(
e => e.metaData.collection === collection && e.slug === slug,
);
unpubStore.splice(existingEntryIndex, 1);
if (existingEntryIndex >= 0) {
unpubStore.splice(existingEntryIndex, 1);
}
return Promise.resolve();
}

persistEntry({ path, raw, slug }, mediaFiles, options = {}) {
persistEntry({ path, raw, slug, oldPath, oldSlug }, mediaFiles, options = {}) {
if (options.useWorkflow) {
const unpubStore = window.repoFilesUnpublished;
const existingEntryIndex = unpubStore.findIndex(e => e.file.path === path);
const existingPath = oldPath || path;
const existingEntryIndex = unpubStore.findIndex(e => e.file.path === existingPath);
if (existingEntryIndex >= 0) {
const unpubEntry = { ...unpubStore[existingEntryIndex], data: raw };
unpubEntry.title = options.parsedData && options.parsedData.title;
unpubEntry.description = options.parsedData && options.parsedData.description;
if (oldPath) {
Object.assign(unpubEntry, { file: { path }, slug });
}
unpubEntry.metaData.title = options.parsedData && options.parsedData.title;
unpubEntry.metaData.description = options.parsedData && options.parsedData.description;
unpubStore.splice(existingEntryIndex, 1, unpubEntry);
} else {
const unpubEntry = {
Expand All @@ -165,6 +171,8 @@ export default class TestRepo {
status: this.options.initialWorkflowStatus,
title: options.parsedData && options.parsedData.title,
description: options.parsedData && options.parsedData.description,
parentSlug: oldSlug || slug,
parentPath: oldPath || path,
},
slug,
};
Expand Down
32 changes: 27 additions & 5 deletions packages/netlify-cms-core/src/actions/editorialWorkflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { actions as notifActions } from 'redux-notifications';
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';
import { serializeValues } from 'Lib/serializeEntryValues';
import { currentBackend } from 'src/backend';
import { getAsset } from 'Reducers';
import { selectFields } from 'Reducers/collections';
import { getAsset, selectSlugs, selectUnpublishedEntry } from 'Reducers';
import { selectFields, selectSlugField } from 'Reducers/collections';
import { EDITORIAL_WORKFLOW } from 'Constants/publishModes';
import { EDITORIAL_WORKFLOW_ERROR } from 'netlify-cms-lib-util';
import { loadEntry } from './entries';
import { loadEntry, deleteEntry } from './entries';
import ValidationErrorTypes from 'Constants/validationErrorTypes';

const { notifSend } = notifActions;
Expand Down Expand Up @@ -97,12 +97,13 @@ function unpublishedEntriesFailed(error) {
};
}

function unpublishedEntryPersisting(collection, entry, transactionID) {
function unpublishedEntryPersisting(collection, entry, slug, transactionID) {
return {
type: UNPUBLISHED_ENTRY_PERSIST_REQUEST,
payload: {
collection: collection.get('name'),
entry,
slug,
},
optimist: { type: BEGIN, id: transactionID },
};
Expand Down Expand Up @@ -261,6 +262,7 @@ export function loadUnpublishedEntries(collections) {
return (dispatch, getState) => {
const state = getState();
if (state.config.get('publish_mode') !== EDITORIAL_WORKFLOW) return;
if (state.editorialWorkflow.get('isFetched')) return;
const backend = currentBackend(state.config);
dispatch(unpublishedEntriesLoading());
backend
Expand Down Expand Up @@ -288,6 +290,7 @@ export function persistUnpublishedEntry(collection, existingUnpublishedEntry) {
const state = getState();
const entryDraft = state.entryDraft;
const fieldsErrors = entryDraft.get('fieldsErrors');
const unavailableSlugs = selectSlugs(state, collection.get('name'));

// Early return if draft contains validation errors
if (!fieldsErrors.isEmpty()) {
Expand Down Expand Up @@ -319,11 +322,16 @@ export function persistUnpublishedEntry(collection, existingUnpublishedEntry) {
* update the entry and entryDraft with the serialized values.
*/
const fields = selectFields(collection, entry.get('slug'));
const slugField = selectSlugField(collection);
const serializedData = serializeValues(entryDraft.getIn(['entry', 'data']), fields);
const serializedEntry = entry.set('data', serializedData);
const serializedEntryDraft = entryDraft.set('entry', serializedEntry);
const manualSlug = serializedData.get(slugField);
const entrySlug = serializedEntry.get('slug');
const slugChanged = manualSlug && entrySlug && manualSlug !== entrySlug;
const selectedSlug = manualSlug || entrySlug;

dispatch(unpublishedEntryPersisting(collection, serializedEntry, transactionID));
dispatch(unpublishedEntryPersisting(collection, serializedEntry, selectedSlug, transactionID));
const persistAction = existingUnpublishedEntry
? backend.persistUnpublishedEntry
: backend.persistEntry;
Expand All @@ -334,6 +342,8 @@ export function persistUnpublishedEntry(collection, existingUnpublishedEntry) {
serializedEntryDraft,
assetProxies.toJS(),
state.integrations,
selectedSlug,
unavailableSlugs,
];

try {
Expand All @@ -348,6 +358,10 @@ export function persistUnpublishedEntry(collection, existingUnpublishedEntry) {
}),
);
dispatch(unpublishedEntryPersisted(collection, serializedEntry, transactionID, newSlug));
if (slugChanged && selectUnpublishedEntry(state, collection.get('name'), entrySlug)) {
// Delete previous entry
dispatch(deleteUnpublishedEntry(collection.get('name'), entrySlug));
}
} catch (error) {
dispatch(
notifSend({
Expand Down Expand Up @@ -448,6 +462,10 @@ export function publishUnpublishedEntry(collection, slug) {
const collections = state.collections;
const backend = currentBackend(state.config);
const transactionID = uuid();
const unpublishedEntry = selectUnpublishedEntry(state, collection, slug);
const parentSlug = unpublishedEntry.getIn(['metaData', 'parentSlug']);
const slugChanged = parentSlug && parentSlug !== slug;

dispatch(unpublishedEntryPublishRequest(collection, slug, transactionID));
return backend
.publishUnpublishedEntry(collection, slug)
Expand All @@ -461,6 +479,10 @@ export function publishUnpublishedEntry(collection, slug) {
);
dispatch(unpublishedEntryPublished(collection, slug, transactionID));
dispatch(loadEntry(collections.get(collection), slug));
if (slugChanged) {
// Delete parent entry
dispatch(deleteEntry(state.collections.get(collection), parentSlug));
}
})
.catch(error => {
dispatch(
Expand Down
30 changes: 24 additions & 6 deletions packages/netlify-cms-core/src/actions/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { actions as notifActions } from 'redux-notifications';
import { serializeValues } from 'Lib/serializeEntryValues';
import { currentBackend } from 'src/backend';
import { getIntegrationProvider } from 'Integrations';
import { getAsset, selectIntegration } from 'Reducers';
import { selectFields } from 'Reducers/collections';
import { getAsset, selectSlugEntries, selectIntegration } from 'Reducers';
import { selectFields, selectSlugField } from 'Reducers/collections';
import { selectCollectionEntriesCursor } from 'Reducers/cursors';
import { Cursor } from 'netlify-cms-lib-util';
import { createEntry } from 'ValueObjects/Entry';
Expand Down Expand Up @@ -121,7 +121,6 @@ export function entryPersisted(collection, entry, slug) {
payload: {
collectionName: collection.get('name'),
entrySlug: entry.get('slug'),

/**
* Pass slug from backend for newly created entries.
*/
Expand Down Expand Up @@ -202,10 +201,10 @@ export function changeDraft(entry) {
};
}

export function changeDraftField(field, value, metadata) {
export function changeDraftField(field, value, metadata, hasChanged = true) {
return {
type: DRAFT_CHANGE_FIELD,
payload: { field, value, metadata },
payload: { field, value, metadata, hasChanged },
};
}

Expand Down Expand Up @@ -410,6 +409,7 @@ export function persistEntry(collection) {
const state = getState();
const entryDraft = state.entryDraft;
const fieldsErrors = entryDraft.get('fieldsErrors');
const unavailableSlugs = selectSlugEntries(state, collection.get('name'));

// Early return if draft contains validation errors
if (!fieldsErrors.isEmpty()) {
Expand Down Expand Up @@ -441,12 +441,26 @@ export function persistEntry(collection) {
* update the entry and entryDraft with the serialized values.
*/
const fields = selectFields(collection, entry.get('slug'));
const slugField = selectSlugField(collection);
const serializedData = serializeValues(entryDraft.getIn(['entry', 'data']), fields);
const serializedEntry = entry.set('data', serializedData);
const serializedEntryDraft = entryDraft.set('entry', serializedEntry);
const manualSlug = slugField && serializedData.get(slugField);
const entrySlug = serializedEntry.get('slug');
const selectedSlug = manualSlug || entrySlug;
const slugChanged = manualSlug && entrySlug && manualSlug !== entrySlug;

dispatch(entryPersisting(collection, serializedEntry));
return backend
.persistEntry(state.config, collection, serializedEntryDraft, assetProxies.toJS())
.persistEntry(
state.config,
collection,
serializedEntryDraft,
assetProxies.toJS(),
state.integrations,
selectedSlug,
unavailableSlugs,
)
.then(slug => {
dispatch(
notifSend({
Expand All @@ -458,6 +472,10 @@ export function persistEntry(collection) {
}),
);
dispatch(entryPersisted(collection, serializedEntry, slug));
if (slugChanged) {
// Delete previous entry
dispatch(deleteEntry(collection, entrySlug));
}
})
.catch(error => {
console.error(error);
Expand Down
Loading