import { Store } from 'shared/libs/stores' ; import cfg from 'teleport/config' ; import { File } from './types' ; const defaultState = { files : [ ] as File [ ] , clusterId : '' , serverId : '' , login : '' , } ; type State = typeof defaultState ; export default class StoreFiles extends Store < State > { state = { ... defaultState , } ; constructor ( json ? ) { super ( ) ; json && this . setState ( json ) ; } makeUrl ( location , filename ) { const { clusterId , serverId , login } = this . state ; return cfg . getScpUrl ( { clusterId , serverId , login , location , filename , } ) ; } remove ( id ) { const files = this . state . files . filter ( f => f . id !== id ) ; return this . setState ( { files } ) ; } add ( { location , name , blob , isUpload } ) { const url = this . makeUrl ( location , name ) ; const file = makeFile ( { url , name , isUpload , blob , } ) ; return this . setState ( { files : [ ... this . state . files , file ] , } ) ; } update ( partial ) { const index = this . state . files . findIndex ( f => f . id === partial . id ) ; const file = this . state . files [ index ] ; this . state . files [ index ] = { ... file , ... partial , } ; this . setState ( { files : [ ... this . state . files ] , } ) ; } } export function makeFile ( json ) { const { url , name , isUpload , blob } = json ; return { id : new Date ( ) . getTime ( ) + name , url , name , isUpload , blob , status : 'processing' , error : '' , } ; }