ResetPropertyDialog.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { FC, useContext } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { rootContext, dataContext } from '@/context';
  4. import DeleteTemplate from '@/components/customDialog/DeleteDialogTemplate';
  5. import { Property } from '@/consts';
  6. import { DatabaseService, CollectionService } from '@/http';
  7. import type { CollectionObject } from '@server/types';
  8. export interface EditPropertyProps {
  9. target: CollectionObject | string;
  10. type: 'collection' | 'database';
  11. property: Property;
  12. cb?: (target: CollectionObject | string) => void;
  13. }
  14. const ResetPropertyDialog: FC<EditPropertyProps> = props => {
  15. // context
  16. const { fetchCollection } = useContext(dataContext);
  17. const { handleCloseDialog } = useContext(rootContext);
  18. // props
  19. const { cb, target, type, property } = props;
  20. // UI handlers
  21. const handleDelete = async () => {
  22. switch (type) {
  23. case 'collection':
  24. const collection = target as CollectionObject;
  25. if (!collection || !collection.schema) {
  26. return;
  27. }
  28. // Reset the property in the collection schema
  29. await CollectionService.setProperty(collection.collection_name, {
  30. [property.key]: '',
  31. });
  32. // Refresh the collection to get the latest schema
  33. await fetchCollection((target as CollectionObject).collection_name);
  34. break;
  35. case 'database':
  36. await DatabaseService.setProperty({
  37. db_name: target as string,
  38. properties: { [property.key]: '' },
  39. });
  40. break;
  41. }
  42. handleCloseDialog();
  43. cb && (await cb(target));
  44. };
  45. // i18n
  46. const { t: btnTrans } = useTranslation('btn');
  47. const { t: dialogTrans } = useTranslation('dialog');
  48. return (
  49. <DeleteTemplate
  50. label={btnTrans('reset')}
  51. title={dialogTrans('resetPropertyTitle')}
  52. text={dialogTrans('resetPropertyInfo')}
  53. compare={property.key}
  54. handleDelete={handleDelete}
  55. />
  56. );
  57. };
  58. export default ResetPropertyDialog;