ReleaseCollectionDialog.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { useContext, useState } from 'react';
  2. import { Typography, makeStyles, Theme } from '@material-ui/core';
  3. import { useTranslation } from 'react-i18next';
  4. import { CollectionHttp } from '@/http/Collection';
  5. import { rootContext } from '@/context/Root';
  6. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  7. const useStyles = makeStyles((theme: Theme) => ({
  8. desc: {
  9. margin: '8px 0 16px 0',
  10. maxWidth: 480,
  11. },
  12. }));
  13. const ReleaseCollectionDialog = (props: any) => {
  14. const classes = useStyles();
  15. const { collection, onRelease } = props;
  16. const { t: dialogTrans } = useTranslation('dialog');
  17. const { t: btnTrans } = useTranslation('btn');
  18. const { handleCloseDialog } = useContext(rootContext);
  19. const [disabled, setDisabled] = useState(false);
  20. // confirm action
  21. const handleConfirm = async () => {
  22. // disable confirm button
  23. setDisabled(true);
  24. try {
  25. // release collection
  26. await CollectionHttp.releaseCollection(collection);
  27. // execute callback
  28. onRelease && onRelease();
  29. // enable confirm button
  30. setDisabled(false);
  31. // close dialog
  32. handleCloseDialog();
  33. } finally {
  34. // enable confirm button
  35. setDisabled(false);
  36. }
  37. };
  38. return (
  39. <DialogTemplate
  40. title={dialogTrans('releaseTitle', {
  41. type: collection,
  42. })}
  43. handleClose={handleCloseDialog}
  44. children={
  45. <>
  46. <Typography variant="body1" component="p" className={classes.desc}>
  47. {dialogTrans('releaseContent', { type: collection })}
  48. </Typography>
  49. </>
  50. }
  51. confirmLabel={btnTrans('release')}
  52. handleConfirm={handleConfirm}
  53. confirmDisabled={disabled}
  54. />
  55. );
  56. };
  57. export default ReleaseCollectionDialog;