chore: remove redux-optimist

This commit is contained in:
Kancer (Nilay) Gökırmak 2020-10-21 20:30:55 +02:00 committed by GitHub
parent a90795ed9a
commit 950f5c59ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 125 deletions

View File

@ -62,7 +62,6 @@
"react-window": "^1.8.5",
"redux": "^4.0.5",
"redux-notifications": "^4.0.1",
"redux-optimist": "^1.0.0",
"redux-thunk": "^2.3.0",
"sanitize-filename": "^1.6.1",
"semaphore": "^1.0.5",

View File

@ -1,4 +1,3 @@
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';
import * as actions from '../editorialWorkflow';
import { addAssets } from '../media';
import configureMockStore from 'redux-mock-store';
@ -126,7 +125,6 @@ describe('editorialWorkflow actions', () => {
collection: 'posts',
slug,
},
optimist: { type: BEGIN, id: '000000000000000000000' },
});
expect(actions[1]).toEqual({
type: 'MEDIA_LOAD_REQUEST',
@ -146,7 +144,6 @@ describe('editorialWorkflow actions', () => {
collection: 'posts',
slug,
},
optimist: { type: COMMIT, id: '000000000000000000000' },
});
expect(actions[4]).toEqual({
@ -206,7 +203,6 @@ describe('editorialWorkflow actions', () => {
collection: 'posts',
slug,
},
optimist: { type: BEGIN, id: '000000000000000000000' },
});
expect(actions[1]).toEqual({
type: 'NOTIF_SEND',
@ -220,7 +216,6 @@ describe('editorialWorkflow actions', () => {
collection: 'posts',
slug,
},
optimist: { type: REVERT, id: '000000000000000000000' },
});
});
});

View File

@ -1,7 +1,5 @@
import uuid from 'uuid/v4';
import { get } from 'lodash';
import { actions as notifActions } from 'redux-notifications';
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';
import { ThunkDispatch } from 'redux-thunk';
import { Map, List } from 'immutable';
import { currentBackend, slugFromCustomPath } from '../backend';
@ -121,138 +119,109 @@ function unpublishedEntriesFailed(error: Error) {
};
}
function unpublishedEntryPersisting(
collection: Collection,
entry: EntryMap,
transactionID: string,
) {
function unpublishedEntryPersisting(collection: Collection, slug: string) {
return {
type: UNPUBLISHED_ENTRY_PERSIST_REQUEST,
payload: {
collection: collection.get('name'),
entry,
slug,
},
optimist: { type: BEGIN, id: transactionID },
};
}
function unpublishedEntryPersisted(collection: Collection, transactionID: string, slug: string) {
function unpublishedEntryPersisted(collection: Collection, entry: EntryMap) {
return {
type: UNPUBLISHED_ENTRY_PERSIST_SUCCESS,
payload: {
collection: collection.get('name'),
slug,
entry,
},
optimist: { type: COMMIT, id: transactionID },
};
}
function unpublishedEntryPersistedFail(error: Error, transactionID: string) {
function unpublishedEntryPersistedFail(error: Error, collection: Collection, slug: string) {
return {
type: UNPUBLISHED_ENTRY_PERSIST_FAILURE,
payload: { error },
optimist: { type: REVERT, id: transactionID },
payload: {
error,
collection: collection.get('name'),
slug,
},
error,
};
}
function unpublishedEntryStatusChangeRequest(
collection: string,
slug: string,
oldStatus: Status,
newStatus: Status,
transactionID: string,
) {
function unpublishedEntryStatusChangeRequest(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST,
payload: {
collection,
slug,
oldStatus,
newStatus,
},
optimist: { type: BEGIN, id: transactionID },
};
}
function unpublishedEntryStatusChangePersisted(
collection: string,
slug: string,
oldStatus: Status,
newStatus: Status,
transactionID: string,
) {
return {
type: UNPUBLISHED_ENTRY_STATUS_CHANGE_SUCCESS,
payload: {
collection,
slug,
oldStatus,
newStatus,
},
optimist: { type: COMMIT, id: transactionID },
};
}
function unpublishedEntryStatusChangeError(
collection: string,
slug: string,
transactionID: string,
) {
function unpublishedEntryStatusChangeError(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_STATUS_CHANGE_FAILURE,
payload: { collection, slug },
optimist: { type: REVERT, id: transactionID },
};
}
function unpublishedEntryPublishRequest(collection: string, slug: string, transactionID: string) {
function unpublishedEntryPublishRequest(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_PUBLISH_REQUEST,
payload: { collection, slug },
optimist: { type: BEGIN, id: transactionID },
};
}
function unpublishedEntryPublished(collection: string, slug: string, transactionID: string) {
function unpublishedEntryPublished(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_PUBLISH_SUCCESS,
payload: { collection, slug },
optimist: { type: COMMIT, id: transactionID },
};
}
function unpublishedEntryPublishError(collection: string, slug: string, transactionID: string) {
function unpublishedEntryPublishError(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_PUBLISH_FAILURE,
payload: { collection, slug },
optimist: { type: REVERT, id: transactionID },
};
}
function unpublishedEntryDeleteRequest(collection: string, slug: string, transactionID: string) {
// The reducer doesn't handle this action -- it is for `optimist`.
function unpublishedEntryDeleteRequest(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_DELETE_REQUEST,
payload: { collection, slug },
optimist: { type: BEGIN, id: transactionID },
};
}
function unpublishedEntryDeleted(collection: string, slug: string, transactionID: string) {
function unpublishedEntryDeleted(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_DELETE_SUCCESS,
payload: { collection, slug },
optimist: { type: COMMIT, id: transactionID },
};
}
function unpublishedEntryDeleteError(collection: string, slug: string, transactionID: string) {
// The reducer doesn't handle this action -- it is for `optimist`.
function unpublishedEntryDeleteError(collection: string, slug: string) {
return {
type: UNPUBLISHED_ENTRY_DELETE_FAILURE,
payload: { collection, slug },
optimist: { type: REVERT, id: transactionID },
};
}
@ -374,7 +343,6 @@ export function persistUnpublishedEntry(collection: Collection, existingUnpublis
}
const backend = currentBackend(state.config);
const transactionID = uuid();
const entry = entryDraft.get('entry');
const assetProxies = getMediaAssets({
entry,
@ -383,7 +351,7 @@ export function persistUnpublishedEntry(collection: Collection, existingUnpublis
const serializedEntry = getSerializedEntry(collection, entry);
const serializedEntryDraft = entryDraft.set('entry', serializedEntry);
dispatch(unpublishedEntryPersisting(collection, serializedEntry, transactionID));
dispatch(unpublishedEntryPersisting(collection, entry.get('slug')));
const persistAction = existingUnpublishedEntry
? backend.persistUnpublishedEntry
: backend.persistEntry;
@ -405,7 +373,8 @@ export function persistUnpublishedEntry(collection: Collection, existingUnpublis
dismissAfter: 4000,
}),
);
dispatch(unpublishedEntryPersisted(collection, transactionID, newSlug));
dispatch(unpublishedEntryPersisted(collection, serializedEntry));
if (entry.get('slug') !== newSlug) {
dispatch(loadUnpublishedEntry(collection, newSlug));
navigateToEntry(collection.get('name'), newSlug);
@ -421,7 +390,9 @@ export function persistUnpublishedEntry(collection: Collection, existingUnpublis
dismissAfter: 8000,
}),
);
return Promise.reject(dispatch(unpublishedEntryPersistedFail(error, transactionID)));
return Promise.reject(
dispatch(unpublishedEntryPersistedFail(error, collection, entry.get('slug'))),
);
}
};
}
@ -436,10 +407,7 @@ export function updateUnpublishedEntryStatus(
if (oldStatus === newStatus) return;
const state = getState();
const backend = currentBackend(state.config);
const transactionID = uuid();
dispatch(
unpublishedEntryStatusChangeRequest(collection, slug, oldStatus, newStatus, transactionID),
);
dispatch(unpublishedEntryStatusChangeRequest(collection, slug));
backend
.updateUnpublishedEntryStatus(collection, slug, newStatus)
.then(() => {
@ -452,15 +420,7 @@ export function updateUnpublishedEntryStatus(
dismissAfter: 4000,
}),
);
dispatch(
unpublishedEntryStatusChangePersisted(
collection,
slug,
oldStatus,
newStatus,
transactionID,
),
);
dispatch(unpublishedEntryStatusChangePersisted(collection, slug, newStatus));
})
.catch((error: Error) => {
dispatch(
@ -473,7 +433,7 @@ export function updateUnpublishedEntryStatus(
dismissAfter: 8000,
}),
);
dispatch(unpublishedEntryStatusChangeError(collection, slug, transactionID));
dispatch(unpublishedEntryStatusChangeError(collection, slug));
});
};
}
@ -482,8 +442,7 @@ export function deleteUnpublishedEntry(collection: string, slug: string) {
return (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
const state = getState();
const backend = currentBackend(state.config);
const transactionID = uuid();
dispatch(unpublishedEntryDeleteRequest(collection, slug, transactionID));
dispatch(unpublishedEntryDeleteRequest(collection, slug));
return backend
.deleteUnpublishedEntry(collection, slug)
.then(() => {
@ -494,7 +453,7 @@ export function deleteUnpublishedEntry(collection: string, slug: string) {
dismissAfter: 4000,
}),
);
dispatch(unpublishedEntryDeleted(collection, slug, transactionID));
dispatch(unpublishedEntryDeleted(collection, slug));
})
.catch((error: Error) => {
dispatch(
@ -504,7 +463,7 @@ export function deleteUnpublishedEntry(collection: string, slug: string) {
dismissAfter: 8000,
}),
);
dispatch(unpublishedEntryDeleteError(collection, slug, transactionID));
dispatch(unpublishedEntryDeleteError(collection, slug));
});
};
}
@ -514,9 +473,8 @@ export function publishUnpublishedEntry(collectionName: string, slug: string) {
const state = getState();
const collections = state.collections;
const backend = currentBackend(state.config);
const transactionID = uuid();
const entry = selectUnpublishedEntry(state, collectionName, slug);
dispatch(unpublishedEntryPublishRequest(collectionName, slug, transactionID));
dispatch(unpublishedEntryPublishRequest(collectionName, slug));
try {
await backend.publishUnpublishedEntry(entry);
// re-load media after entry was published
@ -528,7 +486,7 @@ export function publishUnpublishedEntry(collectionName: string, slug: string) {
dismissAfter: 4000,
}),
);
dispatch(unpublishedEntryPublished(collectionName, slug, transactionID));
dispatch(unpublishedEntryPublished(collectionName, slug));
const collection = collections.get(collectionName);
if (collection.has('nested')) {
dispatch(loadEntries(collection));
@ -548,7 +506,7 @@ export function publishUnpublishedEntry(collectionName: string, slug: string) {
dismissAfter: 8000,
}),
);
dispatch(unpublishedEntryPublishError(collectionName, slug, transactionID));
dispatch(unpublishedEntryPublishError(collectionName, slug));
}
};
}
@ -557,10 +515,9 @@ export function unpublishPublishedEntry(collection: Collection, slug: string) {
return (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
const state = getState();
const backend = currentBackend(state.config);
const transactionID = uuid();
const entry = selectEntry(state, collection.get('name'), slug);
const entryDraft = (Map().set('entry', entry) as unknown) as EntryDraft;
dispatch(unpublishedEntryPersisting(collection, entry, transactionID));
dispatch(unpublishedEntryPersisting(collection, slug));
return backend
.deleteEntry(state, collection, slug)
.then(() =>
@ -574,7 +531,7 @@ export function unpublishPublishedEntry(collection: Collection, slug: string) {
}),
)
.then(() => {
dispatch(unpublishedEntryPersisted(collection, transactionID, slug));
dispatch(unpublishedEntryPersisted(collection, entry));
dispatch(entryDeleted(collection, slug));
dispatch(loadUnpublishedEntry(collection, slug));
dispatch(
@ -593,7 +550,7 @@ export function unpublishPublishedEntry(collection: Collection, slug: string) {
dismissAfter: 8000,
}),
);
dispatch(unpublishedEntryPersistedFail(error, transactionID));
dispatch(unpublishedEntryPersistedFail(error, collection, entry.get('slug')));
});
};
}

View File

@ -7,7 +7,7 @@ import { getMediaLibrary } from './lib/registry';
import store from './redux';
import { configFailed } from './actions/config';
import { createMediaLibrary, insertMedia } from './actions/mediaLibrary';
import { MediaLibraryInstance } from './types/redux';
import { MediaLibraryInstance, State } from './types/redux';
type MediaLibraryOptions = {};
@ -26,6 +26,8 @@ const initializeMediaLibrary = once(async function initializeMediaLibrary(name,
);
store.dispatch(configFailed(err));
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const handleInsert = (url: string) => store.dispatch(insertMedia(url, undefined));
const instance = await lib.init({ options, handleInsert });
store.dispatch(createMediaLibrary(instance));
@ -33,7 +35,7 @@ const initializeMediaLibrary = once(async function initializeMediaLibrary(name,
});
store.subscribe(() => {
const state = store.getState();
const state = store.getState() as State;
const mediaLibraryName = state.config.getIn(['media_library', 'name']);
if (mediaLibraryName && !state.mediaLibrary.get('externalLibrary')) {
const mediaLibraryConfig = state.config.get('media_library').toJS();

View File

@ -1,17 +1,14 @@
import { combineReducers } from 'redux';
import { connectRouter } from 'connected-react-router';
import { reducer as notifReducer } from 'redux-notifications';
import optimist from 'redux-optimist';
import reducers from './index';
const createRootReducer = history => {
return optimist(
combineReducers({
...reducers,
notifs: notifReducer,
router: connectRouter(history),
}),
);
return combineReducers({
...reducers,
notifs: notifReducer,
router: connectRouter(history),
});
};
export default createRootReducer;

View File

@ -9,6 +9,7 @@ import {
UNPUBLISHED_ENTRIES_SUCCESS,
UNPUBLISHED_ENTRY_PERSIST_REQUEST,
UNPUBLISHED_ENTRY_PERSIST_SUCCESS,
UNPUBLISHED_ENTRY_PERSIST_FAILURE,
UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST,
UNPUBLISHED_ENTRY_STATUS_CHANGE_SUCCESS,
UNPUBLISHED_ENTRY_STATUS_CHANGE_FAILURE,
@ -66,36 +67,43 @@ const unpublishedEntries = (state = Map(), action: EditorialWorkflowAction) => {
});
case UNPUBLISHED_ENTRY_PERSIST_REQUEST: {
return state.setIn(
['entities', `${action.payload!.collection}.${action.payload!.slug}`, 'isPersisting'],
true,
);
}
case UNPUBLISHED_ENTRY_PERSIST_SUCCESS:
// Update Optimistically
return state.withMutations(map => {
map.setIn(
['entities', `${action.payload!.collection}.${action.payload!.entry.get('slug')}`],
fromJS(action.payload!.entry),
);
map.setIn(
[
'entities',
`${action.payload!.collection}.${action.payload!.entry.get('slug')}`,
'isPersisting',
],
true,
);
map.deleteIn([
'entities',
`${action.payload!.collection}.${action.payload!.entry.get('slug')}`,
'isPersisting',
]);
map.updateIn(['pages', 'ids'], List(), list =>
list.push(action.payload!.entry.get('slug')),
);
});
}
case UNPUBLISHED_ENTRY_PERSIST_SUCCESS:
// Update Optimistically
return state.deleteIn([
'entities',
`${action.payload!.collection}.${action.payload!.slug}`,
'isPersisting',
]);
case UNPUBLISHED_ENTRY_PERSIST_FAILURE:
return state.setIn(
['entities', `${action.payload!.collection}.${action.payload!.slug}`, 'isPersisting'],
false,
);
case UNPUBLISHED_ENTRY_STATUS_CHANGE_REQUEST:
// Update Optimistically
return state.setIn(
['entities', `${action.payload!.collection}.${action.payload!.slug}`, 'isUpdatingStatus'],
true,
);
case UNPUBLISHED_ENTRY_STATUS_CHANGE_SUCCESS:
return state.withMutations(map => {
map.setIn(
['entities', `${action.payload!.collection}.${action.payload!.slug}`, 'status'],
@ -103,11 +111,10 @@ const unpublishedEntries = (state = Map(), action: EditorialWorkflowAction) => {
);
map.setIn(
['entities', `${action.payload!.collection}.${action.payload!.slug}`, 'isUpdatingStatus'],
true,
false,
);
});
case UNPUBLISHED_ENTRY_STATUS_CHANGE_SUCCESS:
case UNPUBLISHED_ENTRY_STATUS_CHANGE_FAILURE:
return state.setIn(
['entities', `${action.payload!.collection}.${action.payload!.slug}`, 'isUpdatingStatus'],
@ -122,9 +129,7 @@ const unpublishedEntries = (state = Map(), action: EditorialWorkflowAction) => {
case UNPUBLISHED_ENTRY_PUBLISH_SUCCESS:
case UNPUBLISHED_ENTRY_PUBLISH_FAILURE:
return state.withMutations(map => {
map.deleteIn(['entities', `${action.payload!.collection}.${action.payload!.slug}`]);
});
return state.deleteIn(['entities', `${action.payload!.collection}.${action.payload!.slug}`]);
case UNPUBLISHED_ENTRY_DELETE_SUCCESS:
return state.deleteIn(['entities', `${action.payload!.collection}.${action.payload!.slug}`]);

View File

@ -5,6 +5,7 @@ import { waitUntilAction } from './middleware/waitUntilAction';
import createRootReducer from '../reducers/combinedReducer';
import history from '../routing/history';
import { State } from '../types/redux';
import { Reducer } from 'react';
declare global {
interface Window {
@ -12,9 +13,8 @@ declare global {
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const store = createStore<State, any, {}, {}>(
createRootReducer(history),
const store = createStore<State | undefined, AnyAction, unknown, unknown>(
(createRootReducer(history) as unknown) as Reducer<State | undefined, AnyAction>,
compose(
applyMiddleware(
routerMiddleware(history),

View File

@ -1 +0,0 @@
declare module 'redux-optimist';

View File

@ -14964,11 +14964,6 @@ redux-notifications@^4.0.1:
react-redux "^4.0.0"
react-transition-group "^1.1.3"
redux-optimist@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/redux-optimist/-/redux-optimist-1.0.0.tgz#1f3d4ffbcd11573159bb90e96c68e35e3b875818"
integrity sha512-AG1v8o6UZcGXTEH2jVcWG6KD+gEix+Cj9JXAAzln9MPkauSVd98H7N7EOOyT/v4c9N1mJB4sm1zfspGlLDkUEw==
redux-thunk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"