Skip to content

Commit

Permalink
fix: incorrect version defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Apr 17, 2023
1 parent 086feaf commit 3f9bbe9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
23 changes: 15 additions & 8 deletions src/admin/components/utilities/DocumentInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const DocumentInfoProvider: React.FC<Props> = ({
const getVersions = useCallback(async () => {
let versionFetchURL;
let publishedFetchURL;
let draftsEnabled = false;
let shouldFetchVersions = false;
let unpublishedVersionJSON = null;
let versionJSON = null;
Expand Down Expand Up @@ -93,12 +94,14 @@ export const DocumentInfoProvider: React.FC<Props> = ({
};

if (global) {
draftsEnabled = Boolean(global?.versions?.drafts);
shouldFetchVersions = Boolean(global?.versions);
versionFetchURL = `${baseURL}/globals/${global.slug}/versions`;
publishedFetchURL = `${baseURL}/globals/${global.slug}?${qs.stringify(publishedVersionParams)}`;
}

if (collection) {
draftsEnabled = Boolean(collection?.versions?.drafts);
shouldFetchVersions = Boolean(collection?.versions);
versionFetchURL = `${baseURL}/${collection.slug}/versions`;

Expand All @@ -122,15 +125,19 @@ export const DocumentInfoProvider: React.FC<Props> = ({
}

if (shouldFetch) {
let publishedJSON = await fetch(publishedFetchURL, {
credentials: 'include',
headers: {
'Accept-Language': i18n.language,
},
}).then((res) => res.json());
let publishedJSON;

if (draftsEnabled) {
publishedJSON = await fetch(publishedFetchURL, {
credentials: 'include',
headers: {
'Accept-Language': i18n.language,
},
}).then((res) => res.json());

if (collection) {
publishedJSON = publishedJSON?.docs?.[0];
if (collection) {
publishedJSON = publishedJSON?.docs?.[0];
}
}

if (shouldFetchVersions) {
Expand Down
9 changes: 5 additions & 4 deletions src/collections/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import getBaseUploadFields from '../../uploads/getBaseFields';
import { formatLabels } from '../../utilities/formatLabels';
import { defaults, authDefaults } from './defaults';
import { Config } from '../../config/types';
import { versionCollectionDefaults } from '../../versions/defaults';
import baseVersionFields from '../../versions/baseFields';
import TimestampsRequired from '../../errors/TimestampsRequired';
import mergeBaseFields from '../../fields/mergeBaseFields';
Expand Down Expand Up @@ -40,12 +39,14 @@ const sanitizeCollection = (config: Config, collection: CollectionConfig): Sanit
};
}

if (sanitized.versions.drafts.autosave === true) sanitized.versions.drafts.autosave = {};
if (sanitized.versions.drafts.autosave === true) {
sanitized.versions.drafts.autosave = {
interval: 2000,
};
}

sanitized.fields = mergeBaseFields(sanitized.fields, baseVersionFields);
}

sanitized.versions = merge(versionCollectionDefaults, sanitized.versions);
}

if (sanitized.upload) {
Expand Down
9 changes: 5 additions & 4 deletions src/globals/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { GlobalConfig, SanitizedGlobalConfig } from './types';
import defaultAccess from '../../auth/defaultAccess';
import baseVersionFields from '../../versions/baseFields';
import mergeBaseFields from '../../fields/mergeBaseFields';
import { versionGlobalDefaults } from '../../versions/defaults';

const sanitizeGlobals = (collections: CollectionConfig[], globals: GlobalConfig[]): SanitizedGlobalConfig[] => {
const sanitizedGlobals = globals.map((global) => {
Expand Down Expand Up @@ -42,12 +41,14 @@ const sanitizeGlobals = (collections: CollectionConfig[], globals: GlobalConfig[
};
}

if (sanitizedGlobal.versions.drafts.autosave === true) sanitizedGlobal.versions.drafts.autosave = {};
if (sanitizedGlobal.versions.drafts.autosave === true) {
sanitizedGlobal.versions.drafts.autosave = {
interval: 2000,
};
}

sanitizedGlobal.fields = mergeBaseFields(sanitizedGlobal.fields, baseVersionFields);
}

sanitizedGlobal.versions = merge(versionGlobalDefaults, sanitizedGlobal.versions);
}

// /////////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/getEntityPolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function getEntityPolicies<T extends Args>(args: T): ReturnType<T> {
}
} else if (field.fields) {
executeFieldPolicies({
policiesObj: mutablePolicies,
policiesObj,
fields: field.fields,
operation,
entityAccessPromise,
Expand All @@ -156,14 +156,14 @@ export function getEntityPolicies<T extends Args>(args: T): ReturnType<T> {
if (tabHasName(tab)) {
if (!mutablePolicies[tab.name]) mutablePolicies[tab.name] = { fields: {} };
executeFieldPolicies({
policiesObj: mutablePolicies[tab.name].fields,
policiesObj: mutablePolicies[tab.name],
fields: tab.fields,
operation,
entityAccessPromise,
});
} else {
executeFieldPolicies({
policiesObj: mutablePolicies,
policiesObj,
fields: tab.fields,
operation,
entityAccessPromise,
Expand Down
17 changes: 0 additions & 17 deletions src/versions/defaults.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/access-control/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('access control', () => {

async function createDoc(data: any): Promise<{ id: string }> {
return payload.create({
collection: docLevelAccessSlug,
collection: slug,
data,
});
}

0 comments on commit 3f9bbe9

Please sign in to comment.