@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
149 lines (147 loc) • 5.8 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ofType } from 'redux-observable';
import { map, switchMap, withLatestFrom } from 'rxjs/operators';
import { fetchItemHistory as getContentHistory, fetchVersions, revertTo } from '../../services/content';
import { catchAjaxError } from '../../utils/ajax';
import {
compareBothVersions,
compareBothVersionsComplete,
compareBothVersionsFailed,
compareToPreviousVersion,
fetchItemVersions,
fetchItemVersionsComplete,
fetchItemVersionsFailed,
resetVersionsState,
revertContent,
revertContentComplete,
revertContentFailed,
revertToPreviousVersion,
versionsChangeItem
} from '../actions/versions';
import { NEVER, of } from 'rxjs';
import { historyDialogClosed } from '../actions/dialogs';
import { fetchHistory as getConfigurationHistory } from '../../services/configuration';
import { reloadDetailedItem } from '../actions/content';
import { emitSystemEvent, itemReverted, showRevertItemSuccessNotification } from '../actions/system';
import { batchActions } from '../actions/misc';
import { getHostToGuestBus } from '../../utils/subjects';
import { reloadRequest } from '../actions/preview';
export default [
(action$, state$) =>
action$.pipe(
ofType(fetchItemVersions.type, versionsChangeItem.type),
withLatestFrom(state$),
switchMap(([{ payload }, state]) => {
var _a, _b, _c, _d;
const service = state.versions.isConfig
? getConfigurationHistory(
state.sites.active,
(_a = payload === null || payload === void 0 ? void 0 : payload.path) !== null && _a !== void 0
? _a
: state.versions.item.path,
(_b = payload === null || payload === void 0 ? void 0 : payload.environment) !== null && _b !== void 0
? _b
: state.versions.environment,
(_c = payload === null || payload === void 0 ? void 0 : payload.module) !== null && _c !== void 0
? _c
: state.versions.module
)
: getContentHistory(
state.sites.active,
(_d = payload === null || payload === void 0 ? void 0 : payload.path) !== null && _d !== void 0
? _d
: state.versions.item.path
);
return service.pipe(map(fetchItemVersionsComplete), catchAjaxError(fetchItemVersionsFailed));
})
),
(action$, state$) =>
action$.pipe(
ofType(compareBothVersions.type, compareToPreviousVersion.type),
withLatestFrom(state$),
switchMap(([{ payload }, state]) =>
fetchVersions(
state.sites.active,
state.versions.item.path,
[state.versions.selected[0], state.versions.selected[1]],
state.contentTypes.byId
).pipe(map(compareBothVersionsComplete), catchAjaxError(compareBothVersionsFailed))
)
),
(action$, state$) =>
action$.pipe(
ofType(revertContent.type, revertToPreviousVersion.type),
withLatestFrom(state$),
switchMap(([{ payload }, state]) => {
var _a, _b;
return revertTo(
state.sites.active,
(_a = payload.path) !== null && _a !== void 0 ? _a : state.versions.item.path,
(_b = payload.versionNumber) !== null && _b !== void 0 ? _b : state.versions.previous
).pipe(
map(() => revertContentComplete({ path: payload.path })),
catchAjaxError(revertContentFailed)
);
})
),
(action$, state$) =>
action$.pipe(
ofType(revertContentComplete.type),
withLatestFrom(state$),
switchMap(([{ payload }, state]) => {
var _a;
if (payload.path === ((_a = state.preview.guest) === null || _a === void 0 ? void 0 : _a.path)) {
getHostToGuestBus().next({ type: reloadRequest.type });
}
return of(
batchActions([
emitSystemEvent(itemReverted({ target: payload.path })),
fetchItemVersions(),
showRevertItemSuccessNotification(),
reloadDetailedItem({ path: payload.path })
])
);
})
),
(action$, state$) =>
action$.pipe(
ofType(historyDialogClosed.type),
withLatestFrom(state$),
switchMap(([, { dialogs }]) => {
if (!dialogs.viewVersion.open && !dialogs.compareVersions.open) {
return of(resetVersionsState());
} else {
return NEVER;
}
})
)
];