Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import clsx from 'clsx';
  3. import { makeStyles } from '@material-ui/core/styles';
  4. import CircularProgress from '@material-ui/core/CircularProgress';
  5. import { green } from '@material-ui/core/colors';
  6. import Button from '@material-ui/core/Button';
  7. import Fab from '@material-ui/core/Fab';
  8. import CheckIcon from '@material-ui/icons/Check';
  9. import SaveIcon from '@material-ui/icons/Save';
  10.  
  11. const useStyles = makeStyles(theme => ({
  12.   root: {
  13.     display: 'flex',
  14.     alignItems: 'center',
  15.   },
  16.   wrapper: {
  17.     margin: theme.spacing(1),
  18.     position: 'relative',
  19.   },
  20.   buttonSuccess: {
  21.     backgroundColor: green[500],
  22.     '&:hover': {
  23.       backgroundColor: green[700],
  24.     },
  25.   },
  26.   fabProgress: {
  27.     color: green[500],
  28.     position: 'absolute',
  29.     top: -6,
  30.     left: -6,
  31.     zIndex: 1,
  32.   },
  33.   buttonProgress: {
  34.     color: green[500],
  35.     position: 'absolute',
  36.     top: '50%',
  37.     left: '50%',
  38.     marginTop: -12,
  39.     marginLeft: -12,
  40.   },
  41. }));
  42.  
  43. export default function CircularIntegration() {
  44.   const classes = useStyles();
  45.   const [loading, setLoading] = React.useState(true);
  46.   const [success, setSuccess] = React.useState(false);
  47.   const [completed, setCompleted] = React.useState(0);
  48.   const timer = React.useRef();
  49.  
  50.   const buttonClassname = clsx({
  51.     [classes.buttonSuccess]: success,
  52.   });
  53.  
  54.   React.useEffect(() => {
  55.     function progress() {
  56.       setCompleted(prevCompleted => (prevCompleted >= 100 ? 0 : prevCompleted + 10));
  57.     }
  58.  
  59.     const timer = setInterval(progress, 1000);
  60.     return () => {
  61.       clearInterval(timer);
  62.     };
  63.   }, []);
  64.  
  65.   React.useEffect(() => {
  66.     return () => {
  67.       clearTimeout(timer.current);
  68.     };
  69.   }, []);
  70.  
  71.   const handleButtonClick = () => {
  72.     if (!loading) {
  73.       setSuccess(false);
  74.       setLoading(true);
  75.       timer.current = setTimeout(() => {
  76.         setSuccess(true);
  77.         setLoading(false);
  78.       }, 2000);
  79.     }
  80.   };
  81.  
  82.   return (
  83.     <div className={classes.root}>
  84.       <div className={classes.wrapper}>
  85.         <Fab
  86.           aria-label="save"
  87.           color="primary"
  88.           className={buttonClassname}
  89.           onClick={handleButtonClick}
  90.         >
  91.           {success ? <CheckIcon /> : <SaveIcon />}
  92.         </Fab>
  93.         {loading && <CircularProgress size={68} className={classes.fabProgress} />}
  94.       </div>
  95.       <div className={classes.wrapper}>
  96.         {/* <Button
  97.           variant="contained"
  98.           color="primary"
  99.           className={buttonClassname}
  100.           disabled={loading}
  101.           onClick={handleButtonClick}
  102.         >
  103.           Accept terms
  104.         </Button> */}
  105.         <CircularProgress variant="static" value={completed}></CircularProgress>
  106.         {loading && <CircularProgress size={24} className={classes.buttonProgress} />}
  107.       </div>
  108.     </div>
  109.   );
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement