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

121 lines
3.1 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';
import request from 'utils/request';
class ShowAsset extends React.Component {
constructor (props) {
super(props);
this.getLongClaimId = this.getLongClaimId.bind(this);
this.getClaimData = this.getClaimData.bind(this);
}
componentDidMount () {
const { name, modifier } = this.props;
// create request params
let body = {};
if (modifier) {
if (modifier.id) {
body['claimId'] = modifier.id;
} else {
body['channelName'] = modifier.channel.name;
body['channelClaimId'] = modifier.channel.id;
}
}
body['claimName'] = name;
const params = {
method : 'POST',
headers: new Headers({
'Content-Type': 'application/json',
}),
body: JSON.stringify(body),
}
// make request
this.getLongClaimId(params)
.then(claimLongId => {
2018-02-07 00:05:31 +01:00
return Promise.all([this.getShortClaimId(claimLongId, name), this.getClaimData(claimLongId, name)]);
})
2018-02-05 02:43:02 +01:00
.then(([shortId, claimData]) => {
this.props.onAssetClaimDataUpdate(claimData, shortId);
})
.catch(error => {
this.props.onShowAssetError(error);
});
}
getLongClaimId (params) {
2018-02-07 06:55:04 +01:00
const url = `/api/claim/long-id`;
return new Promise((resolve, reject) => {
request(url, params)
2018-02-05 02:43:02 +01:00
.then(({ success, message, data }) => {
console.log('get long claim id response:', message);
if (!success) {
reject(message);
}
2018-02-05 02:43:02 +01:00
resolve(data);
})
.catch((error) => {
reject(error.message);
});
});
}
getShortClaimId (longId, name) {
2018-02-07 06:55:04 +01:00
const url = `/api/claim/short-id/${longId}/${name}`;
2018-02-05 02:43:02 +01:00
return new Promise((resolve, reject) => {
request(url)
.then(({ success, message, data }) => {
console.log('get short claim id response:', data);
if (!success) {
reject(message);
}
resolve(data);
})
.catch((error) => {
reject(error.message);
});
});
}
2018-02-05 02:43:02 +01:00
getClaimData (claimId, claimName) {
return new Promise((resolve, reject) => {
2018-02-07 06:55:04 +01:00
const url = `/api/claim/data/${claimName}/${claimId}`;
return request(url)
.then(({ success, message }) => {
console.log('get claim data response:', message);
if (!success) {
reject(message);
}
resolve(message);
})
.catch((error) => {
reject(error.message);
});
});
}
2018-02-05 01:40:28 +01:00
componentWillUnmount () {
this.props.onAssetClaimDataClear();
}
render () {
const { error, claimData, extension } = this.props;
if (error) {
return (
<ErrorPage error={error}/>
);
}
if (claimData) {
if (extension) {
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;