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

99 lines
3.4 KiB
React
Raw Normal View History

import React from 'react';
import ErrorPage from 'components/ErrorPage';
import ShowAsset from 'containers/ShowAsset';
import ShowChannel from 'containers/ShowChannel';
import lbryUri from 'utils/lbryUri';
import { CHANNEL, ASSET } from 'constants/show_request_types';
class ShowPage extends React.Component {
constructor (props) {
super(props);
this.state = {
error: null,
};
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);
}
componentDidMount () {
console.log('ShowPage did mount');
const identifier = this.props.match.params.identifier;
const claim = this.props.match.params.claim;
this.parseUrlAndUpdateState(identifier, claim);
}
componentWillReceiveProps (nextProps) {
if (nextProps.match.params !== this.props.match.params) {
console.log('ShowPage received new params props');
const identifier = nextProps.match.params.identifier;
const claim = nextProps.match.params.claim;
this.parseUrlAndUpdateState(identifier, claim);
}
}
parseUrlAndUpdateState (identifier, claim) {
if (identifier) {
2018-01-31 20:49:20 +01:00
return this.parseAndUpdateIdentifierAndClaim(identifier, claim);
}
2018-01-31 20:49:20 +01:00
this.parseAndUpdateClaimOnly(claim);
}
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
let isChannel, channelName, channelClaimId, claimId, claimName, extension;
2018-01-31 20:49:20 +01:00
try {
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(modifier));
({ claimName, extension } = lbryUri.parseClaim(claim));
2018-01-31 20:49:20 +01:00
} catch (error) {
return this.setState({error: error.message});
2018-01-31 20:49:20 +01:00
}
// update the store
if (isChannel) {
return this.props.onAssetRequest(claimName, null, channelName, channelClaimId, extension);
} else {
return this.props.onAssetRequest(claimName, claimId, null, null, extension);
}
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
let isChannel, channelName, channelClaimId;
try {
({ isChannel, channelName, channelClaimId } = lbryUri.parseIdentifier(claim));
} catch (error) {
return this.setState({error: error.message});
}
// return early if this request is for a channel
if (isChannel) {
return this.props.onChannelRequest(channelName, channelClaimId);
}
// 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?
try {
({claimName, extension} = lbryUri.parseClaim(claim));
} catch (error) {
return this.setState({error: error.message});
}
this.props.onAssetRequest(claimName, null, null, null, extension);
}
render () {
console.log('rendering ShowPage');
console.log('ShowPage props', this.props);
if (this.state.error) {
return (
<ErrorPage error={this.state.error}/>
);
}
switch (this.props.requestType) {
case CHANNEL:
return <ShowChannel/>;
case ASSET:
return <ShowAsset/>;
default:
return <p>loading...</p>;
}
}
};
export default ShowPage;