2019-12-18 18:16:02 +02:00
|
|
|
import { fromJS, List, Map, Set } from 'immutable';
|
2020-04-01 06:13:27 +03:00
|
|
|
import { isEqual, orderBy } from 'lodash';
|
2016-10-17 12:35:31 +02:00
|
|
|
import { actions as notifActions } from 'redux-notifications';
|
2019-12-18 18:16:02 +02:00
|
|
|
import { serializeValues } from '../lib/serializeEntryValues';
|
|
|
|
import { currentBackend, Backend } from '../backend';
|
|
|
|
import { getIntegrationProvider } from '../integrations';
|
|
|
|
import { selectIntegration, selectPublishedSlugs } from '../reducers';
|
2020-04-01 06:13:27 +03:00
|
|
|
import { selectFields, updateFieldByKey, selectSortDataPath } from '../reducers/collections';
|
2019-12-18 18:16:02 +02:00
|
|
|
import { selectCollectionEntriesCursor } from '../reducers/cursors';
|
2020-01-15 00:15:14 +02:00
|
|
|
import { Cursor, ImplementationMediaFile } from 'netlify-cms-lib-util';
|
2019-12-18 18:16:02 +02:00
|
|
|
import { createEntry, EntryValue } from '../valueObjects/Entry';
|
|
|
|
import AssetProxy, { createAssetProxy } from '../valueObjects/AssetProxy';
|
|
|
|
import ValidationErrorTypes from '../constants/validationErrorTypes';
|
|
|
|
import { addAssets, getAsset } from './media';
|
2020-04-01 06:13:27 +03:00
|
|
|
import {
|
|
|
|
Collection,
|
|
|
|
EntryMap,
|
|
|
|
State,
|
|
|
|
EntryFields,
|
|
|
|
EntryField,
|
|
|
|
SortDirection,
|
|
|
|
} from '../types/redux';
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
import { ThunkDispatch } from 'redux-thunk';
|
2020-03-22 16:53:06 +02:00
|
|
|
import { AnyAction } from 'redux';
|
2020-01-15 00:15:14 +02:00
|
|
|
import { waitForMediaLibraryToLoad, loadMedia } from './mediaLibrary';
|
2019-12-25 10:47:02 +01:00
|
|
|
import { waitUntil } from './waitUntil';
|
2020-04-01 06:13:27 +03:00
|
|
|
import { selectIsFetching, selectEntriesSortFields } from '../reducers/entries';
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2016-10-17 12:35:31 +02:00
|
|
|
const { notifSend } = notifActions;
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
2019-09-24 22:16:09 +02:00
|
|
|
* Constant Declarations
|
2016-06-08 04:42:24 -03:00
|
|
|
*/
|
2016-06-05 01:52:18 -07:00
|
|
|
export const ENTRY_REQUEST = 'ENTRY_REQUEST';
|
|
|
|
export const ENTRY_SUCCESS = 'ENTRY_SUCCESS';
|
|
|
|
export const ENTRY_FAILURE = 'ENTRY_FAILURE';
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
export const ENTRIES_REQUEST = 'ENTRIES_REQUEST';
|
|
|
|
export const ENTRIES_SUCCESS = 'ENTRIES_SUCCESS';
|
|
|
|
export const ENTRIES_FAILURE = 'ENTRIES_FAILURE';
|
|
|
|
|
2020-04-01 06:13:27 +03:00
|
|
|
export const SORT_ENTRIES_REQUEST = 'SORT_ENTRIES_REQUEST';
|
|
|
|
export const SORT_ENTRIES_SUCCESS = 'SORT_ENTRIES_SUCCESS';
|
|
|
|
export const SORT_ENTRIES_FAILURE = 'SORT_ENTRIES_FAILURE';
|
|
|
|
|
2016-07-19 17:11:22 -03:00
|
|
|
export const DRAFT_CREATE_FROM_ENTRY = 'DRAFT_CREATE_FROM_ENTRY';
|
2016-08-24 21:36:44 -03:00
|
|
|
export const DRAFT_CREATE_EMPTY = 'DRAFT_CREATE_EMPTY';
|
2016-06-08 04:42:24 -03:00
|
|
|
export const DRAFT_DISCARD = 'DRAFT_DISCARD';
|
2016-12-29 17:18:24 -02:00
|
|
|
export const DRAFT_CHANGE_FIELD = 'DRAFT_CHANGE_FIELD';
|
2017-01-13 19:30:40 -02:00
|
|
|
export const DRAFT_VALIDATION_ERRORS = 'DRAFT_VALIDATION_ERRORS';
|
2019-02-05 23:27:34 +01:00
|
|
|
export const DRAFT_CLEAR_ERRORS = 'DRAFT_CLEAR_ERRORS';
|
2019-02-28 13:30:23 -05:00
|
|
|
export const DRAFT_LOCAL_BACKUP_RETRIEVED = 'DRAFT_LOCAL_BACKUP_RETRIEVED';
|
|
|
|
export const DRAFT_CREATE_FROM_LOCAL_BACKUP = 'DRAFT_CREATE_FROM_LOCAL_BACKUP';
|
2019-12-11 02:33:02 +01:00
|
|
|
export const DRAFT_CREATE_DUPLICATE_FROM_ENTRY = 'DRAFT_CREATE_DUPLICATE_FROM_ENTRY';
|
2016-06-10 00:16:01 -03:00
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
export const ENTRY_PERSIST_REQUEST = 'ENTRY_PERSIST_REQUEST';
|
|
|
|
export const ENTRY_PERSIST_SUCCESS = 'ENTRY_PERSIST_SUCCESS';
|
|
|
|
export const ENTRY_PERSIST_FAILURE = 'ENTRY_PERSIST_FAILURE';
|
|
|
|
|
2017-07-21 23:40:33 -07:00
|
|
|
export const ENTRY_DELETE_REQUEST = 'ENTRY_DELETE_REQUEST';
|
|
|
|
export const ENTRY_DELETE_SUCCESS = 'ENTRY_DELETE_SUCCESS';
|
|
|
|
export const ENTRY_DELETE_FAILURE = 'ENTRY_DELETE_FAILURE';
|
|
|
|
|
2019-11-17 11:51:50 +02:00
|
|
|
export const ADD_DRAFT_ENTRY_MEDIA_FILE = 'ADD_DRAFT_ENTRY_MEDIA_FILE';
|
|
|
|
export const REMOVE_DRAFT_ENTRY_MEDIA_FILE = 'REMOVE_DRAFT_ENTRY_MEDIA_FILE';
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Simple Action Creators (Internal)
|
2016-09-20 14:00:03 +02:00
|
|
|
* We still need to export them for tests
|
2016-06-08 04:42:24 -03:00
|
|
|
*/
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryLoading(collection: Collection, slug: string) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_REQUEST,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
2016-10-12 16:01:27 +02:00
|
|
|
slug,
|
|
|
|
},
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryLoaded(collection: Collection, entry: EntryValue) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
2016-10-12 16:01:27 +02:00
|
|
|
entry,
|
|
|
|
},
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryLoadError(error: Error, collection: Collection, slug: string) {
|
2017-01-10 22:23:22 -02:00
|
|
|
return {
|
|
|
|
type: ENTRY_FAILURE,
|
|
|
|
payload: {
|
|
|
|
error,
|
|
|
|
collection: collection.get('name'),
|
|
|
|
slug,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entriesLoading(collection: Collection) {
|
2016-06-08 04:42:24 -03:00
|
|
|
return {
|
|
|
|
type: ENTRIES_REQUEST,
|
|
|
|
payload: {
|
2016-10-12 16:01:27 +02:00
|
|
|
collection: collection.get('name'),
|
|
|
|
},
|
2016-06-08 04:42:24 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entriesLoaded(
|
|
|
|
collection: Collection,
|
|
|
|
entries: EntryValue[],
|
|
|
|
pagination: number | null,
|
2020-01-15 00:15:14 +02:00
|
|
|
cursor: Cursor,
|
2019-12-18 18:16:02 +02:00
|
|
|
append = true,
|
|
|
|
) {
|
2016-06-08 04:42:24 -03:00
|
|
|
return {
|
|
|
|
type: ENTRIES_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
2016-10-12 16:01:27 +02:00
|
|
|
entries,
|
|
|
|
page: pagination,
|
2018-06-11 19:03:43 -07:00
|
|
|
cursor: Cursor.create(cursor),
|
|
|
|
append,
|
2016-10-12 16:01:27 +02:00
|
|
|
},
|
2016-06-08 04:42:24 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entriesFailed(collection: Collection, error: Error) {
|
2016-06-08 04:42:24 -03:00
|
|
|
return {
|
|
|
|
type: ENTRIES_FAILURE,
|
|
|
|
error: 'Failed to load entries',
|
|
|
|
payload: error.toString(),
|
2016-10-12 16:01:27 +02:00
|
|
|
meta: { collection: collection.get('name') },
|
2016-06-08 04:42:24 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-01 06:13:27 +03:00
|
|
|
export function sortByField(
|
|
|
|
collection: Collection,
|
|
|
|
key: string,
|
|
|
|
direction: SortDirection = SortDirection.Ascending,
|
|
|
|
) {
|
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
|
|
|
|
// if we're already fetching we update the sort key, but skip loading entries
|
|
|
|
const isFetching = selectIsFetching(state.entries, collection.get('name'));
|
|
|
|
dispatch({
|
|
|
|
type: SORT_ENTRIES_REQUEST,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
|
|
|
key,
|
|
|
|
direction,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (isFetching) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const integration = selectIntegration(state, collection.get('name'), 'listEntries');
|
|
|
|
const provider: Backend = integration
|
|
|
|
? getIntegrationProvider(state.integrations, backend.getToken, integration)
|
|
|
|
: backend;
|
|
|
|
|
|
|
|
let entries = await provider.listAllEntries(collection);
|
|
|
|
|
|
|
|
const sortFields = selectEntriesSortFields(getState().entries, collection.get('name'));
|
|
|
|
if (sortFields && sortFields.length > 0) {
|
|
|
|
const keys = sortFields.map(v => selectSortDataPath(collection, v.get('key')));
|
|
|
|
const orders = sortFields.map(v =>
|
|
|
|
v.get('direction') === SortDirection.Ascending ? 'asc' : 'desc',
|
|
|
|
);
|
|
|
|
entries = orderBy(entries, keys, orders);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: SORT_ENTRIES_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
|
|
|
key,
|
|
|
|
direction,
|
|
|
|
entries,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
dispatch({
|
|
|
|
type: SORT_ENTRIES_FAILURE,
|
|
|
|
payload: {
|
|
|
|
collection: collection.get('name'),
|
|
|
|
key,
|
|
|
|
direction,
|
|
|
|
error,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryPersisting(collection: Collection, entry: EntryMap) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return {
|
2016-06-06 21:53:22 -03:00
|
|
|
type: ENTRY_PERSIST_REQUEST,
|
2016-02-25 20:40:35 -08:00
|
|
|
payload: {
|
2016-10-12 19:19:05 +02:00
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: entry.get('slug'),
|
2016-10-12 16:01:27 +02:00
|
|
|
},
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryPersisted(collection: Collection, entry: EntryMap, slug: string) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return {
|
|
|
|
type: ENTRY_PERSIST_SUCCESS,
|
|
|
|
payload: {
|
2016-10-12 19:19:05 +02:00
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: entry.get('slug'),
|
WIP - Global UI (#785)
* update top bar and collections sidebar UI
* update collection entries UI
* improve global layout
* merge search page into collection page
* enable new entry button
* search fixup
* wip -initial editor update
* update editor scrolling and markdown toolbar position
* wip
* editor toolbar progress
* editor toolbar wip
* finished basic editor toolbar
* add standalone toggle component
* improve markdown toolbar spacing
* add user avatar placeholder
* finish markdown toggle styling
* refactor icon setup, add new icons
* add new icons to markdown editor toolbar
* remove extra app container
* add markdown active mark style
* relation and text widget styling
* widget design updates, basic list/object design update
* widget style updates, image widget improvements
* refactor widget directory, fix file removal
* widget focus styles
* finish editor widget focus styles
* migrate media library modal to react-modal
* wip - migrate editor component form to modal
* wip - move editor component form to modal
* wip - embed plugin forms in the editor
* inline shortcode forms working
* disable react hot loading, its breaking things
* improve shortcode form styles
* make shortcode form collapsible, improve styling
* add close functionality to shortcode blocks
* improve base media library styling
* fix shortcode label
* migrate unstyled workflow to new UI
* wip - reorganizing everything
* more work moving everything
* finish more moving and eliminating stuff
* restructure, remove react-toolbox
* wip - removing old stuff, more restructure
* finish restructure
* wip - css arch
* switch back to test repo
* update react-datetime to ^2.11.0
* remove leftover react-toolbox button
* more restructuring clean-up
* fix UI component directory case
* wip -css editor control style
* wip - consolidate widget styles
* wip - use a single control renderer
* fixed object values breaking
* wip - editor control active styles
* pass control wrapper to widgets
* ensure branch name is trimmed
* wip - improve widget authoring support
* import Map to Widget component
* refactor toolbar buttons
* wip - more widget active styles
* break out editor toggle component
* add local scroll sync back
* update editor toggle icons
* limit editor control pane content width
* fix editor control spacing
* migrate markdown toolbar stickiness to css
* fix markdown toolbar border radius
* temporarily use test backend
* stop markdown toolbar from going to bottom
* restore disabled markdown toolbar buttons for raw
* test markdown widget without focus styles
* more widget updates
* remove card visuals from editor
* disable dragging editor split off screen
* use editorControl component for shortcode fields
* make header site link configurable
* add configurable collection descriptions
* temporarily add example assets
* add basic list view
* remove outdated css mixins
* add and implement search icon
* activate quick add menu
* visualize usable space in editor view
* fix entry close, other improvements
* wip - editorial workflow updates
* some dropshadow and other CSS tweaks
* workflow ui updates
* add worfklow card buttons
* fix workflow card button handlers
* some dropshadow and other CSS tweaks
* make workflow board wider
* center workflow and collection views
* add basic responsiveness
* a bunch of fun UI fixes! a BUNCH! (#875)
* give `.nc-entryEditor-toolbar-mainSection` left and right child divs
* a bunch of fun UI fixes! a BUNCH!
* remove obscure --buttonShadow
* revert to test repo
* fix not found page styling
* allow workflow publishing from any column
* disallow publishing from all columns, with feedback
* fix new entry button
* fix markdown state persisting across entries
* enable simple workflow save and new from editor
* update slug in address bar when saving new entry
* wip - workflow updates, deletion working
* add status change functionality to editor
* wip - improving status change from editor
* editor toolbar back button improvements, loading improvements, cleanup
* progress on the media library UI cleanup
* remove font smothing css
* a quick fix for these buttons
* tweaks
* progress on media library modal— broken FYI
* fix media library functionality, finish migrating footer
* remove media library footer files
* remove leftover css import
* fix media library
* editor publishing functionality complete (unstyled)
* remove leftover loader var from media library
* wip - editor publishing styles
* add status dropdown styling
* editor toolbar style updates
* editor toolbar state improvements
* progress on the media library UI cleanup, style improvements
* finish editorial workflow editor styling
* finish media library styling
* fix config
* add what-input to optimize focus styling
* fix button
* fix navigation blocking for simple workflow
* improve simple workflow publishing
* add avatar dropdown to editor top bar
* style github and test-repo auth pages
* add git gateway auth page styles
* improve editor error styling
2017-12-07 12:37:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass slug from backend for newly created entries.
|
|
|
|
*/
|
|
|
|
slug,
|
2016-10-12 16:01:27 +02:00
|
|
|
},
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryPersistFail(collection: Collection, entry: EntryMap, error: Error) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return {
|
2016-10-12 19:19:05 +02:00
|
|
|
type: ENTRY_PERSIST_FAILURE,
|
2016-06-06 21:53:22 -03:00
|
|
|
error: 'Failed to persist entry',
|
2016-10-12 19:19:05 +02:00
|
|
|
payload: {
|
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: entry.get('slug'),
|
|
|
|
error: error.toString(),
|
|
|
|
},
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryDeleting(collection: Collection, slug: string) {
|
2017-07-21 23:40:33 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_DELETE_REQUEST,
|
|
|
|
payload: {
|
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: slug,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryDeleted(collection: Collection, slug: string) {
|
2017-07-21 23:40:33 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_DELETE_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: slug,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function entryDeleteFail(collection: Collection, slug: string, error: Error) {
|
2017-07-21 23:40:33 -07:00
|
|
|
return {
|
|
|
|
type: ENTRY_DELETE_FAILURE,
|
|
|
|
payload: {
|
|
|
|
collectionName: collection.get('name'),
|
|
|
|
entrySlug: slug,
|
|
|
|
error: error.toString(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function emptyDraftCreated(entry: EntryValue) {
|
2016-08-24 21:36:44 -03:00
|
|
|
return {
|
|
|
|
type: DRAFT_CREATE_EMPTY,
|
2016-10-12 16:01:27 +02:00
|
|
|
payload: entry,
|
2016-08-24 21:36:44 -03:00
|
|
|
};
|
|
|
|
}
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Exported simple Action Creators
|
|
|
|
*/
|
2020-03-16 20:48:49 +01:00
|
|
|
export function createDraftFromEntry(entry: EntryValue) {
|
2016-02-25 20:40:35 -08:00
|
|
|
return {
|
2016-07-19 17:11:22 -03:00
|
|
|
type: DRAFT_CREATE_FROM_ENTRY,
|
2020-03-16 20:48:49 +01:00
|
|
|
payload: { entry },
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-25 10:47:02 +01:00
|
|
|
export function draftDuplicateEntry(entry: EntryMap) {
|
2019-12-11 02:33:02 +01:00
|
|
|
return {
|
|
|
|
type: DRAFT_CREATE_DUPLICATE_FROM_ENTRY,
|
2020-03-31 12:58:30 +01:00
|
|
|
payload: createEntry(entry.get('collection'), '', '', {
|
|
|
|
data: entry.get('data'),
|
|
|
|
mediaFiles: entry.get('mediaFiles').toJS(),
|
|
|
|
}),
|
2019-12-11 02:33:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
export function discardDraft() {
|
2019-12-18 18:16:02 +02:00
|
|
|
return { type: DRAFT_DISCARD };
|
2016-06-06 21:53:22 -03:00
|
|
|
}
|
|
|
|
|
2020-02-28 11:40:51 +01:00
|
|
|
export function changeDraftField(
|
|
|
|
field: string,
|
|
|
|
value: string,
|
|
|
|
metadata: Record<string, unknown>,
|
|
|
|
entries: EntryMap[],
|
|
|
|
) {
|
2016-12-29 17:18:24 -02:00
|
|
|
return {
|
|
|
|
type: DRAFT_CHANGE_FIELD,
|
2020-02-28 11:40:51 +01:00
|
|
|
payload: { field, value, metadata, entries },
|
2016-12-29 17:18:24 -02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function changeDraftFieldValidation(
|
|
|
|
uniquefieldId: string,
|
|
|
|
errors: { type: string; message: string }[],
|
|
|
|
) {
|
2017-01-13 19:30:40 -02:00
|
|
|
return {
|
|
|
|
type: DRAFT_VALIDATION_ERRORS,
|
2019-02-05 23:27:34 +01:00
|
|
|
payload: { uniquefieldId, errors },
|
2017-01-13 19:30:40 -02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-05 23:27:34 +01:00
|
|
|
export function clearFieldErrors() {
|
|
|
|
return { type: DRAFT_CLEAR_ERRORS };
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function localBackupRetrieved(entry: EntryValue) {
|
2019-02-28 13:30:23 -05:00
|
|
|
return {
|
|
|
|
type: DRAFT_LOCAL_BACKUP_RETRIEVED,
|
2019-12-18 18:16:02 +02:00
|
|
|
payload: { entry },
|
2019-02-28 13:30:23 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadLocalBackup() {
|
2019-12-18 18:16:02 +02:00
|
|
|
return {
|
|
|
|
type: DRAFT_CREATE_FROM_LOCAL_BACKUP,
|
2019-02-28 13:30:23 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
export function addDraftEntryMediaFile(file: ImplementationMediaFile) {
|
2019-11-17 11:51:50 +02:00
|
|
|
return { type: ADD_DRAFT_ENTRY_MEDIA_FILE, payload: file };
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function removeDraftEntryMediaFile({ id }: { id: string }) {
|
|
|
|
return { type: REMOVE_DRAFT_ENTRY_MEDIA_FILE, payload: { id } };
|
2019-11-17 11:51:50 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function persistLocalBackup(entry: EntryMap, collection: Collection) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return (_dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2019-02-28 13:30:23 -05:00
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
2019-11-17 11:51:50 +02:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
return backend.persistLocalDraftBackup(entry, collection);
|
2019-02-28 13:30:23 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-25 10:47:02 +01:00
|
|
|
export function createDraftDuplicateFromEntry(entry: EntryMap) {
|
|
|
|
return (dispatch: ThunkDispatch<State, {}, AnyAction>) => {
|
|
|
|
dispatch(
|
|
|
|
waitUntil({
|
|
|
|
predicate: ({ type }) => type === DRAFT_CREATE_EMPTY,
|
|
|
|
run: () => dispatch(draftDuplicateEntry(entry)),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function retrieveLocalBackup(collection: Collection, slug: string) {
|
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2019-02-28 13:30:23 -05:00
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
2019-12-18 18:16:02 +02:00
|
|
|
const { entry } = await backend.getLocalDraftBackup(collection, slug);
|
2019-11-17 11:51:50 +02:00
|
|
|
|
2019-02-28 13:30:23 -05:00
|
|
|
if (entry) {
|
2019-11-17 11:51:50 +02:00
|
|
|
// load assets from backup
|
2019-12-18 18:16:02 +02:00
|
|
|
const mediaFiles = entry.mediaFiles || [];
|
2020-02-04 11:12:59 +02:00
|
|
|
const assetProxies: AssetProxy[] = await Promise.all(
|
|
|
|
mediaFiles.map(file => {
|
|
|
|
if (file.file || file.url) {
|
2020-02-10 18:05:47 +02:00
|
|
|
return createAssetProxy({
|
|
|
|
path: file.path,
|
|
|
|
file: file.file,
|
|
|
|
url: file.url,
|
2020-02-14 22:31:33 +02:00
|
|
|
field: file.field,
|
2020-02-10 18:05:47 +02:00
|
|
|
});
|
2020-02-04 11:12:59 +02:00
|
|
|
} else {
|
2020-02-10 18:05:47 +02:00
|
|
|
return getAsset({
|
|
|
|
collection,
|
|
|
|
entry: fromJS(entry),
|
|
|
|
path: file.path,
|
2020-02-14 22:31:33 +02:00
|
|
|
field: file.field,
|
2020-02-10 18:05:47 +02:00
|
|
|
})(dispatch, getState);
|
2020-02-04 11:12:59 +02:00
|
|
|
}
|
|
|
|
}),
|
2019-11-17 11:51:50 +02:00
|
|
|
);
|
|
|
|
dispatch(addAssets(assetProxies));
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
return dispatch(localBackupRetrieved(entry));
|
2019-02-28 13:30:23 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function deleteLocalBackup(collection: Collection, slug: string) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return (_dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2019-02-28 13:30:23 -05:00
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
return backend.deleteLocalDraftBackup(collection, slug);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-08 04:42:24 -03:00
|
|
|
/*
|
|
|
|
* Exported Thunk Action Creators
|
|
|
|
*/
|
2016-10-10 15:34:21 -03:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function loadEntry(collection: Collection, slug: string) {
|
2020-02-03 13:33:09 +01:00
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
|
|
|
await waitForMediaLibraryToLoad(dispatch, getState());
|
2016-06-05 01:52:18 -07:00
|
|
|
dispatch(entryLoading(collection, slug));
|
2020-03-16 20:48:49 +01:00
|
|
|
|
|
|
|
try {
|
2020-03-20 19:54:53 +09:00
|
|
|
const loadedEntry = await tryLoadEntry(getState(), collection, slug);
|
2020-03-16 20:48:49 +01:00
|
|
|
dispatch(entryLoaded(collection, loadedEntry));
|
|
|
|
dispatch(createDraftFromEntry(loadedEntry));
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
dispatch(
|
|
|
|
notifSend({
|
|
|
|
message: {
|
|
|
|
details: error.message,
|
|
|
|
key: 'ui.toast.onFailToLoadEntries',
|
|
|
|
},
|
|
|
|
kind: 'danger',
|
|
|
|
dismissAfter: 8000,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
dispatch(entryLoadError(error, collection, slug));
|
|
|
|
}
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-20 19:54:53 +09:00
|
|
|
export async function tryLoadEntry(state: State, collection: Collection, slug: string) {
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
const loadedEntry = await backend.getEntry(state, collection, slug);
|
|
|
|
return loadedEntry;
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:03:43 -07:00
|
|
|
const appendActions = fromJS({
|
2018-08-07 14:46:54 -06:00
|
|
|
['append_next']: { action: 'next', append: true },
|
2018-06-11 19:03:43 -07:00
|
|
|
});
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
const addAppendActionsToCursor = (cursor: Cursor) => {
|
2019-12-18 18:16:02 +02:00
|
|
|
return Cursor.create(cursor).updateStore('actions', (actions: Set<string>) => {
|
|
|
|
return actions.union(
|
|
|
|
appendActions
|
|
|
|
.filter((v: Map<string, string | boolean>) => actions.has(v.get('action') as string))
|
|
|
|
.keySeq(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export function loadEntries(collection: Collection, page = 0) {
|
2020-04-01 06:13:27 +03:00
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2016-10-17 12:35:31 +02:00
|
|
|
if (collection.get('isFetching')) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-25 20:40:35 -08:00
|
|
|
const state = getState();
|
2020-04-01 06:13:27 +03:00
|
|
|
const sortFields = selectEntriesSortFields(state.entries, collection.get('name'));
|
|
|
|
if (sortFields && sortFields.length > 0) {
|
|
|
|
const field = sortFields[0];
|
|
|
|
return dispatch(sortByField(collection, field.get('key'), field.get('direction')));
|
|
|
|
}
|
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
const backend = currentBackend(state.config);
|
2016-10-10 15:34:21 -03:00
|
|
|
const integration = selectIntegration(state, collection.get('name'), 'listEntries');
|
2018-08-07 14:46:54 -06:00
|
|
|
const provider = integration
|
|
|
|
? getIntegrationProvider(state.integrations, backend.getToken, integration)
|
|
|
|
: backend;
|
2018-06-11 19:03:43 -07:00
|
|
|
const append = !!(page && !isNaN(page) && page > 0);
|
2016-02-25 20:40:35 -08:00
|
|
|
dispatch(entriesLoading(collection));
|
2018-08-07 14:46:54 -06:00
|
|
|
|
2020-04-01 06:13:27 +03:00
|
|
|
try {
|
|
|
|
let response: {
|
|
|
|
cursor: Cursor;
|
|
|
|
pagination: number;
|
|
|
|
entries: EntryValue[];
|
|
|
|
} = await provider.listEntries(collection, page);
|
|
|
|
response = {
|
|
|
|
...response,
|
2018-08-07 14:46:54 -06:00
|
|
|
// The only existing backend using the pagination system is the
|
|
|
|
// Algolia integration, which is also the only integration used
|
|
|
|
// to list entries. Thus, this checking for an integration can
|
|
|
|
// determine whether or not this is using the old integer-based
|
|
|
|
// pagination API. Other backends will simply store an empty
|
|
|
|
// cursor, which behaves identically to no cursor at all.
|
|
|
|
cursor: integration
|
|
|
|
? Cursor.create({
|
|
|
|
actions: ['next'],
|
|
|
|
meta: { usingOldPaginationAPI: true },
|
|
|
|
data: { nextPage: page + 1 },
|
|
|
|
})
|
|
|
|
: Cursor.create(response.cursor),
|
2020-04-01 06:13:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
entriesLoaded(
|
|
|
|
collection,
|
|
|
|
response.cursor.meta!.get('usingOldPaginationAPI')
|
|
|
|
? response.entries.reverse()
|
|
|
|
: response.entries,
|
|
|
|
response.pagination,
|
|
|
|
addAppendActionsToCursor(response.cursor),
|
|
|
|
append,
|
2018-08-07 14:46:54 -06:00
|
|
|
),
|
2020-04-01 06:13:27 +03:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
dispatch(
|
|
|
|
notifSend({
|
|
|
|
message: {
|
|
|
|
details: err,
|
|
|
|
key: 'ui.toast.onFailToLoadEntries',
|
|
|
|
},
|
|
|
|
kind: 'danger',
|
|
|
|
dismissAfter: 8000,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
return Promise.reject(dispatch(entriesFailed(collection, err)));
|
|
|
|
}
|
2016-06-06 21:53:22 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
function traverseCursor(backend: Backend, cursor: Cursor, action: string) {
|
|
|
|
if (!cursor.actions!.has(action)) {
|
2018-08-07 14:46:54 -06:00
|
|
|
throw new Error(`The current cursor does not support the pagination action "${action}".`);
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
return backend.traverseCursor(cursor, action);
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function traverseCollectionCursor(collection: Collection, action: string) {
|
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2018-06-11 19:03:43 -07:00
|
|
|
const state = getState();
|
2019-12-18 18:16:02 +02:00
|
|
|
const collectionName = collection.get('name');
|
|
|
|
if (state.entries.getIn(['pages', `${collectionName}`, 'isFetching'])) {
|
2018-06-11 19:03:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
|
|
|
|
const { action: realAction, append } = appendActions.has(action)
|
|
|
|
? appendActions.get(action).toJS()
|
|
|
|
: { action, append: false };
|
|
|
|
const cursor = selectCollectionEntriesCursor(state.cursors, collection.get('name'));
|
|
|
|
|
|
|
|
// Handle cursors representing pages in the old, integer-based
|
|
|
|
// pagination API
|
2020-01-15 00:15:14 +02:00
|
|
|
if (cursor.meta!.get('usingOldPaginationAPI', false)) {
|
|
|
|
return dispatch(loadEntries(collection, cursor.data!.get('nextPage') as number));
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
dispatch(entriesLoading(collection));
|
|
|
|
const { entries, cursor: newCursor } = await traverseCursor(backend, cursor, realAction);
|
2020-04-01 06:13:27 +03:00
|
|
|
|
|
|
|
const pagination = newCursor.meta?.get('page');
|
2018-08-07 14:46:54 -06:00
|
|
|
return dispatch(
|
2020-04-01 06:13:27 +03:00
|
|
|
entriesLoaded(collection, entries, pagination, addAppendActionsToCursor(newCursor), append),
|
2018-08-07 14:46:54 -06:00
|
|
|
);
|
2018-06-11 19:03:43 -07:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2018-08-07 14:46:54 -06:00
|
|
|
dispatch(
|
|
|
|
notifSend({
|
2018-11-02 14:19:49 -03:00
|
|
|
message: {
|
|
|
|
details: err,
|
2020-04-01 06:13:27 +03:00
|
|
|
key: 'ui.toast.onFailToLoadEntries',
|
2018-11-02 14:19:49 -03:00
|
|
|
},
|
2018-08-07 14:46:54 -06:00
|
|
|
kind: 'danger',
|
|
|
|
dismissAfter: 8000,
|
|
|
|
}),
|
|
|
|
);
|
2018-06-11 19:03:43 -07:00
|
|
|
return Promise.reject(dispatch(entriesFailed(collection, err)));
|
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
};
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
|
2020-03-02 11:32:22 +01:00
|
|
|
const escapeHtml = (unsafe: string) => {
|
|
|
|
return unsafe
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
|
|
|
};
|
|
|
|
|
|
|
|
const processValue = (unsafe: string) => {
|
|
|
|
if (['true', 'True', 'TRUE'].includes(unsafe)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (['false', 'False', 'FALSE'].includes(unsafe)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return escapeHtml(unsafe);
|
|
|
|
};
|
|
|
|
|
|
|
|
export function createEmptyDraft(collection: Collection, search: string) {
|
2019-12-18 18:16:02 +02:00
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2020-03-02 11:32:22 +01:00
|
|
|
const params = new URLSearchParams(search);
|
|
|
|
params.forEach((value, key) => {
|
|
|
|
collection = updateFieldByKey(collection, key, field =>
|
|
|
|
field.set('default', processValue(value)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const fields = collection.get('fields', List());
|
|
|
|
const dataFields = createEmptyDraftData(fields);
|
2019-12-18 18:16:02 +02:00
|
|
|
|
2020-03-22 16:53:06 +02:00
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
if (!collection.has('media_folder')) {
|
|
|
|
await waitForMediaLibraryToLoad(dispatch, getState());
|
|
|
|
}
|
|
|
|
|
2020-03-22 16:53:06 +02:00
|
|
|
let newEntry = createEntry(collection.get('name'), '', '', {
|
|
|
|
data: dataFields,
|
|
|
|
mediaFiles: [],
|
|
|
|
});
|
|
|
|
newEntry = await backend.processEntry(state, collection, newEntry);
|
2016-12-29 17:18:24 -02:00
|
|
|
dispatch(emptyDraftCreated(newEntry));
|
2016-08-24 21:36:44 -03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
interface DraftEntryData {
|
2020-03-02 11:32:22 +01:00
|
|
|
[name: string]:
|
|
|
|
| string
|
|
|
|
| null
|
|
|
|
| boolean
|
2020-05-13 13:24:12 +03:00
|
|
|
| List<unknown>
|
2020-03-02 11:32:22 +01:00
|
|
|
| DraftEntryData
|
|
|
|
| DraftEntryData[]
|
2020-05-13 13:24:12 +03:00
|
|
|
| (string | DraftEntryData | boolean | List<unknown>)[];
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createEmptyDraftData(fields: EntryFields, withNameKey = true) {
|
|
|
|
return fields.reduce(
|
2020-03-02 11:32:22 +01:00
|
|
|
(
|
2020-05-13 13:24:12 +03:00
|
|
|
reduction: DraftEntryData | string | undefined | boolean | List<unknown>,
|
2020-03-02 11:32:22 +01:00
|
|
|
value: EntryField | undefined | boolean,
|
|
|
|
) => {
|
2019-12-18 18:16:02 +02:00
|
|
|
const acc = reduction as DraftEntryData;
|
|
|
|
const item = value as EntryField;
|
|
|
|
const subfields = item.get('field') || item.get('fields');
|
|
|
|
const list = item.get('widget') == 'list';
|
|
|
|
const name = item.get('name');
|
|
|
|
const defaultValue = item.get('default', null);
|
|
|
|
const isEmptyDefaultValue = (val: unknown) => [[{}], {}].some(e => isEqual(val, e));
|
|
|
|
|
|
|
|
if (List.isList(subfields)) {
|
|
|
|
const subDefaultValue = list
|
|
|
|
? [createEmptyDraftData(subfields as EntryFields)]
|
|
|
|
: createEmptyDraftData(subfields as EntryFields);
|
|
|
|
if (!isEmptyDefaultValue(subDefaultValue)) {
|
|
|
|
acc[name] = subDefaultValue;
|
2020-05-13 13:24:12 +03:00
|
|
|
} else if (list && List.isList(defaultValue) && (defaultValue as List<unknown>).isEmpty()) {
|
|
|
|
// allow setting an empty list as a default
|
|
|
|
acc[name] = defaultValue;
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
return acc;
|
2019-06-14 16:46:33 +01:00
|
|
|
}
|
2018-11-02 15:55:08 +01:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
if (Map.isMap(subfields)) {
|
|
|
|
const subDefaultValue = list
|
|
|
|
? [createEmptyDraftData(List([subfields as EntryField]), false)]
|
|
|
|
: createEmptyDraftData(List([subfields as EntryField]));
|
|
|
|
if (!isEmptyDefaultValue(subDefaultValue)) {
|
|
|
|
acc[name] = subDefaultValue;
|
2020-05-13 13:24:12 +03:00
|
|
|
} else if (list && List.isList(defaultValue) && (defaultValue as List<unknown>).isEmpty()) {
|
|
|
|
// allow setting an empty list as a default
|
|
|
|
acc[name] = defaultValue;
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
return acc;
|
2019-06-14 16:46:33 +01:00
|
|
|
}
|
2018-11-02 15:55:08 +01:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
if (defaultValue !== null) {
|
|
|
|
if (!withNameKey) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
acc[name] = defaultValue;
|
2019-06-14 16:46:33 +01:00
|
|
|
}
|
2018-11-02 15:55:08 +01:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{} as DraftEntryData,
|
|
|
|
);
|
2018-11-02 15:55:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-22 16:53:06 +02:00
|
|
|
export function getMediaAssets({ entry }: { entry: EntryMap }) {
|
2020-02-14 22:31:33 +02:00
|
|
|
const filesArray = entry.get('mediaFiles').toArray();
|
2020-03-22 16:53:06 +02:00
|
|
|
const assets = filesArray
|
|
|
|
.filter(file => file.get('draft'))
|
|
|
|
.map(file =>
|
|
|
|
createAssetProxy({ path: file.get('path'), file: file.get('file'), url: file.get('url') }),
|
|
|
|
);
|
2019-12-18 18:16:02 +02:00
|
|
|
|
|
|
|
return assets;
|
2019-11-17 11:51:50 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function persistEntry(collection: Collection) {
|
|
|
|
return async (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2016-06-06 21:53:22 -03:00
|
|
|
const state = getState();
|
2017-01-13 19:30:40 -02:00
|
|
|
const entryDraft = state.entryDraft;
|
2017-10-09 22:50:43 +13:00
|
|
|
const fieldsErrors = entryDraft.get('fieldsErrors');
|
2019-04-10 21:38:53 +01:00
|
|
|
const usedSlugs = selectPublishedSlugs(state, collection.get('name'));
|
2017-01-13 19:30:40 -02:00
|
|
|
|
|
|
|
// Early return if draft contains validation errors
|
2017-10-09 22:50:43 +13:00
|
|
|
if (!fieldsErrors.isEmpty()) {
|
2018-08-07 14:46:54 -06:00
|
|
|
const hasPresenceErrors = fieldsErrors.some(errors =>
|
|
|
|
errors.some(error => error.type && error.type === ValidationErrorTypes.PRESENCE),
|
|
|
|
);
|
2018-01-09 11:32:47 +00:00
|
|
|
|
2017-10-09 22:50:43 +13:00
|
|
|
if (hasPresenceErrors) {
|
2018-08-07 14:46:54 -06:00
|
|
|
dispatch(
|
|
|
|
notifSend({
|
2018-11-02 14:19:49 -03:00
|
|
|
message: {
|
|
|
|
key: 'ui.toast.missingRequiredField',
|
|
|
|
},
|
2018-08-07 14:46:54 -06:00
|
|
|
kind: 'danger',
|
|
|
|
dismissAfter: 8000,
|
|
|
|
}),
|
|
|
|
);
|
2017-10-09 22:50:43 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2017-06-21 13:49:43 -04:00
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
const backend = currentBackend(state.config);
|
2016-10-13 14:30:11 +02:00
|
|
|
const entry = entryDraft.get('entry');
|
2020-03-22 16:53:06 +02:00
|
|
|
const assetProxies = getMediaAssets({
|
2020-01-22 20:42:24 +02:00
|
|
|
entry,
|
2019-12-18 18:16:02 +02:00
|
|
|
});
|
2017-08-02 13:11:43 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the values of any fields with registered serializers, and
|
|
|
|
* update the entry and entryDraft with the serialized values.
|
|
|
|
*/
|
2017-09-26 16:20:03 -04:00
|
|
|
const fields = selectFields(collection, entry.get('slug'));
|
|
|
|
const serializedData = serializeValues(entryDraft.getIn(['entry', 'data']), fields);
|
2017-08-02 13:11:43 -04:00
|
|
|
const serializedEntry = entry.set('data', serializedData);
|
|
|
|
const serializedEntryDraft = entryDraft.set('entry', serializedEntry);
|
|
|
|
dispatch(entryPersisting(collection, serializedEntry));
|
2017-07-21 23:40:33 -07:00
|
|
|
return backend
|
2019-12-18 18:16:02 +02:00
|
|
|
.persistEntry({
|
|
|
|
config: state.config,
|
2019-04-10 21:38:53 +01:00
|
|
|
collection,
|
2019-12-18 18:16:02 +02:00
|
|
|
entryDraft: serializedEntryDraft,
|
|
|
|
assetProxies,
|
2019-04-10 21:38:53 +01:00
|
|
|
usedSlugs,
|
2019-12-18 18:16:02 +02:00
|
|
|
})
|
|
|
|
.then((slug: string) => {
|
2018-08-07 14:46:54 -06:00
|
|
|
dispatch(
|
|
|
|
notifSend({
|
2018-11-02 14:19:49 -03:00
|
|
|
message: {
|
2018-11-18 14:06:41 -08:00
|
|
|
key: 'ui.toast.entrySaved',
|
2018-11-02 14:19:49 -03:00
|
|
|
},
|
2018-08-07 14:46:54 -06:00
|
|
|
kind: 'success',
|
|
|
|
dismissAfter: 4000,
|
|
|
|
}),
|
|
|
|
);
|
2020-01-15 00:15:14 +02:00
|
|
|
// re-load media library if entry had media files
|
|
|
|
if (assetProxies.length > 0) {
|
|
|
|
dispatch(loadMedia());
|
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
dispatch(entryPersisted(collection, serializedEntry, slug));
|
2020-04-09 12:44:06 +01:00
|
|
|
if (serializedEntry.get('newRecord')) return dispatch(loadEntry(collection, slug));
|
2016-10-17 12:35:31 +02:00
|
|
|
})
|
2019-12-18 18:16:02 +02:00
|
|
|
.catch((error: Error) => {
|
2017-07-18 19:14:40 -04:00
|
|
|
console.error(error);
|
2018-08-07 14:46:54 -06:00
|
|
|
dispatch(
|
|
|
|
notifSend({
|
2018-11-02 14:19:49 -03:00
|
|
|
message: {
|
|
|
|
details: error,
|
|
|
|
key: 'ui.toast.onFailToPersist',
|
|
|
|
},
|
2018-08-07 14:46:54 -06:00
|
|
|
kind: 'danger',
|
|
|
|
dismissAfter: 8000,
|
|
|
|
}),
|
|
|
|
);
|
2017-08-29 10:49:21 -06:00
|
|
|
return Promise.reject(dispatch(entryPersistFail(collection, serializedEntry, error)));
|
2016-10-17 12:35:31 +02:00
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
};
|
|
|
|
}
|
2017-07-21 23:40:33 -07:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
export function deleteEntry(collection: Collection, slug: string) {
|
|
|
|
return (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
|
2017-07-21 23:40:33 -07:00
|
|
|
const state = getState();
|
|
|
|
const backend = currentBackend(state.config);
|
|
|
|
|
|
|
|
dispatch(entryDeleting(collection, slug));
|
2018-08-07 14:46:54 -06:00
|
|
|
return backend
|
2020-02-04 13:49:30 +02:00
|
|
|
.deleteEntry(state, collection, slug)
|
2018-08-07 14:46:54 -06:00
|
|
|
.then(() => {
|
|
|
|
return dispatch(entryDeleted(collection, slug));
|
|
|
|
})
|
2019-12-18 18:16:02 +02:00
|
|
|
.catch((error: Error) => {
|
2018-08-07 14:46:54 -06:00
|
|
|
dispatch(
|
|
|
|
notifSend({
|
2018-11-02 14:19:49 -03:00
|
|
|
message: {
|
|
|
|
details: error,
|
|
|
|
key: 'ui.toast.onFailToDelete',
|
|
|
|
},
|
2018-08-07 14:46:54 -06:00
|
|
|
kind: 'danger',
|
|
|
|
dismissAfter: 8000,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
console.error(error);
|
|
|
|
return Promise.reject(dispatch(entryDeleteFail(collection, slug, error)));
|
|
|
|
});
|
2017-07-21 23:40:33 -07:00
|
|
|
};
|
|
|
|
}
|