Check for title/slug field on config load. (#1203)

This commit is contained in:
Caleb
2018-05-25 10:15:01 -06:00
committed by Shawn Erquhart
parent 9fd0ff4a6a
commit 0022df57d2
5 changed files with 70 additions and 48 deletions

View File

@ -8,7 +8,8 @@ import {
selectEntryPath,
selectAllowNewEntries,
selectAllowDeletion,
selectFolderEntryExtension
selectFolderEntryExtension,
selectIdentifier,
} from "Reducers/collections";
import { createEntry } from "ValueObjects/Entry";
import { sanitizeSlug } from "Lib/urlHelper";
@ -42,23 +43,14 @@ class LocalStorageAuthStore {
}
}
const slugFormatter = (template = "{{slug}}", entryData, slugConfig) => {
const slugFormatter = (collection, entryData, slugConfig) => {
const template = collection.get('slug') || "{{slug}}";
const date = new Date();
const getIdentifier = (entryData) => {
const validIdentifierFields = ["title", "path"];
const identifiers = validIdentifierFields.map((field) =>
entryData.find((_, key) => key.toLowerCase().trim() === field)
);
const identifier = identifiers.find(ident => ident !== undefined);
if (identifier === undefined) {
throw new Error("Collection must have a field name that is a valid entry identifier");
}
return identifier;
};
const identifier = entryData.get(selectIdentifier(collection));
if (!identifier) {
throw new Error("Collection must have a field name that is a valid entry identifier");
}
const slug = template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => {
switch (field) {
@ -75,7 +67,7 @@ const slugFormatter = (template = "{{slug}}", entryData, slugConfig) => {
case "second":
return (`0${ date.getSeconds() }`).slice(-2);
case "slug":
return getIdentifier(entryData).trim();
return identifier.trim();
default:
return entryData.get(field, "").trim();
}
@ -275,7 +267,7 @@ class Backend {
if (!selectAllowNewEntries(collection)) {
throw (new Error("Not allowed to create new entries in this collection"));
}
const slug = slugFormatter(collection.get("slug"), entryDraft.getIn(["entry", "data"]), config.get("slug"));
const slug = slugFormatter(collection, entryDraft.getIn(["entry", "data"]), config.get("slug"));
const path = selectEntryPath(collection, slug);
entryObj = {
path,