ReleaseCollectionDialog.tsx 2.0 KB

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