ImportSampleDialog.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { makeStyles, Theme, Typography } from '@material-ui/core';
  2. import { FC, useState, useContext } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  5. import CustomSelector from '@/components/customSelector/CustomSelector';
  6. import { rootContext } from '@/context';
  7. import { InsertStatusEnum } from './insert/Types';
  8. import { CollectionHttp } from '@/http/Collection';
  9. import { MilvusHttp } from '@/http/Milvus';
  10. import { LoadSampleParam } from './Types';
  11. const getStyles = makeStyles((theme: Theme) => {
  12. return {
  13. icon: {
  14. fontSize: '16px',
  15. },
  16. selectors: {
  17. '& .selectorWrapper': {
  18. display: 'flex',
  19. flexDirection: 'column',
  20. marginBottom: theme.spacing(2),
  21. '& .selectLabel': {
  22. fontSize: '14px',
  23. lineHeight: '20px',
  24. color: theme.palette.attuDark.main,
  25. },
  26. '& .description': {
  27. color: theme.palette.attuGrey.dark,
  28. marginBottom: theme.spacing(1),
  29. fontSize: 12,
  30. },
  31. },
  32. '& .selector': {
  33. minWidth: '128px',
  34. },
  35. },
  36. };
  37. });
  38. const ImportSampleDialog: FC<{ collection: string }> = props => {
  39. const classes = getStyles();
  40. const [size, setSize] = useState<string>('100');
  41. const [insertStatus, setInsertStatus] = useState<InsertStatusEnum>(
  42. InsertStatusEnum.init
  43. );
  44. const { t: insertTrans } = useTranslation('insert');
  45. const { t: btnTrans } = useTranslation('btn');
  46. const { handleCloseDialog, openSnackBar } = useContext(rootContext);
  47. // selected collection name
  48. const sizeOptions = [
  49. {
  50. label: '100',
  51. value: '100',
  52. },
  53. {
  54. label: '1k',
  55. value: '1000',
  56. },
  57. {
  58. label: '10k',
  59. value: '10000',
  60. },
  61. {
  62. label: '50k',
  63. value: '50000',
  64. },
  65. ];
  66. const handleImportSample = async (
  67. collectionName: string,
  68. size: string
  69. ): Promise<{ result: boolean; msg: string }> => {
  70. const param: LoadSampleParam = {
  71. collection_name: collectionName,
  72. size: size,
  73. };
  74. try {
  75. await CollectionHttp.importSample(collectionName, param);
  76. await MilvusHttp.flush(collectionName);
  77. return { result: true, msg: '' };
  78. } catch (err: any) {
  79. const {
  80. response: {
  81. data: { message },
  82. },
  83. } = err;
  84. return { result: false, msg: message || '' };
  85. }
  86. };
  87. const handleNext = async () => {
  88. if (insertStatus === InsertStatusEnum.success) {
  89. handleCloseDialog();
  90. return;
  91. }
  92. // start loading
  93. setInsertStatus(InsertStatusEnum.loading);
  94. const { result, msg } = await handleImportSample(props.collection, size);
  95. if (!result) {
  96. openSnackBar(msg, 'error');
  97. setInsertStatus(InsertStatusEnum.init);
  98. return;
  99. }
  100. setInsertStatus(InsertStatusEnum.success);
  101. // hide dialog
  102. handleCloseDialog();
  103. };
  104. return (
  105. <DialogTemplate
  106. title={insertTrans('importSampleData', {
  107. collection: props.collection,
  108. })}
  109. handleClose={handleCloseDialog}
  110. confirmLabel={
  111. insertStatus === InsertStatusEnum.init
  112. ? btnTrans('import')
  113. : insertStatus === InsertStatusEnum.loading
  114. ? btnTrans('importing')
  115. : insertStatus === InsertStatusEnum.success
  116. ? btnTrans('done')
  117. : insertStatus
  118. }
  119. handleConfirm={handleNext}
  120. confirmDisabled={insertStatus === InsertStatusEnum.loading}
  121. showActions={true}
  122. showCancel={false}
  123. // don't show close icon when insert not finish
  124. // showCloseIcon={insertStatus !== InsertStatusEnum.loading}
  125. >
  126. <form className={classes.selectors}>
  127. <div className="selectorWrapper">
  128. <div className="description">
  129. <Typography variant="inherit" component="p">
  130. {insertTrans('importSampleDataDesc')}
  131. </Typography>
  132. </div>
  133. <CustomSelector
  134. label={insertTrans('sampleDataSize')}
  135. options={sizeOptions}
  136. wrapperClass="selector"
  137. labelClass="selectLabel"
  138. value={size}
  139. variant="filled"
  140. onChange={(e: { target: { value: unknown } }) => {
  141. const size = e.target.value;
  142. setSize(size as string);
  143. }}
  144. />
  145. </div>
  146. </form>
  147. </DialogTemplate>
  148. );
  149. };
  150. export default ImportSampleDialog;