@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
135 lines (133 loc) • 5.65 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 { createReducer } from '@reduxjs/toolkit';
import { createLookupTable } from '../../utils/object';
import {
compareBothVersions,
compareBothVersionsComplete,
compareBothVersionsFailed,
compareToPreviousVersion,
compareVersion,
fetchItemVersions,
fetchItemVersionsComplete,
fetchItemVersionsFailed,
resetVersionsState,
revertContent,
revertContentComplete,
revertContentFailed,
revertToPreviousVersion,
versionsChangeItem,
versionsChangeLimit,
versionsChangePage
} from '../actions/versions';
const initialState = {
byId: null,
item: null,
rootPath: '/site/website',
error: null,
isFetching: null,
current: null,
versions: null,
allVersions: null,
count: 0,
page: 0,
limit: 10,
selected: [],
previous: null,
compareVersionsBranch: {
compareVersions: null,
isFetching: null,
error: null
}
};
const reducer = createReducer(initialState, {
[fetchItemVersions.type]: (state, { payload }) =>
Object.assign(Object.assign(Object.assign({}, state), payload), { isFetching: true }),
[fetchItemVersionsComplete.type]: (state, { payload: { versions } }) =>
Object.assign(Object.assign({}, state), {
byId: createLookupTable(versions, 'versionNumber'),
count: versions.length,
current: versions.length ? versions[0].versionNumber : null,
allVersions: versions,
versions: versions.slice(state.page * state.limit, (state.page + 1) * state.limit),
isFetching: false,
error: null
}),
[fetchItemVersionsFailed.type]: (state, { payload }) =>
Object.assign(Object.assign({}, state), { error: payload.response, isFetching: false }),
[versionsChangePage.type]: (state, { payload }) =>
Object.assign(Object.assign({}, state), {
page: payload.page,
versions: state.allVersions.slice(payload.page * state.limit, (payload.page + 1) * state.limit)
}),
[versionsChangeLimit.type]: (state, { payload: { limit = 10 } }) =>
Object.assign(Object.assign({}, state), { limit, page: 0, versions: state.allVersions.slice(0, limit) }),
[versionsChangeItem.type]: (state, { payload }) => Object.assign(Object.assign({}, state), { item: payload.item }),
[compareVersion.type]: (state, { payload }) =>
Object.assign(Object.assign({}, state), { selected: payload ? [payload.id] : [] }),
[compareToPreviousVersion.type]: (state, { payload }) => {
var _a;
let i = state.allVersions.findIndex((version) => version.versionNumber === payload.id);
let previous = (_a = state.allVersions) === null || _a === void 0 ? void 0 : _a[i + 1].versionNumber;
return Object.assign(Object.assign({}, state), { selected: [payload.id, previous] });
},
[compareBothVersions.type]: (state, { payload }) =>
Object.assign(Object.assign({}, state), {
selected: payload.versions,
compareVersionsBranch: Object.assign(Object.assign({}, state.compareVersionsBranch), { isFetching: true })
}),
[compareBothVersionsComplete.type]: (state, { payload }) =>
Object.assign(Object.assign({}, state), {
compareVersionsBranch: Object.assign(Object.assign({}, state.compareVersionsBranch), {
compareVersions: payload,
isFetching: false
})
}),
[compareBothVersionsFailed.type]: (state, { payload }) =>
Object.assign(Object.assign({}, state), {
compareVersionsBranch: Object.assign(Object.assign({}, state.compareVersionsBranch), {
error: payload,
isFetching: false
})
}),
[revertToPreviousVersion.type]: (state, { payload }) => {
var _a;
let i = state.allVersions.findIndex((version) => version.versionNumber === payload.id);
let previous = (_a = state.allVersions) === null || _a === void 0 ? void 0 : _a[i + 1].versionNumber;
return Object.assign(Object.assign({}, state), { previous: previous, isFetching: true });
},
[revertContent.type]: (state) => Object.assign(Object.assign({}, state), { isFetching: true }),
[revertContentComplete.type]: (state) => Object.assign(Object.assign({}, state), { isFetching: false }),
[revertContentFailed.type]: (state, { payload }) =>
Object.assign(Object.assign({}, state), { error: payload.response, isFetching: false }),
[resetVersionsState.type]: () => initialState
});
export default reducer;