// Types import {ISetItem} from '../../interfaces'; import {IThunkAction} from '../types'; import {superdeskApi, samsApi} from '../../apis'; // Redux Selectors import {getSelectedSetId} from './selectors'; import { ISetActionTypes, RECEIVE_SETS, REMOVE_SET_IN_STORE, MANAGE_SETS_EDIT, MANAGE_SETS_PREVIEW, MANAGE_SETS_CLOSE_CONTENT_PANEL, MANAGE_SETS_RESET, RECEIVE_ASSETS_COUNT, } from './types'; export function receiveSets(sets: Array): ISetActionTypes { return { type: RECEIVE_SETS, payload: sets, }; } export function removeSetInStore(set: ISetItem): ISetActionTypes { return { type: REMOVE_SET_IN_STORE, payload: set, }; } export function editSet(setId?: string): ISetActionTypes { return { type: MANAGE_SETS_EDIT, payload: setId, }; } export function previewSet(setId: string): ISetActionTypes { return { type: MANAGE_SETS_PREVIEW, payload: setId, }; } export function closeSetContentPanel(): ISetActionTypes { return { type: MANAGE_SETS_CLOSE_CONTENT_PANEL, }; } export function onManageSetsModalClosed(): ISetActionTypes { return { type: MANAGE_SETS_RESET, }; } export function receiveAssetsCount(counts: Dictionary): ISetActionTypes { return { type: RECEIVE_ASSETS_COUNT, payload: counts, }; } export function loadSets(): IThunkAction> { return (dispatch) => { return samsApi.sets.getAll() .then((sets: Array) => { const setIds: Array = sets.map((set) => { return set._id; }); dispatch(receiveSets(sets)); // Only load Set's Asset count if there are Sets configured return !sets.length ? sets : dispatch(loadAssetsCount(setIds)) .then(() => sets); }); }; } export function loadAssetsCount(setIds: Array): IThunkAction> { return (dispatch) => { return samsApi.assets.getCount(setIds) .then((counts: Dictionary) => { dispatch(receiveAssetsCount(counts)); return Promise.resolve(counts); }) .catch(() => ({})); }; } function openDeleteConfirmationModal(set: ISetItem): Promise { const {gettext} = superdeskApi.localization; const {confirm} = superdeskApi.ui; const el = document.createElement('div'); // FIXME: Add an extra backdrop that will cover the Manage Sets modal // This is required because the ui-framework calculates z-index // based on the number of active modals, where as we're using // a mixture of the ui-framework and pure React modals // (superdeskApi.ui.showModal vs superdeskApi.ui.confirm) el.classList.add('modal__backdrop', 'fade', 'in'); el.style.zIndex = '1050'; document.body.append(el); return confirm( gettext('Are you sure you want to delete the Set "{{name}}"?', {name: set.name}), gettext('Delete Set?'), ) .then((response: boolean) => { el.remove(); return response; }); } export function confirmBeforeDeletingSet(set: ISetItem): IThunkAction { return (dispatch, getState) => { return openDeleteConfirmationModal(set) .then((response: boolean) => { if (response === true) { return samsApi.sets.delete(set) .then(() => { if (getSelectedSetId(getState()) === set._id) { dispatch(closeSetContentPanel()); } }); } return Promise.resolve(); }); }; }