lbry-desktop/ui/js/actions/file_info.js

122 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-04-28 17:14:44 +02:00
import * as types from 'constants/action_types'
import lbry from 'lbry'
import {
doClaimListMine
} from 'actions/content'
2017-04-28 17:14:44 +02:00
import {
2017-05-15 05:50:59 +02:00
selectClaimsByUri,
selectClaimListMineIsPending,
2017-04-28 17:14:44 +02:00
} from 'selectors/claims'
2017-05-15 05:50:59 +02:00
import {
selectFileListIsPending,
selectAllFileInfos,
selectUrisLoading,
2017-05-15 05:50:59 +02:00
} from 'selectors/file_info'
import {
doCloseModal,
} from 'actions/app'
const {
shell,
} = require('electron')
2017-04-28 17:14:44 +02:00
2017-05-15 05:50:59 +02:00
export function doFetchFileInfo(uri) {
2017-04-28 17:14:44 +02:00
return function(dispatch, getState) {
const state = getState()
2017-05-15 05:50:59 +02:00
const claim = selectClaimsByUri(state)[uri]
const outpoint = claim ? `${claim.txid}:${claim.nout}` : null
const alreadyFetching = !!selectUrisLoading(state)[uri]
2017-04-28 17:14:44 +02:00
2017-05-15 05:50:59 +02:00
if (!alreadyFetching) {
2017-04-28 17:14:44 +02:00
dispatch({
2017-05-15 05:50:59 +02:00
type: types.FETCH_FILE_INFO_STARTED,
2017-04-28 17:14:44 +02:00
data: {
2017-05-15 05:50:59 +02:00
outpoint,
2017-04-28 17:14:44 +02:00
}
})
2017-05-15 05:50:59 +02:00
lbry.file_list({outpoint: outpoint, full_status: true}).then(fileInfos => {
2017-05-15 05:50:59 +02:00
dispatch({
type: types.FETCH_FILE_INFO_COMPLETED,
data: {
outpoint,
fileInfo: fileInfos && fileInfos.length ? fileInfos[0] : null,
2017-05-15 05:50:59 +02:00
}
})
})
}
2017-04-28 17:14:44 +02:00
}
}
export function doFileList() {
return function(dispatch, getState) {
const state = getState()
const isPending = selectFileListIsPending(state)
if (!isPending) {
dispatch({
type: types.FILE_LIST_STARTED,
})
lbry.file_list().then((fileInfos) => {
dispatch({
type: types.FILE_LIST_COMPLETED,
data: {
fileInfos,
}
})
})
}
}
}
export function doOpenFileInShell(fileInfo) {
return function(dispatch, getState) {
shell.openItem(fileInfo.download_path)
}
}
export function doOpenFileInFolder(fileInfo) {
return function(dispatch, getState) {
shell.showItemInFolder(fileInfo.download_path)
}
}
export function doDeleteFile(outpoint, deleteFromComputer) {
return function(dispatch, getState) {
dispatch({
type: types.FILE_DELETE,
data: {
outpoint
}
})
lbry.file_delete({
outpoint: outpoint,
delete_target_file: deleteFromComputer,
})
dispatch(doCloseModal())
}
}
export function doFetchFileInfosAndPublishedClaims() {
return function(dispatch, getState) {
const state = getState(),
isClaimListMinePending = selectClaimListMineIsPending(state),
isFileInfoListPending = selectFileListIsPending(state)
if (isClaimListMinePending === undefined) {
dispatch(doClaimListMine())
}
if (isFileInfoListPending === undefined) {
dispatch(doFileList())
}
}
}