fix(list-entries-integration): don't show commit date & author default sort (#4849)

This commit is contained in:
Erez Rokah 2021-01-19 06:59:00 -08:00 committed by GitHub
parent 38f37cd0c6
commit 529186a7cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 36 deletions

View File

@ -5,6 +5,7 @@ import { trimStart, trim, get, isPlainObject, isEmpty } from 'lodash';
import * as publishModes from 'Constants/publishModes'; import * as publishModes from 'Constants/publishModes';
import { validateConfig } from 'Constants/configSchema'; import { validateConfig } from 'Constants/configSchema';
import { selectDefaultSortableFields, traverseFields } from '../reducers/collections'; import { selectDefaultSortableFields, traverseFields } from '../reducers/collections';
import { getIntegrations, selectIntegration } from '../reducers/integrations';
import { resolveBackend } from 'coreSrc/backend'; import { resolveBackend } from 'coreSrc/backend';
import { I18N, I18N_FIELD, I18N_STRUCTURE } from '../lib/i18n'; import { I18N, I18N_FIELD, I18N_STRUCTURE } from '../lib/i18n';
@ -143,6 +144,12 @@ const defaults = {
publish_mode: publishModes.SIMPLE, publish_mode: publishModes.SIMPLE,
}; };
const hasIntegration = (config, collection) => {
const integrations = getIntegrations(config);
const integration = selectIntegration(integrations, collection.get('name'), 'listEntries');
return !!integration;
};
export function normalizeConfig(config) { export function normalizeConfig(config) {
return Map(config).withMutations(map => { return Map(config).withMutations(map => {
map.set( map.set(
@ -281,7 +288,11 @@ export function applyDefaults(config) {
if (!collection.has('sortable_fields')) { if (!collection.has('sortable_fields')) {
const backend = resolveBackend(config); const backend = resolveBackend(config);
const defaultSortable = selectDefaultSortableFields(collection, backend); const defaultSortable = selectDefaultSortableFields(
collection,
backend,
hasIntegration(map, collection),
);
collection = collection.set('sortable_fields', fromJS(defaultSortable)); collection = collection.set('sortable_fields', fromJS(defaultSortable));
} }

View File

@ -377,17 +377,21 @@ export const selectEntryCollectionTitle = (collection: Collection, entry: EntryM
export const COMMIT_AUTHOR = 'commit_author'; export const COMMIT_AUTHOR = 'commit_author';
export const COMMIT_DATE = 'commit_date'; export const COMMIT_DATE = 'commit_date';
export const selectDefaultSortableFields = (collection: Collection, backend: Backend) => { export const selectDefaultSortableFields = (
collection: Collection,
backend: Backend,
hasIntegration: boolean,
) => {
let defaultSortable = SORTABLE_FIELDS.map((type: string) => { let defaultSortable = SORTABLE_FIELDS.map((type: string) => {
const field = selectInferedField(collection, type); const field = selectInferedField(collection, type);
if (backend.isGitBackend() && type === 'author' && !field) { if (backend.isGitBackend() && type === 'author' && !field && !hasIntegration) {
// default to commit author if not author field is found // default to commit author if not author field is found
return COMMIT_AUTHOR; return COMMIT_AUTHOR;
} }
return field; return field;
}).filter(Boolean); }).filter(Boolean);
if (backend.isGitBackend()) { if (backend.isGitBackend() && !hasIntegration) {
// always have commit date by default // always have commit date by default
defaultSortable = [COMMIT_DATE, ...defaultSortable]; defaultSortable = [COMMIT_DATE, ...defaultSortable];
} }

View File

@ -1,42 +1,49 @@
import { fromJS, List } from 'immutable'; import { fromJS, List } from 'immutable';
import { CONFIG_SUCCESS } from '../actions/config'; import { CONFIG_SUCCESS } from '../actions/config';
import { Integrations, IntegrationsAction, Integration } from '../types/redux'; import { Integrations, IntegrationsAction, Integration, Config } from '../types/redux';
interface Acc { interface Acc {
providers: Record<string, {}>; providers: Record<string, {}>;
hooks: Record<string, string | Record<string, string>>; hooks: Record<string, string | Record<string, string>>;
} }
export const getIntegrations = (config: Config) => {
const integrations: Integration[] = config.get('integrations', List()).toJS() || [];
const newState = integrations.reduce(
(acc, integration) => {
const { hooks, collections, provider, ...providerData } = integration;
acc.providers[provider] = { ...providerData };
if (!collections) {
hooks.forEach(hook => {
acc.hooks[hook] = provider;
});
return acc;
}
const integrationCollections =
collections === '*'
? config
.get('collections')
.map(collection => collection!.get('name'))
.toArray()
: (collections as string[]);
integrationCollections.forEach(collection => {
hooks.forEach(hook => {
acc.hooks[collection]
? ((acc.hooks[collection] as Record<string, string>)[hook] = provider)
: (acc.hooks[collection] = { [hook]: provider });
});
});
return acc;
},
{ providers: {}, hooks: {} } as Acc,
);
return fromJS(newState);
};
const integrations = (state = null, action: IntegrationsAction): Integrations | null => { const integrations = (state = null, action: IntegrationsAction): Integrations | null => {
switch (action.type) { switch (action.type) {
case CONFIG_SUCCESS: { case CONFIG_SUCCESS: {
const integrations: Integration[] = action.payload.get('integrations', List()).toJS() || []; return getIntegrations(action.payload);
const newState = integrations.reduce(
(acc, integration) => {
const { hooks, collections, provider, ...providerData } = integration;
acc.providers[provider] = { ...providerData };
if (!collections) {
hooks.forEach(hook => {
acc.hooks[hook] = provider;
});
return acc;
}
const integrationCollections =
collections === '*'
? action.payload.get('collections').map(collection => collection.get('name'))
: (collections as string[]);
integrationCollections.forEach(collection => {
hooks.forEach(hook => {
acc.hooks[collection]
? ((acc.hooks[collection] as Record<string, string>)[hook] = provider)
: (acc.hooks[collection] = { [hook]: provider });
});
});
return acc;
},
{ providers: {}, hooks: {} } as Acc,
);
return fromJS(newState);
} }
default: default:
return state; return state;

View File

@ -44,6 +44,8 @@ export type Config = StaticallyTypedRecord<{
site_url?: string; site_url?: string;
show_preview_links?: boolean; show_preview_links?: boolean;
isFetching?: boolean; isFetching?: boolean;
integrations: List<Integration>;
collections: List<StaticallyTypedRecord<{ name: string }>>;
}>; }>;
type PagesObject = { type PagesObject = {
@ -315,10 +317,7 @@ export interface Integration {
} }
export interface IntegrationsAction extends Action<string> { export interface IntegrationsAction extends Action<string> {
payload: StaticallyTypedRecord<{ payload: Config;
integrations: List<Integration>;
collections: StaticallyTypedRecord<{ name: string }>[];
}>;
} }
interface EntryPayload { interface EntryPayload {