Guest User

Untitled

a guest
Nov 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import * as React from 'react';
  2. import { observable } from 'mobx';
  3. import { observer } from 'mobx-react';
  4.  
  5. import { DocumentModel } from '../models';
  6. import { Icon, Tooltip } from '../controls';
  7.  
  8. @observer
  9. export class Component extends React.Component<{
  10. record: DocumentModel,
  11. privateKey: string,
  12. getLabel: (privateKey: string, url: string) => Promise<string>,
  13. }, {}> {
  14. @observable isLoading = false;
  15. @observable isError = false;
  16. @observable result: string | undefined;
  17.  
  18. url: string = '';
  19.  
  20. componentWillUnmount() {
  21. URL.revokeObjectURL(this.url);
  22. }
  23.  
  24. loadFile = () => {
  25. const { record, privateKey } = this.props;
  26. this.isLoading = true;
  27. this.props.getLabel(privateKey, record.url)
  28. .then(res => { this.isLoading = false; this.result = res; })
  29. .catch(err => { this.isLoading = false; this.isError = true; });
  30. }
  31.  
  32. render() {
  33. if (this.isLoading) {
  34. return (<Icon type={'loading'} />);
  35. }
  36.  
  37. if (this.isError) {
  38. return (
  39. <Tooltip title={'Label not available'}>
  40. <Icon type="exclamation-circle-o" />
  41. </Tooltip>
  42. );
  43. }
  44.  
  45. if (this.result) {
  46. const name = `${this.props.record.id}.${this.props.record.fileType}`;
  47.  
  48. this.url = URL.createObjectURL(new Blob([this.result]));
  49.  
  50. return (
  51. <Tooltip title={'Download is ready'}>
  52. <a href={this.url} download={name}>
  53. <Icon type={'download'} />
  54. </a>
  55. </Tooltip>
  56. );
  57. }
  58.  
  59. return (
  60. <a onClick={() => { this.loadFile(); }}>{'Check'}</a>
  61. );
  62. }
  63. }
  64.  
  65. import { connect } from 'react-redux';
  66. import { RootState } from '@src/modules';
  67. import { SitesSelectors } from '@src/modules/sites';
  68. import { getLabel } from '@src/services';
  69. const mapStateToProps = (state: RootState) => ({
  70. privateKey: SitesSelectors.getSelectedSitePrivateKey(state),
  71. getLabel: getLabel,
  72. });
  73.  
  74. const Connected = connect(mapStateToProps, {
  75. })(Component);
  76.  
  77. export default Connected;
Add Comment
Please, Sign In to add comment