Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. uploadFile(file) {
  2. this.setState({
  3. uploading: true,
  4. dragIn: false,
  5. });
  6. const url = `${API_URL}/co_request/attached_document`;
  7. const docUrl = `${DOC_URL}/document`;
  8. const uploadDocUrl = `${DOC_URL}/upload/`;
  9. const formData = new FormData();
  10.  
  11. const { intl } = this.props;
  12.  
  13. const owners = [];
  14. if(!owners.includes(this.props.ownerKey)){
  15. owners.push(this.props.ownerKey);
  16. }
  17. if(!owners.includes(this.props.userInfo.person.person_key)){
  18. owners.push(this.props.userInfo.person.person_key);
  19. }
  20. if(!owners.includes(this.props.operation.requester_key)){
  21. owners.push(this.props.operation.requester_key);
  22. }
  23.  
  24. fetchAuthorized(
  25. docUrl,
  26. { method: 'POST',
  27. body: JSON.stringify({
  28. document: {
  29. type: this.props.documentType,
  30. observation: "",
  31. owners,
  32. certifier: 'notary_office',
  33. refer_to: this.props.ownerDocument,
  34. },
  35. signatories: [],
  36. }),
  37. },
  38. null,
  39. this.props.userInfo.person.person_key,
  40. true,
  41. ).then(
  42. (document) => {
  43. formData.append('file', file[0]);
  44. fetchAuthorized(
  45. `${uploadDocUrl}${document.document_key}`,
  46. { method: 'PUT', body: formData },
  47. null,
  48. this.props.userInfo.person.person_key,
  49. true,
  50. )
  51. .then(responseUp => {
  52. fetchAuthorized(
  53. url,
  54. { method: 'POST',
  55. body: JSON.stringify({
  56. related_party_key: this.props.relatedPartyKey,
  57. attached_document_list: [
  58. {
  59. document_type: this.props.documentType,
  60. document_key: responseUp.document_key,
  61. document_url: responseUp.url,
  62. },
  63. ],
  64. }),
  65. },
  66. null,
  67. this.props.userInfo.person.person_key,
  68. true,
  69. ).then(
  70. (response) => {
  71. this.setState({
  72. uploading: false,
  73. dragIn: false,
  74. uploaded: true,
  75. });
  76. toast.success(
  77. `${file[0].name} ${intl.formatMessage(messages.uploaded)}`,
  78. );
  79. this.props.updateAfterUpload(response);
  80. }
  81. ).catch(error => {
  82. console.log(error);
  83. this.setState({ uploading: false, uploaded: false, dragIn: true });
  84. toast.error(
  85. `${file[0].name} ${intl.formatMessage(
  86. messages.uploadFailed,
  87. )} ${JSON.stringify(error.response.description)}`,
  88. );
  89. });
  90. })
  91. .catch(error => {
  92. console.log(error);
  93. this.setState({ uploading: false, uploaded: false, dragIn: true });
  94. toast.error(
  95. `${file[0].name} ${intl.formatMessage(
  96. messages.uploadFailed,
  97. )} ${JSON.stringify(error.response.description)}`,
  98. );
  99. });
  100. }
  101. ).catch(error => {
  102. console.log(error);
  103. this.setState({ uploading: false, uploaded: false, dragIn: false });
  104. toast.error(
  105. `${file[0].name} ${intl.formatMessage(
  106. messages.uploadFailed,
  107. )} ${JSON.stringify(error.response.description)}`,
  108. );
  109. });
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement