import * as actions from "@nteract/actions" ; import { toJS } from "@nteract/commutable" ; import { NotebookV4 } from "@nteract/commutable/lib/v4" ; import * as selectors from "@nteract/selectors" ; import { AppState , DirectoryContentRecordProps , DummyContentRecordProps , FileContentRecordProps , IContentProvider , NotebookContentRecordProps , ServerConfig } from "@nteract/types" ; import { RecordOf } from "immutable" ; import { ofType , StateObservable } from "redux-observable" ; import { bookstore } from "rx-jupyter" ; import { EMPTY , Observable , of } from "rxjs" ; import { AjaxResponse } from "rxjs/ajax" ; import { catchError , map , switchMap , tap } from "rxjs/operators" ; param action$ param state$ export function publishToBookstore ( action$ , state$ , dependencies ) { return action$ . pipe ( ofType ( actions . PUBLISH_TO_BOOKSTORE ) , switchMap ( action => { const state = state$ . value ; const host = selectors . currentHost ( state ) ; const serverConfig = selectors . serverConfig ( host ) ; if ( ! action . payload ) { return of ( { type : "ERROR" , error : true , payload : { error : new Error ( "saving content to Bookstore needs a payload" ) } } ) as any ; } if ( host . type !== "jupyter" ) { return EMPTY ; } const content = selectors . contentByRef ( state ) . get ( action . payload . contentRef ) ; if ( ! content || content . type !== "notebook" ) { return of ( { type : "ERROR" , error : true , payload : { error : new Error ( "Only Notebooks can be published to Bookstore" ) } } ) as any ; } const notebook = toJS ( content . model . notebook ) ; return dependencies . contentProvider . save ( serverConfig , content . filepath , { content : notebook , type : "notebook" } ) . pipe ( tap ( ( xhr ) => { if ( xhr . status !== 200 ) { throw new Error ( xhr . response ) ; } } ) , map ( ( nb ) => { return actions . publishToBookstoreAfterSave ( { contentRef : action . payload . contentRef , model : { name : content . filepath . split ( "/" ) . pop ( ) , path : content . filepath , type : content . type , created : content && content . created && content . created . toString ( ) , last_modified : "" , content : notebook , mimetype : content . mimetype , format : content . format } } ) ; } ) , catchError ( ( xhrError ) => of ( actions . publishToBookstoreFailed ( { error : xhrError , contentRef : action . payload . contentRef } ) ) ) ) ; } ) ) ; } param action$ param state$ export function publishToBookstoreAfterSave ( action$ , state$ ) { const state = state$ . value ; const host = selectors . currentHost ( state ) ; const serverConfig = selectors . serverConfig ( host ) ; return action$ . pipe ( ofType ( actions . PUBLISH_TO_BOOKSTORE_AFTER_SAVE ) , switchMap ( action => { const targetPath = action . payload . model . path ; const model = action . payload . model ; return bookstore . publish ( serverConfig , targetPath , model ) . pipe ( tap ( ( xhr ) => { if ( xhr . status !== 200 ) { throw new Error ( xhr . response ) ; } } ) , map ( ( ) => { actions . publishToBookstoreSucceeded ( { contentRef : action . payload . contentRef } ) ; } ) , catchError ( ( xhrError ) => of ( actions . publishToBookstoreFailed ( { error : xhrError , contentRef : action . payload . contentRef } ) ) ) ) ; } ) ) ; }