import { createAction as origCreateAction , ActionMeta } from "redux-actions" ; import * as Etebase from "etebase" ; import { ConnectionInfo , SettingsType } from "./" ; import { Message } from "./reducers" ; type FunctionAny = ( ... args : any [ ] ) => any ; function createAction < Func extends FunctionAny , MetaFunc extends FunctionAny > ( actionType , payloadCreator , metaCreator ? ) { return origCreateAction ( actionType , payloadCreator , metaCreator as any ) as any ; } export const login = createAction ( "LOGIN" , async ( etebase ) => { return etebase . save ( ) ; } ) ; export const setCacheCollection = createAction ( "SET_CACHE_COLLECTION" , async ( colMgr , col ) => { return { cache : colMgr . cacheSave ( col ) , meta : col . getMeta ( ) , collectionType : col . getCollectionType ( ) } ; } , ( _colMgr , col ) => { return { colUid : col . uid , deleted : col . isDeleted } ; } ) ; export const unsetCacheCollection = createAction ( "UNSET_CACHE_COLLECTION" , ( _colMgr , colUid ) => { return colUid ; } , ( _colMgr , colUid ) => { return { colUid , deleted : true } ; } ) ; export const collectionUpload = createAction ( "COLLECTION_UPLOAD" , async ( colMgr , col ) => { await colMgr . upload ( col ) ; return { cache : colMgr . cacheSave ( col ) , meta : col . getMeta ( ) , collectionType : col . getCollectionType ( ) } ; } , ( _colMgr , col ) => { return { colUid : col . uid , deleted : col . isDeleted } ; } ) ; export const setCacheItem = createAction ( "SET_CACHE_ITEM" , async ( _col , itemMgr , item ) => { return { cache : itemMgr . cacheSave ( item ) , meta : item . getMeta ( ) , isDeleted : item . isDeleted } ; } , ( col , _itemMgr , item ) => { return { colUid : col . uid , itemUid : item . uid } ; } ) ; export const setCacheItemMulti = createAction ( "SET_CACHE_ITEM_MULTI" , async ( _colUid , itemMgr , items ) => { const ret = [ ] ; for ( const item of items ) { ret . push ( { cache : itemMgr . cacheSave ( item ) , meta : item . getMeta ( ) , isDeleted : item . isDeleted } ) ; } return ret ; } , ( colUid , _itemMgr , items , _deps ? ) => { return { colUid , items : items } ; } ) ; export const itemBatch = createAction ( "ITEM_BATCH" , async ( _col , itemMgr , items , deps ? ) => { await itemMgr . batch ( items , deps ) ; const ret = [ ] ; for ( const item of items ) { ret . push ( { cache : itemMgr . cacheSave ( item ) , meta : item . getMeta ( ) , isDeleted : item . isDeleted } ) ; } return ret ; } , ( col , _itemMgr , items , _deps ? ) => { return { colUid : col . uid , items : items } ; } ) ; export const setSyncCollection = createAction ( "SET_SYNC_COLLECTION" , ( uid , stoken ) => { return { uid , stoken } ; } ) ; export const setSyncItem = createAction ( "SET_SYNC_ITEM" , ( colUid , itemUid ) => { return { colUid , itemUid } ; } ) ; export const unsetSyncItem = createAction ( "UNSET_SYNC_ITEM" , ( colUid , itemUid ) => { return { colUid , itemUid } ; } ) ; export const setSyncGeneral = createAction ( "SET_SYNC_GENERAL" , ( stoken ) => { return stoken ; } ) ; export const logout = createAction ( "LOGOUT" , ( creds ) => { ( async ( ) => { const etebase = creds ; etebase . logout ( ) ; } ) ( ) ; return true ; } ) ; export const performSync = createAction ( "PERFORM_SYNC" , ( syncPromise ) => { return syncPromise ; } ) ; export const setConnectionInfo = createAction ( "SET_CONNECTION_INFO" , ( connectionInfo ) => { return { ... connectionInfo } ; } ) ; export const setSyncStatus = createAction ( "SET_SYNC_STATUS" , ( status ) => { return status ; } ) ; export const addError = createAction ( "ADD_ERROR" , ( e ) => { return e ; } ) ; export const popError = createAction ( "POP_ERROR" , ( ) => { return null ; } ) ; export const clearErrors = createAction ( "CLEAR_ERRORS" , ( _etebase ) => { return true ; } ) ; export const pushMessage = createAction ( "PUSH_MESSAGE" , ( message ) => { return message ; } ) ; export const popMessage = createAction ( "POP_MESSAGE" , ( ) => { return true ; } ) ; export const setSettings = createAction ( "SET_SETTINGS" , ( settings ) => { return { ... settings } ; } ) ;