ReleaseCollectionDialog.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { useContext, useState } from 'react';
  2. import { Typography, Theme } from '@mui/material';
  3. import { useTranslation } from 'react-i18next';
  4. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  5. import { rootContext, dataContext } from '@/context';
  6. import { makeStyles } from '@mui/styles';
  7. import type { CollectionObject } from '@server/types';
  8. const useStyles = makeStyles((theme: Theme) => ({
  9. desc: {
  10. margin: '8px 0 16px 0',
  11. maxWidth: 480,
  12. color: theme.palette.text.secondary,
  13. },
  14. }));
  15. const ReleaseCollectionDialog = (props: {
  16. collection: CollectionObject;
  17. onRelease?: (collection: CollectionObject) => void;
  18. }) => {
  19. const { releaseCollection } = useContext(dataContext);
  20. const classes = useStyles();
  21. const { collection, onRelease } = props;
  22. const { t: dialogTrans } = useTranslation('dialog');
  23. const { t: btnTrans } = useTranslation('btn');
  24. const { t: successTrans } = useTranslation('success');
  25. const { t: collectionTrans } = useTranslation('collection');
  26. const { handleCloseDialog, openSnackBar } = useContext(rootContext);
  27. const [disabled, setDisabled] = useState(false);
  28. // confirm action
  29. const handleConfirm = async () => {
  30. // disable confirm button
  31. setDisabled(true);
  32. try {
  33. // release collection
  34. await releaseCollection(collection.collection_name);
  35. // show success message
  36. openSnackBar(
  37. successTrans('release', {
  38. name: collectionTrans('collection'),
  39. })
  40. );
  41. // execute callback
  42. onRelease && (await onRelease(collection));
  43. // enable confirm button
  44. setDisabled(false);
  45. // close dialog
  46. handleCloseDialog();
  47. } finally {
  48. // enable confirm button
  49. setDisabled(false);
  50. }
  51. };
  52. return (
  53. <DialogTemplate
  54. title={dialogTrans('releaseTitle', {
  55. type: collection.collection_name,
  56. })}
  57. handleClose={handleCloseDialog}
  58. children={
  59. <>
  60. <Typography variant="body1" component="p" className={classes.desc}>
  61. {dialogTrans('releaseContent', {
  62. type: collection.collection_name,
  63. })}
  64. </Typography>
  65. </>
  66. }
  67. confirmLabel={btnTrans('release')}
  68. handleConfirm={handleConfirm}
  69. confirmDisabled={disabled}
  70. />
  71. );
  72. };
  73. export default ReleaseCollectionDialog;