2018-01-31 02:15:23 +01:00
|
|
|
import React from 'react';
|
2018-02-07 20:30:39 +01:00
|
|
|
import ErrorPage from 'components/ErrorPage';
|
2018-02-02 20:10:58 +01:00
|
|
|
import ShowAssetLite from 'components/ShowAssetLite';
|
|
|
|
import ShowAssetDetails from 'components/ShowAssetDetails';
|
2018-01-31 02:15:23 +01:00
|
|
|
|
2018-02-08 08:18:18 +01:00
|
|
|
import { ASSET } from 'constants/show_request_types';
|
|
|
|
|
|
|
|
function requestIsAnAssetRequest ({ requestType }) {
|
|
|
|
return requestType === ASSET;
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestIsNewRequest (nextProps, props) {
|
|
|
|
return (nextProps.requestId !== props.requestId);
|
|
|
|
}
|
|
|
|
|
2018-01-31 02:15:23 +01:00
|
|
|
class ShowAsset extends React.Component {
|
|
|
|
componentDidMount () {
|
2018-02-08 05:15:44 +01:00
|
|
|
const { requestId, requestName, requestModifier, assetRequests } = this.props;
|
2018-02-08 08:18:18 +01:00
|
|
|
const existingRequest = assetRequests[requestId];
|
|
|
|
if (existingRequest) { // case: the assetRequest exists
|
|
|
|
this.onRepeatRequest(existingRequest);
|
2018-02-08 03:01:51 +01:00
|
|
|
} else { // case: the asset request does not exist
|
2018-02-08 05:15:44 +01:00
|
|
|
this.onNewRequest(requestId, requestName, requestModifier);
|
2018-01-31 02:15:23 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-08 03:01:51 +01:00
|
|
|
componentWillReceiveProps (nextProps) {
|
2018-02-08 05:15:44 +01:00
|
|
|
// case where componentDidMount triggered new props
|
2018-02-08 22:22:54 +01:00
|
|
|
if (requestIsAnAssetRequest(nextProps) && requestIsNewRequest(nextProps, this.props)) {
|
2018-02-08 05:15:44 +01:00
|
|
|
const { requestId, requestName, requestModifier, assetRequests } = nextProps;
|
2018-02-08 08:18:18 +01:00
|
|
|
const existingRequest = assetRequests[requestId];
|
|
|
|
if (existingRequest) { // case: the assetRequest exists
|
|
|
|
this.onRepeatRequest(existingRequest);
|
2018-02-08 03:01:51 +01:00
|
|
|
} else { // case: the asset request does not exist
|
2018-02-08 05:15:44 +01:00
|
|
|
this.onNewRequest(requestId, requestName, requestModifier);
|
2018-02-08 03:01:51 +01:00
|
|
|
}
|
2018-02-08 05:15:44 +01:00
|
|
|
} else {
|
2018-02-08 22:22:54 +01:00
|
|
|
console.log('ShowAsset receiving new props -> request.id did not update', nextProps);
|
2018-02-08 03:01:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
onNewRequest (id, requestName, requestModifier) {
|
|
|
|
console.log('new request');
|
|
|
|
this.props.onNewRequest(id, requestName, requestModifier);
|
|
|
|
}
|
2018-02-08 07:22:17 +01:00
|
|
|
onRepeatRequest ({ error, name, claimId }) {
|
2018-02-08 03:01:51 +01:00
|
|
|
console.log('repeat request');
|
|
|
|
// if error, return and update state with error
|
2018-02-08 07:22:17 +01:00
|
|
|
if (error) {
|
|
|
|
return this.props.onRequestError(error);
|
2018-02-08 03:01:51 +01:00
|
|
|
}
|
2018-02-08 06:30:32 +01:00
|
|
|
// update the showAsset data in the store
|
2018-02-08 20:50:30 +01:00
|
|
|
const { assetList } = this.props;
|
2018-02-08 06:30:32 +01:00
|
|
|
const assetId = `a#${name}#${claimId}`;
|
2018-02-08 20:50:30 +01:00
|
|
|
const existingAssetRecord = assetList[assetId];
|
2018-02-08 08:49:01 +01:00
|
|
|
if (existingAssetRecord) { // case: the asset data already exists
|
2018-02-08 20:22:19 +01:00
|
|
|
this.showExistingAsset(existingAssetRecord);
|
2018-02-08 03:01:51 +01:00
|
|
|
} else { // case: the asset data does not exist yet
|
2018-02-08 19:02:29 +01:00
|
|
|
this.showNewAsset(assetId, name, claimId);
|
2018-02-08 03:01:51 +01:00
|
|
|
}
|
2018-01-31 02:15:23 +01:00
|
|
|
}
|
2018-02-08 19:02:29 +01:00
|
|
|
showNewAsset (assetId, name, claimId) {
|
|
|
|
this.props.onShowNewAsset(assetId, name, claimId);
|
|
|
|
}
|
2018-02-08 20:22:19 +01:00
|
|
|
showExistingAsset (existingAssetRecord) {
|
2018-02-08 19:02:29 +01:00
|
|
|
let { error, name, claimId, shortId, claimData } = existingAssetRecord;
|
2018-02-08 20:22:19 +01:00
|
|
|
this.props.onShowExistingAsset(error, name, claimId, shortId, claimData);
|
2018-02-08 19:02:29 +01:00
|
|
|
}
|
2018-02-05 01:40:28 +01:00
|
|
|
componentWillUnmount () {
|
2018-02-08 03:01:51 +01:00
|
|
|
this.props.onLeaveShowAsset();
|
2018-02-05 01:40:28 +01:00
|
|
|
}
|
2018-01-31 02:15:23 +01:00
|
|
|
render () {
|
2018-02-08 03:01:51 +01:00
|
|
|
const { error, name, requestExtension } = this.props;
|
2018-02-07 19:52:09 +01:00
|
|
|
if (error) {
|
|
|
|
return (
|
2018-02-07 20:30:39 +01:00
|
|
|
<ErrorPage error={error}/>
|
2018-02-07 19:52:09 +01:00
|
|
|
);
|
|
|
|
}
|
2018-02-08 05:15:44 +01:00
|
|
|
if (name) { // direct requests are passing because name is present so it just goes
|
2018-02-08 03:01:51 +01:00
|
|
|
if (requestExtension) {
|
2018-02-05 02:51:17 +01:00
|
|
|
return (
|
2018-02-07 19:52:09 +01:00
|
|
|
<ShowAssetLite />
|
2018-02-05 02:51:17 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2018-02-07 19:52:09 +01:00
|
|
|
<ShowAssetDetails />
|
2018-02-05 02:51:17 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2018-01-31 02:15:23 +01:00
|
|
|
return (
|
2018-02-07 19:52:09 +01:00
|
|
|
<div> </div>
|
2018-01-31 02:15:23 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ShowAsset;
|