CreateCollection.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Modal } from 'react-bootstrap';
  3. import { createAlbum } from 'services/collectionService';
  4. import UploadService from 'services/uploadService';
  5. import { CollectionAndItsLatestFile } from 'services/collectionService';
  6. import { getToken } from 'utils/common/key';
  7. import constants from 'utils/strings/constants';
  8. export default function CreateCollection(props) {
  9. const {
  10. acceptedFiles,
  11. setProgressView,
  12. progressBarProps,
  13. refetchData,
  14. modalView,
  15. closeModal,
  16. closeUploadModal,
  17. setUploadErrors,
  18. setBannerErrorCode,
  19. } = props;
  20. const [albumName, setAlbumName] = useState('');
  21. const handleChange = (event) => {
  22. setAlbumName(event.target.value);
  23. };
  24. useEffect(() => {
  25. if (acceptedFiles == null) {
  26. return;
  27. }
  28. let commonPathPrefix: string = (() => {
  29. const paths: string[] = acceptedFiles.map((files) => files.path);
  30. paths.sort();
  31. let firstPath = paths[0],
  32. lastPath = paths[paths.length - 1],
  33. L = firstPath.length,
  34. i = 0;
  35. while (i < L && firstPath.charAt(i) === lastPath.charAt(i)) i++;
  36. return firstPath.substring(0, i);
  37. })();
  38. if (commonPathPrefix) {
  39. commonPathPrefix = commonPathPrefix.substr(
  40. 1,
  41. commonPathPrefix.lastIndexOf('/') - 1
  42. );
  43. }
  44. setAlbumName(commonPathPrefix);
  45. }, [acceptedFiles]);
  46. const handleSubmit = async (event) => {
  47. try {
  48. const token = getToken();
  49. event.preventDefault();
  50. closeModal();
  51. closeUploadModal();
  52. const collection = await createAlbum(albumName);
  53. const collectionAndItsLatestFile: CollectionAndItsLatestFile = {
  54. collection,
  55. file: null,
  56. };
  57. progressBarProps.setPercentComplete(0);
  58. setProgressView(true);
  59. await UploadService.uploadFiles(
  60. acceptedFiles,
  61. collectionAndItsLatestFile,
  62. token,
  63. progressBarProps,
  64. setUploadErrors
  65. );
  66. refetchData();
  67. } catch (err) {
  68. setBannerErrorCode(err.message);
  69. setProgressView(false);
  70. }
  71. };
  72. return (
  73. <Modal
  74. show={modalView}
  75. onHide={closeModal}
  76. centered
  77. backdrop="static"
  78. style={{ background: 'rgba(0, 0, 0, 0.8)' }}
  79. >
  80. <Modal.Header closeButton>
  81. <Modal.Title>{constants.CREATE_COLLECTION}</Modal.Title>
  82. </Modal.Header>
  83. <Modal.Body>
  84. <Form onSubmit={handleSubmit}>
  85. <Form.Group controlId="formBasicEmail">
  86. <Form.Control
  87. type="text"
  88. placeholder={constants.ALBUM_NAME}
  89. value={albumName}
  90. onChange={handleChange}
  91. />
  92. </Form.Group>
  93. <Button
  94. variant="primary"
  95. type="submit"
  96. style={{ width: '100%' }}
  97. >
  98. {constants.CREATE}
  99. </Button>
  100. </Form>
  101. </Modal.Body>
  102. </Modal>
  103. );
  104. }