import { IRestApiResponse } from 'superdesk-api' ; import { ASSET_LIST_STYLE , IAssetItem , IAssetSearchParams , LIST_ACTION } from '../../interfaces' ; import { IThunkAction } from '../types' ; import { ASSET_SET_LIST_STYLE , IAssetActionTypes , MANAGE_ASSET_UPDATE_IN_STORE , MANAGE_ASSETS_CLOSE_CONTENT_PANEL , MANAGE_ASSETS_EDIT , MANAGE_ASSETS_PREVIEW , MANAGE_MULTIACTIONBAR_CLOSE , POP_ASSET_SEARCH_PARAMS , PUSH_ASSET_SEARCH_PARAMS , RECEIVE_ASSETS , SET_ASSET_SEARCH_PARAMS , UPDATE_SELECTED_ASSET_IDS , UPDATE_MULTIPLE_SELECTED_ASSET_IDS , } from './types' ; import { samsApi , superdeskApi } from '../../apis' ; import { getAssetListItemIds , getAssetListStyle , getAssets , getAssetSearchParams , getSelectedAssetId , getSelectedAssetIds , getSelectedAssetItems , } from './selectors' ; import { getDisabledSetIds } from '../sets/selectors' ; import { verifyAssetBeforeLocking } from '../../utils/assets' ; export function receiveAssets ( response , listAction ? ) { return { type : RECEIVE_ASSETS , payload : { response : response , listAction : listAction , } , } ; } export function setAssetSearchParams ( params ) { return { type : SET_ASSET_SEARCH_PARAMS , payload : params , } ; } function _pushAssetSearchParams ( params ) { return { type : PUSH_ASSET_SEARCH_PARAMS , payload : params , } ; } export function pushAssetSearchParams ( params ) { return ( dispatch ) => { dispatch ( _pushAssetSearchParams ( params ) ) ; return dispatch ( queryAssetsFromCurrentSearch ( LIST_ACTION . REPLACE ) ) ; } ; } function _popAssetSearchParams ( ) { return { type : POP_ASSET_SEARCH_PARAMS } ; } export function popAssetSearchParams ( ) { return ( dispatch ) => { dispatch ( _popAssetSearchParams ( ) ) ; return dispatch ( queryAssetsFromCurrentSearch ( LIST_ACTION . REPLACE ) ) ; } ; } export function sessionUnlock ( session_id ) { return ( dispatch , getState ) => { const assetItemIds = getAssetListItemIds ( getState ( ) ) ; const assets = getAssets ( getState ( ) ) ; const assetIds = [ ] ; Object . keys ( assetItemIds ) . forEach ( ( element ) => { if ( assets [ assetItemIds [ element ] ] . lock_session === session_id ) { assetIds . push ( assets [ assetItemIds [ element ] ] . _id ) ; } } ) ; return dispatch < any > ( loadAssetsByIds ( assetIds ) ) ; } ; } export function setAssetListStyle ( style ) { return { type : ASSET_SET_LIST_STYLE , payload : style , } ; } export function toggleAssetListStyle ( ) { return ( dispatch , getState ) => { dispatch ( setAssetListStyle ( getAssetListStyle ( getState ( ) ) === ASSET_LIST_STYLE . GRID ? ASSET_LIST_STYLE . LIST : ASSET_LIST_STYLE . GRID ) ) ; return dispatch < any > ( queryAssetsFromCurrentSearch ( ) ) ; } ; } export function previewAsset ( asset_id ) { return { type : MANAGE_ASSETS_PREVIEW , payload : asset_id , } ; } export function closeAssetContentPanel ( ) { return { type : MANAGE_ASSETS_CLOSE_CONTENT_PANEL , } ; } export function updateSelectedAssetIds ( asset_id ) { return { type : UPDATE_SELECTED_ASSET_IDS , payload : asset_id , } ; } export function updateMultipleSelectedAssetIds ( asset_id ) { return { type : UPDATE_MULTIPLE_SELECTED_ASSET_IDS , payload : asset_id , } ; } export function closeMultiActionBar ( ) { return { type : MANAGE_MULTIACTIONBAR_CLOSE , } ; } export function queryAssets ( params , listAction ? ) { return ( dispatch , getState ) => { return samsApi . assets . query ( params , getAssetListStyle ( getState ( ) ) ) . then ( ( response ) => { dispatch ( receiveAssets ( response , listAction ) ) ; } ) ; } ; } export function queryAssetsFromCurrentSearch ( listAction ? ) { return ( dispatch , getState ) => { const params = getAssetSearchParams ( getState ( ) ) ; return dispatch ( queryAssets ( params , listAction ) ) ; } ; } export function loadNextAssetsPage ( ) { return ( dispatch , getState ) => { const params = getAssetSearchParams ( getState ( ) ) ; params . page += 1 ; return dispatch ( queryAssets ( params , LIST_ACTION . APPEND ) ) ; } ; } export function updateAssetSearchParamsAndListItems ( params , listAction ? ) { return ( dispatch , getState ) => { if ( listAction === LIST_ACTION . REPLACE ) { dispatch ( setAssetSearchParams ( { ... params , page : 1 , } ) ) ; } else { dispatch ( setAssetSearchParams ( params ) ) ; } samsApi . assets . setSearchUrlParams ( getAssetSearchParams ( getState ( ) ) ) ; return dispatch ( queryAssetsFromCurrentSearch ( listAction ) ) ; } ; } export function updateAssetSearchParamsAndListItemsFromURL ( listAction ? ) { return ( dispatch , _getState ) => { const searchParams = samsApi . assets . getSearchUrlParams ( ) ; if ( Object . keys ( searchParams ) . length > 0 ) { dispatch ( setAssetSearchParams ( searchParams ) ) ; } return dispatch ( queryAssetsFromCurrentSearch ( listAction ) ) ; } ; } export function updateAssetInStore ( asset , assetId ) { return { type : MANAGE_ASSET_UPDATE_IN_STORE , payload : { asset : asset , assetId : assetId , } , } ; } export function editAsset ( assetId ? ) { return { type : MANAGE_ASSETS_EDIT , payload : assetId , } ; } export function unlockAsset ( asset ) { return ( dispatch , getState ) => { return samsApi . assets . unlockAsset ( asset , { } ) . then ( ( unlockedAsset ) => { dispatch ( updateAssetInStore ( unlockedAsset , asset . _id ) ) ; const getassets = getAssets ( getState ( ) ) ; return getassets [ asset . _id ] ; } ) ; } ; } export function forceUnlockAsset ( asset ) { return ( dispatch , getState ) => { return samsApi . assets . unlockAsset ( asset , { 'force' : true } ) . then ( ( unlockedAsset ) => { dispatch ( updateAssetInStore ( unlockedAsset , asset . _id ) ) ; const getassets = getAssets ( getState ( ) ) ; return getassets [ asset . _id ] ; } ) ; } ; } export function lockAsset ( asset ) { return ( dispatch , getState ) => { if ( verifyAssetBeforeLocking ( asset , 'edit' ) ) { return Promise . resolve ( ) ; } else { return samsApi . assets . lockAsset ( asset , { 'lock_action' : 'edit' } ) . then ( ( lockedAsset ) => { dispatch ( updateAssetInStore ( lockedAsset , asset . _id ) ) ; const getassets = getAssets ( getState ( ) ) ; return getassets [ asset . _id ] ; } ) ; } } ; } export function onEditAsset ( asset ) { return ( dispatch , getState ) => { const disabledSetIds = getDisabledSetIds ( getState ( ) ) ; if ( disabledSetIds . indexOf ( asset . set_id ) === - 1 ) { dispatch ( lockAsset ( asset ) ) . then ( ( ) => { dispatch ( editAsset ( asset . _id ) ) ; } ) ; } } ; } export function updateAsset ( original , updates ) { return ( dispatch ) => { return samsApi . assets . update ( original , updates ) . then ( ( updatedAsset ) => { dispatch ( updateAssetInStore ( updatedAsset , updatedAsset . _id ) ) ; return updatedAsset ; } ) ; } ; } export function deleteAsset ( asset ) { return ( dispatch , getState ) => { const selectedAssetId = getSelectedAssetId ( getState ( ) ) ; const selectedAssetIds = getSelectedAssetIds ( getState ( ) ) ; return samsApi . assets . deleteAsset ( asset ) . then ( ( ) => { if ( selectedAssetId === asset . _id ) { dispatch ( closeAssetContentPanel ( ) ) ; } if ( selectedAssetIds . indexOf ( asset . _id ) !== - 1 ) { dispatch ( updateSelectedAssetIds ( asset . _id ) ) ; } } ) ; } ; } export function deleteAssets ( asset ? ) { return ( dispatch , getState ) => { if ( asset !== undefined ) { return openDeleteConfirmationModal ( asset . name ) . then ( ( response ) => { if ( response === true ) { dispatch ( deleteAsset ( asset ) ) ; } } ) ; } else { const selectedAssets = getSelectedAssetItems ( getState ( ) ) ; const assetName = ( selectedAssets . length === 1 ) ? selectedAssets [ 0 ] . name : undefined ; return openDeleteConfirmationModal ( assetName , selectedAssets . length ) . then ( ( response ) => { if ( response === true ) { Promise . all ( selectedAssets . map ( ( selectedAsset ) => dispatch ( deleteAsset ( selectedAsset ) ) ) ) . finally ( ( ) => { dispatch ( closeMultiActionBar ( ) ) ; } ) ; } return Promise . resolve ( ) ; } ) ; } } ; } export function loadAssetsByIds ( ids ) { return ( dispatch , getState ) => { const loadedAssetIds = Object . keys ( getAssets ( getState ( ) ) ) ; const attachmentsToLoad = ids . filter ( ( id ) => loadedAssetIds . includes ( id ) ) ; if ( attachmentsToLoad . length === 0 ) { return Promise . resolve ( ) ; } return samsApi . assets . getByIds ( attachmentsToLoad ) . then ( ( response ) => { dispatch ( receiveAssets ( response ) ) ; } ) ; } ; } export function reloadAssetList ( ) { return ( dispatch ) => ( dispatch ( updateAssetSearchParamsAndListItems ( { } , LIST_ACTION . REPLACE ) ) ) ; } function openDeleteConfirmationModal ( assetName ? , asset_length ? ) { 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 ) ; if ( assetName !== undefined ) { return confirm ( gettext ( 'Are you sure you want to delete the asset "{{name}}"?' , { name : assetName } ) , gettext ( 'Delete Asset?' ) ) . then ( ( response ) => { el . remove ( ) ; return response ; } ) ; } else if ( asset_length !== undefined ) { return confirm ( gettext ( 'Are you sure you want to delete these {{length}} assets' , { length : asset_length } ) , gettext ( 'Delete Asset?' ) ) . then ( ( response ) => { el . remove ( ) ; return response ; } ) ; } else { return Promise . reject ( ) ; } }