EditPropertyDialog.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { FC, useContext, useMemo, useState } from 'react';
  2. import { Typography, Theme } from '@mui/material';
  3. import { useTranslation } from 'react-i18next';
  4. import { rootContext, dataContext } from '@/context';
  5. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  6. import CustomInput from '@/components/customInput/CustomInput';
  7. import { formatForm } from '@/utils';
  8. import { IForm, useFormValidation } from '@/hooks';
  9. import { ITextfieldConfig } from '@/components/customInput/Types';
  10. import { Property } from '@/consts';
  11. import { makeStyles } from '@mui/styles';
  12. import { DatabaseService } from '@/http';
  13. import type { CollectionObject } from '@server/types';
  14. const useStyles = makeStyles((theme: Theme) => ({
  15. desc: {
  16. margin: '8px 0 16px 0',
  17. },
  18. }));
  19. export interface EditPropertyProps {
  20. target: CollectionObject | string;
  21. type: 'collection' | 'database';
  22. property: Property;
  23. cb?: (target: CollectionObject | string) => void;
  24. }
  25. const EditPropertyDialog: FC<EditPropertyProps> = props => {
  26. const { setCollectionProperty } = useContext(dataContext);
  27. const { handleCloseDialog } = useContext(rootContext);
  28. const { cb, target, property } = props;
  29. const [form, setForm] = useState<IForm>({
  30. key: 'property',
  31. value: property.value,
  32. });
  33. const classes = useStyles();
  34. const checkedForm = useMemo(() => {
  35. return formatForm(form);
  36. }, [form]);
  37. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  38. const { t: dialogTrans } = useTranslation('dialog');
  39. const { t: warningTrans } = useTranslation('warning');
  40. const { t: btnTrans } = useTranslation('btn');
  41. const handleInputChange = (value: string) => {
  42. setForm({ ...form, key: 'property', value });
  43. };
  44. const handleConfirm = async () => {
  45. let value: unknown = '';
  46. if (form.value !== '') {
  47. switch (property.type) {
  48. case 'number':
  49. value = Number(form.value);
  50. break;
  51. case 'boolean':
  52. value = form.value === 'true';
  53. break;
  54. default:
  55. value = form.value;
  56. }
  57. }
  58. switch (props.type) {
  59. case 'collection':
  60. await setCollectionProperty(
  61. (target as CollectionObject).collection_name,
  62. property.key,
  63. value
  64. );
  65. break;
  66. case 'database':
  67. await DatabaseService.setProperty({
  68. db_name: target as string,
  69. properties: { [property.key]: value },
  70. });
  71. break;
  72. }
  73. handleCloseDialog();
  74. cb && (await cb(target));
  75. };
  76. const propertyInputConfig: ITextfieldConfig = {
  77. label: dialogTrans('value'),
  78. key: 'value',
  79. onChange: handleInputChange,
  80. variant: 'filled',
  81. placeholder: '',
  82. fullWidth: true,
  83. validations: [
  84. {
  85. rule: property.type === 'number' ? 'number' : 'bool',
  86. errorText:
  87. property.type === 'number'
  88. ? warningTrans('integer', { name: dialogTrans('value') })
  89. : warningTrans('bool', { name: dialogTrans('value') }),
  90. },
  91. ],
  92. defaultValue: form.value,
  93. };
  94. return (
  95. <DialogTemplate
  96. title={dialogTrans('editPropertyTitle')}
  97. handleClose={handleCloseDialog}
  98. children={
  99. <>
  100. <Typography variant="body1" component="p" className={classes.desc}>
  101. <code>
  102. <b>{property.key}</b>
  103. </code>
  104. </Typography>
  105. <CustomInput
  106. type="text"
  107. textConfig={propertyInputConfig}
  108. checkValid={checkIsValid}
  109. validInfo={validation}
  110. />
  111. </>
  112. }
  113. confirmLabel={btnTrans('confirm')}
  114. handleConfirm={handleConfirm}
  115. confirmDisabled={disabled}
  116. />
  117. );
  118. };
  119. export default EditPropertyDialog;