Import.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { FC } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { makeStyles, Theme, Divider, Typography } from '@material-ui/core';
  4. import CustomSelector from '@/components/customSelector/CustomSelector';
  5. import { InsertImportProps } from './Types';
  6. import Uploader from '@/components/uploader/Uploader';
  7. import { INSERT_MAX_SIZE } from '@/consts';
  8. import { parseByte } from '@/utils';
  9. const getStyles = makeStyles((theme: Theme) => ({
  10. tip: {
  11. color: theme.palette.attuGrey.dark,
  12. fontWeight: 500,
  13. marginBottom: theme.spacing(1),
  14. },
  15. selectors: {
  16. '& .selectorWrapper': {
  17. display: 'flex',
  18. justifyContent: 'space-between',
  19. alignItems: 'center',
  20. marginBottom: theme.spacing(3),
  21. '& .selectLabel': {
  22. fontSize: '14px',
  23. lineHeight: '20px',
  24. color: theme.palette.attuDark.main,
  25. },
  26. '& .divider': {
  27. width: '20px',
  28. margin: theme.spacing(0, 4),
  29. backgroundColor: theme.palette.attuGrey.dark,
  30. },
  31. },
  32. '& .selector': {
  33. flexBasis: '40%',
  34. minWidth: '256px',
  35. },
  36. },
  37. uploadWrapper: {
  38. marginTop: theme.spacing(3),
  39. padding: theme.spacing(1),
  40. backgroundColor: '#f9f9f9',
  41. '& .text': {
  42. color: theme.palette.attuGrey.dark,
  43. },
  44. '& .file': {
  45. marginBottom: theme.spacing(1),
  46. },
  47. '& .uploaderWrapper': {
  48. display: 'flex',
  49. alignItems: 'center',
  50. border: '1px solid #e9e9ed',
  51. padding: theme.spacing(1),
  52. backgroundColor: '#fff',
  53. '& .uploader': {
  54. marginRight: theme.spacing(1),
  55. },
  56. },
  57. '& .sampleWrapper': {
  58. '& .sample': {
  59. backgroundColor: '#fff',
  60. padding: theme.spacing(2),
  61. margin: theme.spacing(1, 0),
  62. },
  63. },
  64. '& .title': {
  65. marginTop: theme.spacing(1),
  66. },
  67. '& .noteList': {
  68. marginTop: theme.spacing(1),
  69. paddingLeft: theme.spacing(3),
  70. },
  71. '& .noteItem': {
  72. maxWidth: '560px',
  73. },
  74. },
  75. }));
  76. const InsertImport: FC<InsertImportProps> = ({
  77. collectionOptions,
  78. partitionOptions,
  79. selectedCollection,
  80. selectedPartition,
  81. handleCollectionChange,
  82. handlePartitionChange,
  83. handleUploadedData,
  84. handleUploadFileChange,
  85. fileName,
  86. setFileName,
  87. }) => {
  88. const { t: insertTrans } = useTranslation('insert');
  89. const { t: collectionTrans } = useTranslation('collection');
  90. const { t: partitionTrans } = useTranslation('partition');
  91. const classes = getStyles();
  92. return (
  93. <section>
  94. <Typography className={classes.tip}>
  95. {insertTrans('targetTip')}
  96. </Typography>
  97. <section className={classes.selectors}>
  98. <div className="selectorWrapper">
  99. <CustomSelector
  100. options={collectionOptions}
  101. disabled={collectionOptions.length === 0}
  102. wrapperClass="selector"
  103. labelClass="selectLabel"
  104. value={selectedCollection}
  105. variant="filled"
  106. label={collectionTrans('collection')}
  107. onChange={(e: { target: { value: unknown } }) => {
  108. const collection = e.target.value;
  109. handleCollectionChange &&
  110. handleCollectionChange(collection as string);
  111. }}
  112. />
  113. <Divider classes={{ root: 'divider' }} />
  114. <CustomSelector
  115. options={partitionOptions}
  116. disabled={partitionOptions.length === 0}
  117. wrapperClass="selector"
  118. labelClass="selectLabel"
  119. value={selectedPartition}
  120. variant="filled"
  121. label={partitionTrans('partition')}
  122. onChange={(e: { target: { value: unknown } }) => {
  123. const partition = e.target.value;
  124. handlePartitionChange(partition as string);
  125. }}
  126. />
  127. </div>
  128. </section>
  129. <div className={classes.uploadWrapper}>
  130. <Typography className="text file" variant="body1">
  131. {insertTrans('file')}
  132. </Typography>
  133. <div className="uploaderWrapper">
  134. <Uploader
  135. btnClass="uploader"
  136. label={insertTrans('uploaderLabel')}
  137. accept=".csv,.json"
  138. // selected collection will affect schema, which is required for uploaded data validation check
  139. // so upload file should be disabled until user select one collection
  140. disabled={!selectedCollection}
  141. disableTooltip={insertTrans('uploadFileDisableTooltip')}
  142. setFileName={setFileName}
  143. handleUploadedData={handleUploadedData}
  144. maxSize={parseByte(`${INSERT_MAX_SIZE}m`)}
  145. overSizeWarning={insertTrans('overSizeWarning', {
  146. size: INSERT_MAX_SIZE,
  147. })}
  148. handleUploadFileChange={handleUploadFileChange}
  149. />
  150. <Typography className="text">
  151. {fileName || insertTrans('fileNamePlaceHolder')}
  152. </Typography>
  153. </div>
  154. <Typography variant="body2" className="text title">
  155. {insertTrans('noteTitle')}
  156. </Typography>
  157. <ul className="noteList">
  158. {insertTrans('notes').map((note: string) => (
  159. <li key={note} className="text noteItem">
  160. <Typography>{note}</Typography>
  161. </li>
  162. ))}
  163. </ul>
  164. </div>
  165. </section>
  166. );
  167. };
  168. export default InsertImport;