Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component, ChangeEvent, KeyboardEvent, CSSProperties } from 'react';
  2. import { WithStyles, Snackbar } from '@material-ui/core';
  3. import classNames from 'classnames';
  4. import Button from '@material-ui/core/Button';
  5. import Paper from '@material-ui/core/Paper';
  6. // import TextField from '@material-ui/core/TextField';
  7. import Scrollbars from 'react-custom-scrollbars';
  8. import { Theme, withStyles } from '@material-ui/core/styles';
  9. // import { BigAvatar } from 'src/Component/Avatar';
  10. import { Query, MutationFn, Mutation } from 'react-apollo';
  11. import { GET_ME } from 'src/Graphql/User/query';
  12. import UserInterface from 'src/Interface/UserInterface';
  13. import { DataProxy } from 'apollo-cache';
  14. import { ReturnLink } from '../All/ReturnLink';
  15. import { ACTIVE_MAILER } from 'src/Graphql/mutation';
  16.  
  17. const styles = (theme: Theme): Record<string, CSSProperties | Record<string, CSSProperties>> => ({
  18.   col: {
  19.     boxSizing: 'border-box' as 'border-box',
  20.     display: 'flex',
  21.     flex: '0 1 auto',
  22.     flexDirection: 'column' as 'column',
  23.     msFlex: '0 1 auto',
  24.     WebkitFlex: '0 1 auto',
  25.   },
  26.   row: {
  27.     boxSizing: 'border-box' as 'border-box',
  28.     display: 'flex',
  29.     flex: '0 1 auto',
  30.     flexDirection: 'row' as 'row',
  31.     msFlex: '0 1 auto',
  32.     WebkitFlex: '0 1 auto',
  33.   },
  34.   justifyEnd: {
  35.     justifyContent: 'flex-end',
  36.     padding: '0 44px 44px',
  37.   },
  38.   submitButtonWrapper: {
  39.     marginTop: 0,
  40.   },
  41.   justifyCenter: {
  42.     justifyContent: 'center',
  43.   },
  44.   contentEditEmail: {
  45.     marginTop: 28,
  46.   },
  47.   paper: {
  48.     '@media (max-width: 600px)': {
  49.       width: '100%',
  50.     },
  51.     width: 454,
  52.     margin: '0 auto',
  53.     boxShadow:
  54.       ' 0px 0px 1px 0px rgba(0,0,0,0.2), 0px 0px 0px 0px rgba(0,0,0,.1), 0px 0px 0px 0px rgba(0,0,0,0.12)',
  55.     borderRadius: 0,
  56.     paddingBottom: 20,
  57.     paddingTop: 20,
  58.   },
  59.   inputAvatar: {
  60.     display: 'none',
  61.   },
  62.   wrapper: {
  63.     position: 'relative' as 'relative',
  64.     marginTop: 20,
  65.   },
  66.   buttonProgress: {
  67.     color: theme.customPalette.primary.main,
  68.     position: 'absolute' as 'absolute',
  69.     top: '50%',
  70.     left: '50%',
  71.     marginTop: -8,
  72.     marginLeft: -12,
  73.   },
  74.   contentForm: {
  75.     margin: '14px 0 24px',
  76.     padding: '0 44px',
  77.     '@media (max-width: 600px)': {
  78.       padding: '0 24px',
  79.     },
  80.   },
  81.   textField: {
  82.     width: '100%',
  83.     margin: '20px 0 8px',
  84.     fontWeight: 500,
  85.     fontSize: 60,
  86.   },
  87.   actionButton: {
  88.     color: 'white',
  89.   },
  90.   button: {
  91.     margin: '0px 4px',
  92.     padding: '14px 53px',
  93.     textTransform: 'capitalize' as 'capitalize',
  94.     fontWeight: 400,
  95.     fontSize: 11,
  96.   },
  97.   input: {
  98.     fontSize: 60,
  99.   },
  100.   title: {
  101.     fontSize: 24,
  102.     marginTop: 28,
  103.     fontWeight: 400,
  104.   },
  105. });
  106.  
  107. interface EditEmailProps {
  108.   classes: any;
  109. }
  110. interface EditEmailStates {
  111.   bgColor?: string;
  112.   activate?: boolean;
  113.   textS?: string;
  114.   position?: string;
  115.   isLoading: boolean;
  116.   snackBarMessage?: string;
  117.   username?: string;
  118.   avatar?: string;
  119.   avatarFile?: File;
  120.   avatarName?: string;
  121. }
  122.  
  123. class EditEmail extends Component<EditEmailProps & WithStyles<any>, EditEmailStates> {
  124.   inputFile: any;
  125.   oldUser?: UserInterface;
  126.  
  127.   constructor(props: any) {
  128.     super(props);
  129.     this.state = {
  130.       isLoading: false,
  131.       bgColor: 'green',
  132.       activate: true,
  133.       textS: "Pause Email Notification",
  134.     };
  135.   }
  136.  
  137.   handleKeyPress = (
  138.     mutationDeleteAccount: MutationFn<any, any>,
  139.     evt: any,
  140.   ) => {
  141.     this.handleSubmit(mutationDeleteAccount);
  142.   };
  143.  
  144.   handleCloseSnackbar = () => {
  145.     this.setState({ snackBarMessage: undefined });
  146.   };
  147.  
  148.   preventDefaultKeyPress = (evt: KeyboardEvent<any>) => {
  149.     evt.preventDefault();
  150.   };
  151.  
  152.   handleChangeInput = (evt: ChangeEvent<HTMLInputElement>) => {
  153.     this.setState({ ...this.state, [evt.target.id]: evt.target.value });
  154.   };
  155.  
  156.   showSnackMessage = (snackBarMessage: string) => {
  157.     this.setState({ snackBarMessage });
  158.   };
  159.  
  160.  
  161.   handleSubmit = async (
  162.     mutationEditeMailer: MutationFn<any, any>,
  163.   ) => {
  164.     if (!this.oldUser) {
  165.       return;
  166.     }
  167.  
  168.     this.setState({ isLoading: true });
  169.  
  170.     mutationEditeMailer({
  171.       variables: {
  172.         id: this.oldUser.id,
  173.         mailer: this.oldUser.mailer
  174.       },
  175.     })
  176.       .then(() => {
  177.         this.showSnackMessage('Successfully modified');
  178.       })
  179.       .catch(err => {
  180.         this.showSnackMessage('An error occurred');
  181.       })
  182.       .finally(() => {
  183.         this.setState({ isLoading: false });
  184.       });
  185.   };
  186.  
  187.   updateCache = (cache: DataProxy, EditEmailResult: any) => {
  188.     const data = cache.readQuery<{ me: UserInterface }>({ query: GET_ME });
  189.     if (data) {
  190.       const newUser: UserInterface = EditEmailResult.data.EditEmail;
  191.       data.me = { ...data.me, ...newUser };
  192.       cache.writeQuery({
  193.         query: GET_ME,
  194.         data,
  195.       });
  196.     }
  197.   };
  198.  
  199.  
  200.   render() {
  201.     const { classes } = this.props;
  202.     const { isLoading, snackBarMessage } = this.state;
  203.     return (
  204.       <Query query={GET_ME}>
  205.         {({ data, loading, error }) => {
  206.           if (loading) {
  207.             return 'Loading...';
  208.           }
  209.           if (error) {
  210.             return 'Error';
  211.           }
  212.           this.oldUser = data.me;
  213.           if (this.oldUser !== undefined) {
  214.             return (
  215.               <Mutation mutation={ACTIVE_MAILER} update={this.updateCache}>
  216.                 {mutationActiveEmail => (
  217.                   <Scrollbars
  218.                     style={{ height: '100vh' }}
  219.                     autoHide={true}
  220.                     autoHideTimeout={500}
  221.                     autoHideDuration={50}
  222.                   >
  223.                     <Snackbar
  224.                       open={!!snackBarMessage}
  225.                       message={snackBarMessage}
  226.                       onClose={this.handleCloseSnackbar}
  227.                       autoHideDuration={5000}
  228.                     />
  229.                     <div className={classes.contentEditEmail}>
  230.                       <ReturnLink />
  231.                       <Paper className={classes.paper} elevation={1}>
  232.  
  233.                         <form
  234.                           className={classes.contentForm}
  235.                         >
  236.                           <div className={classNames(
  237.                             classes.row,
  238.                             classes.submitButtonWrapper,
  239.                             classes.justifyCenter,
  240.                           )}>
  241.                             <Button
  242.                               className={classNames(classes.actionButton)}
  243.                               variant="flat"
  244.                               size="small"
  245.                               onClick={this.handleKeyPress.bind(
  246.                                 this,
  247.                                 mutationActiveEmail,
  248.                               )}
  249.                               style={{ backgroundColor: this.oldUser ? this.oldUser.mailer ? 'red' : 'green' : 'green' }}
  250.                               id="btn-save"
  251.  
  252.                               disabled={isLoading}
  253.                             >
  254.                               {this.state.textS}
  255.                             </Button>
  256.                           </div>
  257.  
  258.                         </form>
  259.  
  260.  
  261.  
  262.                       </Paper>
  263.                     </div>
  264.                   </Scrollbars>
  265.                 )}
  266.               </Mutation>
  267.             );
  268.           }
  269.           else { return null; }
  270.         }}
  271.       </Query>
  272.     );
  273.   }
  274. }
  275.  
  276. export default withStyles(styles as any)(EditEmail);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement