2018-01-30 18:00:02 +01:00
|
|
|
import React from 'react';
|
2018-02-01 04:12:54 +01:00
|
|
|
import ErrorPage from 'components/ErrorPage';
|
2018-02-14 20:39:24 +01:00
|
|
|
import ShowAssetLite from 'components/ShowAssetLite';
|
|
|
|
import ShowAssetDetails from 'components/ShowAssetDetails';
|
|
|
|
import ShowChannel from 'components/ShowChannel';
|
2018-01-30 18:00:02 +01:00
|
|
|
import lbryUri from 'utils/lbryUri';
|
|
|
|
|
2018-02-14 20:39:24 +01:00
|
|
|
import { CHANNEL, ASSET_LITE, ASSET_DETAILS } from 'constants/show_request_types';
|
2018-02-02 20:10:58 +01:00
|
|
|
|
2018-01-30 18:00:02 +01:00
|
|
|
class ShowPage extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2018-01-31 20:49:20 +01:00
|
|
|
this.parseUrlAndUpdateState = this.parseUrlAndUpdateState.bind(this);
|
|
|
|
this.parseAndUpdateIdentifierAndClaim = this.parseAndUpdateIdentifierAndClaim.bind(this);
|
|
|
|
this.parseAndUpdateClaimOnly = this.parseAndUpdateClaimOnly.bind(this);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
componentDidMount () {
|
2018-02-07 20:30:39 +01:00
|
|
|
const { identifier, claim } = this.props.match.params;
|
2018-01-31 23:50:35 +01:00
|
|
|
this.parseUrlAndUpdateState(identifier, claim);
|
|
|
|
}
|
|
|
|
componentWillReceiveProps (nextProps) {
|
2018-02-03 01:49:39 +01:00
|
|
|
if (nextProps.match.params !== this.props.match.params) {
|
2018-02-07 20:30:39 +01:00
|
|
|
const { identifier, claim } = nextProps.match.params;
|
2018-01-31 23:50:35 +01:00
|
|
|
this.parseUrlAndUpdateState(identifier, claim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parseUrlAndUpdateState (identifier, claim) {
|
2018-01-30 18:00:02 +01:00
|
|
|
if (identifier) {
|
2018-01-31 20:49:20 +01:00
|
|
|
return this.parseAndUpdateIdentifierAndClaim(identifier, claim);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
2018-01-31 20:49:20 +01:00
|
|
|
this.parseAndUpdateClaimOnly(claim);
|
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
parseAndUpdateIdentifierAndClaim (modifier, claim) {
|
2018-01-31 20:49:20 +01:00
|
|
|
// this is a request for an asset
|
|
|
|
// claim will be an asset claim
|
|
|
|
// the identifier could be a channel or a claim id
|
2018-02-01 23:29:33 +01:00
|
|
|
let isChannel, channelName, channelClaimId, claimId, claimName, extension;
|
2018-01-31 20:49:20 +01:00
|
|
|
try {
|
2018-02-01 23:29:33 +01:00
|
|
|
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(modifier));
|
|
|
|
({ claimName, extension } = lbryUri.parseClaim(claim));
|
2018-01-31 20:49:20 +01:00
|
|
|
} catch (error) {
|
2018-02-07 20:30:39 +01:00
|
|
|
return this.props.onRequestError(error.message);
|
2018-01-31 20:49:20 +01:00
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
// update the store
|
|
|
|
if (isChannel) {
|
2018-02-14 20:39:24 +01:00
|
|
|
return this.props.onNewAssetRequest(claimName, null, channelName, channelClaimId, extension);
|
2018-02-01 23:29:33 +01:00
|
|
|
} else {
|
2018-02-14 20:39:24 +01:00
|
|
|
return this.props.onNewAssetRequest(claimName, claimId, null, null, extension);
|
2018-02-01 23:29:33 +01:00
|
|
|
}
|
2018-01-31 20:49:20 +01:00
|
|
|
}
|
|
|
|
parseAndUpdateClaimOnly (claim) {
|
|
|
|
// this could be a request for an asset or a channel page
|
|
|
|
// claim could be an asset claim or a channel claim
|
2018-01-30 18:00:02 +01:00
|
|
|
let isChannel, channelName, channelClaimId;
|
|
|
|
try {
|
|
|
|
({ isChannel, channelName, channelClaimId } = lbryUri.parseIdentifier(claim));
|
|
|
|
} catch (error) {
|
2018-02-07 20:30:39 +01:00
|
|
|
return this.props.onRequestError(error.message);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
// return early if this request is for a channel
|
2018-01-30 18:00:02 +01:00
|
|
|
if (isChannel) {
|
2018-02-14 20:39:24 +01:00
|
|
|
return this.props.onNewChannelRequest(channelName, channelClaimId);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
// if not for a channel, parse the claim request
|
|
|
|
let claimName, extension; // if I am destructuring below, do I still need to declare these here?
|
2018-01-30 18:00:02 +01:00
|
|
|
try {
|
2018-02-01 23:29:33 +01:00
|
|
|
({claimName, extension} = lbryUri.parseClaim(claim));
|
2018-01-30 18:00:02 +01:00
|
|
|
} catch (error) {
|
2018-02-07 20:30:39 +01:00
|
|
|
return this.props.onRequestError(error.message);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
2018-02-14 20:39:24 +01:00
|
|
|
this.props.onNewAssetRequest(claimName, null, null, null, extension);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
render () {
|
2018-02-07 20:30:39 +01:00
|
|
|
const { error, requestType } = this.props;
|
|
|
|
if (error) {
|
2018-02-01 04:12:54 +01:00
|
|
|
return (
|
2018-02-07 20:30:39 +01:00
|
|
|
<ErrorPage error={error}/>
|
2018-02-01 04:12:54 +01:00
|
|
|
);
|
|
|
|
}
|
2018-02-07 20:30:39 +01:00
|
|
|
switch (requestType) {
|
2018-02-02 20:10:58 +01:00
|
|
|
case CHANNEL:
|
2018-02-03 03:16:18 +01:00
|
|
|
return <ShowChannel />;
|
2018-02-14 20:39:24 +01:00
|
|
|
case ASSET_LITE:
|
|
|
|
return <ShowAssetLite />;
|
|
|
|
case ASSET_DETAILS:
|
|
|
|
return <ShowAssetDetails />;
|
2018-02-02 20:10:58 +01:00
|
|
|
default:
|
|
|
|
return <p>loading...</p>;
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ShowPage;
|