From 8672f7f41a6c5345fe49f35fdc4b7f3b11f6d13a Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 7 Feb 2018 21:30:32 -0800 Subject: [PATCH] added new channel request handling --- react/actions/show.js | 53 ++++++++++++++--------- react/api/channelApi.js | 6 +-- react/constants/show_action_types.js | 4 ++ react/containers/ShowAsset/view.jsx | 4 +- react/containers/ShowChannel/index.js | 28 ++++++++---- react/containers/ShowChannel/view.jsx | 54 ++++++++++++++++++++--- react/reducers/show.js | 62 ++++++++++++--------------- react/sagas/show.js | 22 +++++++++- 8 files changed, 157 insertions(+), 76 deletions(-) diff --git a/react/actions/show.js b/react/actions/show.js index f0693c2b..cf6c0692 100644 --- a/react/actions/show.js +++ b/react/actions/show.js @@ -75,29 +75,42 @@ export function clearShowAsset () { // request for a channel +export function newChannelRequest (id, name, channelId) { + return { + type: actions.NEW_CHANNEL_REQUEST, + data: {id, name, channelId}, + }; +}; + +export function addChannelRequest (id, error, name, claimId) { + return { + type: actions.CHANNEL_REQUEST_ADD, + data: { id, error, name, claimId }, + }; +} // show a channel -export function updateShowChannelError (error) { - return { - type: actions.SHOW_CHANNEL_ERROR, - data: error, - }; -}; - -export function updateChannelData (name, longId, shortId) { - return { - type: actions.CHANNEL_DATA_UPDATE, - data: { name, longId, shortId }, - }; -}; - -export function updateChannelClaimsData (claims, currentPage, totalPages, totalClaims) { - return { - type: actions.CHANNEL_CLAIMS_DATA_UPDATE, - data: { claims, currentPage, totalPages, totalClaims }, - }; -}; +// export function updateShowChannelError (error) { +// return { +// type: actions.SHOW_CHANNEL_ERROR, +// data: error, +// }; +// }; +// +// export function updateChannelData (name, longId, shortId) { +// return { +// type: actions.CHANNEL_DATA_UPDATE, +// data: { name, longId, shortId }, +// }; +// }; +// +// export function updateChannelClaimsData (claims, currentPage, totalPages, totalClaims) { +// return { +// type: actions.CHANNEL_CLAIMS_DATA_UPDATE, +// data: { claims, currentPage, totalPages, totalClaims }, +// }; +// }; // display a file diff --git a/react/api/channelApi.js b/react/api/channelApi.js index d3fbbb7f..3ee06e8a 100644 --- a/react/api/channelApi.js +++ b/react/api/channelApi.js @@ -1,6 +1,6 @@ import Request from 'utils/request'; -export function getLongClaimId (name, modifier) { +export function getLongChannelClaimId (name, modifier) { let body = {}; // create request params if (modifier) { @@ -25,12 +25,12 @@ export function getLongClaimId (name, modifier) { return Request(url, params); }; -export function getShortId (name, claimId) { +export function getShortChannelId (name, claimId) { const url = `/api/claim/short-id/${claimId}/${name}`; return Request(url); }; -export function getClaimData (name, claimId) { +export function getChannelData (name, claimId) { const url = `/api/claim/data/${name}/${claimId}`; return Request(url); }; diff --git a/react/constants/show_action_types.js b/react/constants/show_action_types.js index ae6d587d..30040607 100644 --- a/react/constants/show_action_types.js +++ b/react/constants/show_action_types.js @@ -18,3 +18,7 @@ export const NEW_ASSET_REQUEST = 'NEW_ASSET_REQUEST'; export const ASSET_REQUEST_ADD = 'ASSET_REQUEST_ADD'; export const SHOW_NEW_ASSET = 'SHOW_NEW_ASSET'; export const SHOW_ASSET_CLEAR = 'SHOW_ASSET_CLEAR'; + +export const NEW_CHANNEL_REQUEST = 'NEW_CHANNEL_REQUEST'; +export const CHANNEL_REQUEST_ADD = 'CHANNEL_REQUEST_ADD'; + diff --git a/react/containers/ShowAsset/view.jsx b/react/containers/ShowAsset/view.jsx index e9a336fa..fcd6e733 100644 --- a/react/containers/ShowAsset/view.jsx +++ b/react/containers/ShowAsset/view.jsx @@ -38,12 +38,12 @@ class ShowAsset extends React.Component { console.log('repeat request'); const { assets } = this.props; const { error: requestError, name, claimId } = request; - const assetId = `a#${name}#${claimId}`; // if error, return and update state with error if (requestError) { return this.props.onRequestError(requestError); } - // update the show asset data in the store + // update the showAsset data in the store + const assetId = `a#${name}#${claimId}`; if (assets[assetId]) { // case: the asset data already exists let { error, name, claimId, shortId, claimData } = assets[assetId]; this.props.onShowExistingAsset(assetId, error, name, claimId, shortId, claimData); diff --git a/react/containers/ShowChannel/index.js b/react/containers/ShowChannel/index.js index 4d0c1058..0ec9bd8f 100644 --- a/react/containers/ShowChannel/index.js +++ b/react/containers/ShowChannel/index.js @@ -1,12 +1,15 @@ import { connect } from 'react-redux'; -import {updateChannelData, updateShowChannelError} from 'actions/show'; +import {newChannelRequest, updateRequestError} from 'actions/show'; import View from './view'; const mapStateToProps = ({ show }) => { return { requestId : show.request.id, + requestType : show.request.type, requestChannelName: show.request.data.name, requestChannelId : show.request.data.id, + requestList : show.channelRequests, + channels : show.channels, error : show.showChannel.error, name : show.showChannel.channelData.name, shortId : show.showChannel.channelData.shortId, @@ -16,15 +19,22 @@ const mapStateToProps = ({ show }) => { const mapDispatchToProps = dispatch => { return { - onShowChannelError: (error) => { - dispatch(updateShowChannelError(error)); + // onShowChannelError: (error) => { + // dispatch(updateShowChannelError(error)); + // }, + // onChannelDataUpdate: (name, longId, shortId) => { + // dispatch(updateChannelData(name, longId, shortId)); + // dispatch(updateShowChannelError(null)); // clear any errors + // }, + // onChannelDataClear: () => { + // dispatch(updateChannelData(null, null, null)); + // }, + // new + onNewChannelRequest (id, name, channelId) { + dispatch(newChannelRequest(id, name, channelId)); }, - onChannelDataUpdate: (name, longId, shortId) => { - dispatch(updateChannelData(name, longId, shortId)); - dispatch(updateShowChannelError(null)); // clear any errors - }, - onChannelDataClear: () => { - dispatch(updateChannelData(null, null, null)); + onRequestError: (error) => { + dispatch(updateRequestError(error, null, null)); }, }; }; diff --git a/react/containers/ShowChannel/view.jsx b/react/containers/ShowChannel/view.jsx index 16bc7df7..8d40e827 100644 --- a/react/containers/ShowChannel/view.jsx +++ b/react/containers/ShowChannel/view.jsx @@ -4,17 +4,59 @@ import NavBar from 'containers/NavBar'; import ChannelClaimsDisplay from 'containers/ChannelClaimsDisplay'; import request from 'utils/request'; +import { CHANNEL } from 'constants/show_request_types'; + +function requestIsAChannelRequest ({ requestType }) { + return requestType === CHANNEL; +} + +function channelNameOrIdChanged (nextProps, props) { + return (nextProps.requestChannelName !== props.requestChannelName || nextProps.requestChannelName !== props.requestChannelName); +} + +function existingRequest (requestId, requestList) { + return requestList[requestId]; +} + class ShowChannel extends React.Component { componentDidMount () { console.log('showchannel did mount'); - const {requestChannelName, requestChannelId} = this.props; - this.getAndStoreChannelData(requestChannelName, requestChannelId); + const {requestId, requestName, requestChannelId, requestList} = this.props; + if (existingRequest(requestId, requestList)) { + // const validRequest = existingRequest(requestId, requestList); + // this.onRepeatChannelRequest(validRequest); + console.log('weird, we got a repeat channel request on an unmounted ShowChannel component'); + } else { + this.onNewChannelRequest(requestId, requestName, requestChannelId); + } } componentWillReceiveProps (nextProps) { - if (nextProps.channelRequests !== this.props.channelRequests) { - const {requestChannelName, requestChannelId} = nextProps; - this.getAndStoreChannelData(requestChannelName, requestChannelId); - } + console.log('showchannel will receive new props'); + if (requestIsAChannelRequest(nextProps) && channelNameOrIdChanged(nextProps, this.props)) { + const {requestId, requestList} = nextProps; + if (existingRequest(requestId, requestList)) { + const request = requestList[requestId]; + this.onRepeatChannelRequest(request); + } else { + console.log('weird, we got a new channel request on a mounted ShowChannel component'); + } + }; + } + onNewChannelRequest (requestId, requestName, requestChannelId) { + // validate the request (i.e. get channel full claim id) + // update teh request list + // if error, return early (set the request error in the store) + // if the request is valid... + // add it to the requestList + // update showChannel to reflect the channel details + this.props.onNewChannelRequest(requestId, requestName, requestChannelId); + } + onRepeatChannelRequest ({ id, error, name, claimId }) { + // if error, return early (set the request error in the store) + // if the request is valid... + // update showChannel to reflect the channel details + // see if they are available + // retrieve them if they are not available } getAndStoreChannelData (name, id) { console.log('getting and storing channel data for channel:', name, id); diff --git a/react/reducers/show.js b/react/reducers/show.js index 6694eb4e..d80e9c8e 100644 --- a/react/reducers/show.js +++ b/react/reducers/show.js @@ -9,21 +9,6 @@ const initialState = { data : null, requestId: null, }, - // channelRequest: { - // name: null, - // id : null, - // }, - // assetRequest: { - // name : null, - // modifier: { - // id : null, - // channel: { - // name: null, - // id : null, - // }, - // }, - // extension: null, - // }, showChannel: { error : null, channelData: { @@ -135,26 +120,35 @@ export default function (state = initialState, action) { }), }); // request a channel - + case actions.CHANNEL_REQUEST_ADD: + return Object.assign({}, state, { + channelRequests: Object.assign({}, state.assetRequests, { + [action.data.id]: { + error : action.data.error, + name : action.data.name, + claimId: action.data.claimId, + }, + }), + }); // show a channel - case actions.SHOW_CHANNEL_ERROR: - return Object.assign({}, state, { - showChannel: Object.assign({}, state.showChannel, { - error: action.data, - }), - }); - case actions.CHANNEL_DATA_UPDATE: - return Object.assign({}, state, { - showChannel: Object.assign({}, state.showChannel, { - channelData: action.data, - }), - }); - case actions.CHANNEL_CLAIMS_DATA_UPDATE: - return Object.assign({}, state, { - showChannel: Object.assign({}, state.showChannel, { - channelClaimsData: action.data, - }), - }); + // case actions.SHOW_CHANNEL_ERROR: + // return Object.assign({}, state, { + // showChannel: Object.assign({}, state.showChannel, { + // error: action.data, + // }), + // }); + // case actions.CHANNEL_DATA_UPDATE: + // return Object.assign({}, state, { + // showChannel: Object.assign({}, state.showChannel, { + // channelData: action.data, + // }), + // }); + // case actions.CHANNEL_CLAIMS_DATA_UPDATE: + // return Object.assign({}, state, { + // showChannel: Object.assign({}, state.showChannel, { + // channelClaimsData: action.data, + // }), + // }); // display an asset case actions.FILE_AVAILABILITY_UPDATE: return Object.assign({}, state, { diff --git a/react/sagas/show.js b/react/sagas/show.js index 90d63cd0..9ec5a24a 100644 --- a/react/sagas/show.js +++ b/react/sagas/show.js @@ -1,9 +1,10 @@ import { call, put, takeLatest } from 'redux-saga/effects'; import * as actions from 'constants/show_action_types'; -import { addAssetRequest, updateShowAsset, updateFileAvailability, updateDisplayAssetError } from 'actions/show'; +import { addAssetRequest, updateShowAsset, addChannelRequest, updateShowChannel, updateFileAvailability, updateDisplayAssetError } from 'actions/show'; import { UNAVAILABLE, AVAILABLE } from 'constants/asset_display_states'; import { checkFileAvailability, triggerClaimGet } from 'api/fileApi'; import { getLongClaimId, getShortId, getClaimData } from 'api/assetApi'; +import { getLongChannelClaimId, getShortChannelId, getChannelData } from 'api/channelApi'; function* newAssetRequest (action) { const { id, name, modifier } = action.data; @@ -11,7 +12,6 @@ function* newAssetRequest (action) { try { ({success, message, data: longId} = yield call(getLongClaimId, name, modifier)); } catch (error) { - console.log('error making getLongClaimId call', error); yield put(addAssetRequest(id, error.message, name, null)); } if (success) { @@ -20,6 +20,20 @@ function* newAssetRequest (action) { yield put(addAssetRequest(id, message, name, null)); }; +function* newChannelRequest (action) { + const { id, name, channelId } = action.data; + let success, message, longChannelId; + try { + ({success, message, data: longChannelId} = yield call(getLongChannelClaimId, name, channelId)); + } catch (error) { + yield put(addChannelRequest(id, error.message, name, null)); + } + if (success) { + return yield put(addChannelRequest(id, null, name, longChannelId)); + } + yield put(addChannelRequest(id, message, name, null)); +} + function* getAssetDataAndShowAsset (action) { const {id, name, claimId} = action.data; // if no error, get short Id @@ -83,6 +97,10 @@ export function* watchNewAssetRequest () { yield takeLatest(actions.NEW_ASSET_REQUEST, newAssetRequest); }; +export function* watchNewChannelRequest () { + yield takeLatest(actions.NEW_CHANNEL_REQUEST, newChannelRequest); +}; + export function* watchShowNewAsset () { yield takeLatest(actions.SHOW_NEW_ASSET, getAssetDataAndShowAsset); };