spee.ch/react/containers/ShowAsset/view.jsx

88 lines
2.9 KiB
React
Raw Normal View History

import React from 'react';
import ErrorPage from 'components/ErrorPage';
import ShowAssetLite from 'components/ShowAssetLite';
import ShowAssetDetails from 'components/ShowAssetDetails';
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);
}
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);
} else { // case: the asset request does not exist
2018-02-08 05:15:44 +01:00
this.onNewRequest(requestId, requestName, requestModifier);
}
}
componentWillReceiveProps (nextProps) {
2018-02-08 05:15:44 +01:00
// case where componentDidMount triggered new props
2018-02-08 08:18:18 +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);
} else { // case: the asset request does not exist
2018-02-08 05:15:44 +01:00
this.onNewRequest(requestId, requestName, requestModifier);
}
2018-02-08 05:15:44 +01:00
} else {
console.log('show.assetRequests did not update');
}
}
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 }) {
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 06:30:32 +01:00
// update the showAsset data in the store
2018-02-08 07:22:17 +01:00
const { assets } = this.props;
2018-02-08 06:30:32 +01:00
const assetId = `a#${name}#${claimId}`;
if (assets[assetId]) { // case: the asset data already exists
2018-02-08 07:22:17 +01:00
let { error: assetError, name, claimId, shortId, claimData } = assets[assetId];
this.props.onShowExistingAsset(assetId, assetError, name, claimId, shortId, claimData);
} else { // case: the asset data does not exist yet
this.props.onShowNewAsset(assetId, name, claimId);
}
}
2018-02-05 01:40:28 +01:00
componentWillUnmount () {
this.props.onLeaveShowAsset();
2018-02-05 01:40:28 +01:00
}
render () {
const { error, name, requestExtension } = this.props;
if (error) {
return (
<ErrorPage error={error}/>
);
}
2018-02-08 05:15:44 +01:00
if (name) { // direct requests are passing because name is present so it just goes
if (requestExtension) {
2018-02-05 02:51:17 +01:00
return (
<ShowAssetLite />
2018-02-05 02:51:17 +01:00
);
} else {
return (
<ShowAssetDetails />
2018-02-05 02:51:17 +01:00
);
}
};
return (
<div> </div>
);
}
};
export default ShowAsset;