CreatePartitionDialog.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { FC, useMemo, useState, useContext } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { rootContext } from '../../context/Root';
  5. import DialogTemplate from '../../components/customDialog/DialogTemplate';
  6. import CustomInput from '../../components/customInput/CustomInput';
  7. import { ITextfieldConfig } from '../../components/customInput/Types';
  8. import { useFormValidation } from '../../hooks/Form';
  9. import { formatForm } from '../../utils/Form';
  10. import { PartitionCreateProps } from './Types';
  11. import { PartitionManageParam } from '../partitions/Types';
  12. import { ManageRequestMethods } from '../../types/Common';
  13. import { PartitionHttp } from '../../http/Partition';
  14. const useStyles = makeStyles((theme: Theme) => ({
  15. input: {
  16. margin: theme.spacing(3, 0, 0.5),
  17. },
  18. }));
  19. const CreatePartition: FC<PartitionCreateProps> = ({
  20. onCreate,
  21. collectionName,
  22. }) => {
  23. const classes = useStyles();
  24. const { handleCloseDialog } = useContext(rootContext);
  25. const { t: partitionTrans } = useTranslation('partition');
  26. const { t: btnTrans } = useTranslation('btn');
  27. const { t: warningTrans } = useTranslation('warning');
  28. const [form, setForm] = useState<{ name: string }>({
  29. name: '',
  30. });
  31. const checkedForm = useMemo(() => {
  32. const { name } = form;
  33. return formatForm({ name });
  34. }, [form]);
  35. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  36. const handleInputChange = (value: string) => {
  37. setForm({ name: value });
  38. };
  39. const nameInputConfig: ITextfieldConfig = {
  40. label: partitionTrans('name'),
  41. variant: 'filled',
  42. key: 'name',
  43. fullWidth: true,
  44. onChange: handleInputChange,
  45. className: classes.input,
  46. validations: [
  47. {
  48. rule: 'require',
  49. errorText: warningTrans('required', { name: partitionTrans('name') }),
  50. },
  51. {
  52. rule: 'partitionName',
  53. errorText: partitionTrans('nameWarning'),
  54. },
  55. ],
  56. };
  57. const handleCreatePartition = async () => {
  58. const param: PartitionManageParam = {
  59. partitionName: form.name,
  60. collectionName: collectionName,
  61. type: ManageRequestMethods.CREATE,
  62. };
  63. await PartitionHttp.managePartition(param);
  64. onCreate && onCreate();
  65. handleCloseDialog();
  66. };
  67. const handleClose = () => {};
  68. return (
  69. <DialogTemplate
  70. title={partitionTrans('createTitle')}
  71. handleClose={handleClose}
  72. confirmLabel={btnTrans('create')}
  73. handleConfirm={handleCreatePartition}
  74. confirmDisabled={disabled}
  75. >
  76. <CustomInput
  77. type="text"
  78. textConfig={nameInputConfig}
  79. checkValid={checkIsValid}
  80. validInfo={validation}
  81. />
  82. </DialogTemplate>
  83. );
  84. };
  85. export default CreatePartition;