import { ISetItem } from '../../interfaces' ; import { IThunkAction } from '../types' ; import { superdeskApi , samsApi } from '../../apis' ; 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 ) { return { type : RECEIVE_SETS , payload : sets , } ; } export function removeSetInStore ( set ) { return { type : REMOVE_SET_IN_STORE , payload : set , } ; } export function editSet ( setId ? ) { return { type : MANAGE_SETS_EDIT , payload : setId , } ; } export function previewSet ( setId ) { return { type : MANAGE_SETS_PREVIEW , payload : setId , } ; } export function closeSetContentPanel ( ) { return { type : MANAGE_SETS_CLOSE_CONTENT_PANEL , } ; } export function onManageSetsModalClosed ( ) { return { type : MANAGE_SETS_RESET , } ; } export function receiveAssetsCount ( counts ) { return { type : RECEIVE_ASSETS_COUNT , payload : counts , } ; } export function loadSets ( ) { return ( dispatch ) => { return samsApi . sets . getAll ( ) . then ( ( sets ) => { const setIds = sets . map ( ( set ) => { return set . _id ; } ) ; dispatch ( receiveSets ( sets ) ) ; return ! sets . length ? sets : dispatch ( loadAssetsCount ( setIds ) ) . then ( ( ) => sets ) ; } ) ; } ; } export function loadAssetsCount ( setIds ) { return ( dispatch ) => { return samsApi . assets . getCount ( setIds ) . then ( ( counts ) => { dispatch ( receiveAssetsCount ( counts ) ) ; return Promise . resolve ( counts ) ; } ) . catch ( ( ) => ( { } ) ) ; } ; } function openDeleteConfirmationModal ( set ) { const { gettext } = superdeskApi . localization ; const { confirm } = superdeskApi . ui ; const el = document . createElement ( 'div' ) ; 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 ) => { el . remove ( ) ; return response ; } ) ; } export function confirmBeforeDeletingSet ( set ) { return ( dispatch , getState ) => { return openDeleteConfirmationModal ( set ) . then ( ( response ) => { if ( response === true ) { return samsApi . sets . delete ( set ) . then ( ( ) => { if ( getSelectedSetId ( getState ( ) ) === set . _id ) { dispatch ( closeSetContentPanel ( ) ) ; } } ) ; } return Promise . resolve ( ) ; } ) ; } ; }