Advertisement
deddyprianto

react jsx voucher

Feb 8th, 2023
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useState } from 'react';
  2. import Paper from '@mui/material/Paper';
  3. import Button from '@mui/material/Button';
  4. import CardGiftcardIcon from '@mui/icons-material/CardGiftcard';
  5. import Typography from '@mui/material/Typography';
  6. import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos';
  7. import { isEmptyArray } from 'helpers/CheckEmpty';
  8. import { useHistory } from 'react-router-dom';
  9. import Swal from 'sweetalert2';
  10. import { useSelector, useDispatch } from 'react-redux';
  11. import IconButton from '@mui/material/IconButton';
  12. import CloseIcon from '@mui/icons-material/Close';
  13. import { PaymentAction } from 'redux/actions/PaymentAction';
  14. import { CustomerAction } from 'redux/actions/CustomerAction';
  15.  
  16. const RenderVoucher = ({ handleRemoveVoucher, isDeletingVoucher }) => {
  17.   const history = useHistory();
  18.   const dispatch = useDispatch();
  19.   const [isFilteredVoucher, setIsFilteredVoucher] = useState(false);
  20.   const [myVouchers, setMyVouchers] = useState([]);
  21.  
  22.   const color = useSelector((state) => state.theme.color);
  23.   const selectedVouchers = useSelector(
  24.     (state) => state.payment.selectedVoucher
  25.   );
  26.   const dataVoucher = useSelector((state) => state.voucher.indexVoucer);
  27.   const basket = useSelector((state) => state.order.basket);
  28.  
  29.   useEffect(() => {
  30.     const loadData = async () => {
  31.       const vouchers = await dispatch(CustomerAction.getVoucher());
  32.       setMyVouchers(vouchers.data);
  33.     };
  34.     loadData();
  35.   }, []);
  36.  
  37.   const calculateVoucher = async () => {
  38.     const payload = {
  39.       details: basket?.details,
  40.       outletId: basket?.outletID,
  41.       total: basket?.totalNettAmount,
  42.       customerId: basket?.customerId,
  43.       payments: selectedVouchers.map((item) => ({
  44.         isVoucher: item.isVoucher,
  45.         serialNumber: item.serialNumber,
  46.         voucherId: item.voucherId,
  47.       })),
  48.     };
  49.     Swal.fire({
  50.       title: 'Please Wait !',
  51.       html: 'Voucher will be applied',
  52.       allowOutsideClick: false,
  53.       onBeforeOpen: () => {
  54.         Swal.showLoading();
  55.       },
  56.     });
  57.  
  58.     const dataVoucher = await dispatch(PaymentAction.calculateVoucher(payload));
  59.     const isVoucherCannotApplied = dataVoucher.data.message;
  60.  
  61.     if (isVoucherCannotApplied) {
  62.       setIsFilteredVoucher(true);
  63.       const filterSelectedVoucher = selectedVouchers.filter((itemData) => {
  64.         const isSerialNumber = dataVoucher?.data.payments.some(
  65.           (item) => item.serialNumber === itemData.serialNumber
  66.         );
  67.         if (isSerialNumber) {
  68.           return itemData;
  69.         }
  70.       });
  71.  
  72.       dispatch(PaymentAction.setData(filterSelectedVoucher, 'SELECT_VOUCHER'));
  73.       dispatch({ type: 'INDEX_VOUCHER', payload: dataVoucher.data });
  74.     }
  75.  
  76.     if (isVoucherCannotApplied) {
  77.       Swal.fire({
  78.         icon: 'info',
  79.         title: dataVoucher.data.message,
  80.         allowOutsideClick: false,
  81.         confirmButtonText: 'OK',
  82.         confirmButtonColor: color.primary,
  83.       });
  84.     } else {
  85.       Swal.fire({
  86.         icon: 'success',
  87.         title: 'Success',
  88.         text: 'successfully applied the voucher!',
  89.         confirmButtonColor: color.primary,
  90.       });
  91.     }
  92.   };
  93.  
  94.   useEffect(() => {
  95.     if (
  96.       !isEmptyArray(selectedVouchers) &&
  97.       !isDeletingVoucher &&
  98.       !isFilteredVoucher
  99.     ) {
  100.       calculateVoucher();
  101.     }
  102.   }, [selectedVouchers]);
  103.  
  104.   const styles = {
  105.     paperVoucher: {
  106.       width: '100%',
  107.       marginBottom: 10,
  108.       padding: 10,
  109.       backgroundColor: color.background,
  110.     },
  111.     buttonVoucher: {
  112.       width: '100%',
  113.       height: 40,
  114.       display: 'flex',
  115.       alignItems: 'center',
  116.       justifyContent: 'space-between',
  117.     },
  118.     displayFlexAndAlignCenter: { display: 'flex', alignItems: 'center' },
  119.     icon: {
  120.       color: color.primary,
  121.       fontSize: 16,
  122.       marginRight: 5,
  123.     },
  124.     typography: {
  125.       color: color.primary,
  126.       fontSize: 13,
  127.       textTransform: 'none',
  128.       fontWeight: 'bold',
  129.     },
  130.     iconArrow: {
  131.       fontSize: 13,
  132.       color: color.primary,
  133.     },
  134.   };
  135.  
  136.   const handleSelectVoucher = () => {
  137.     if (
  138.       !isEmptyArray(selectedVouchers) &&
  139.       !selectedVouchers[0]?.applyToLowestItem
  140.     ) {
  141.       Swal.fire({
  142.         icon: 'error',
  143.         title: 'This voucher cannot use multiple voucher',
  144.         allowOutsideClick: false,
  145.         confirmButtonText: 'OK',
  146.         confirmButtonColor: color.primary,
  147.       });
  148.     } else if (
  149.       !isEmptyArray(selectedVouchers) &&
  150.       selectedVouchers[0]?.cannotBeMixed
  151.     ) {
  152.       Swal.fire({
  153.         icon: 'error',
  154.         title: 'This voucher cannot be mixed with other voucher',
  155.         allowOutsideClick: false,
  156.         confirmButtonText: 'Switch to another voucher',
  157.         confirmButtonColor: color.primary,
  158.         showCancelButton: true,
  159.       });
  160.     } else {
  161.       history.push('/my-voucher');
  162.     }
  163.   };
  164.  
  165.   const renderSelectedVoucher = () => {
  166.     if (!isEmptyArray(dataVoucher?.payments)) {
  167.       return dataVoucher?.payments?.map((selectedVoucher, index) => {
  168.         return (
  169.           <div key={index}>
  170.             <div style={styles.buttonVoucher} variant='outlined'>
  171.               <Typography style={styles.typography}>
  172.                 {selectedVoucher.voucherName}
  173.               </Typography>
  174.  
  175.               <IconButton
  176.                 onClick={() => {
  177.                   handleRemoveVoucher(selectedVoucher.serialNumber);
  178.                 }}
  179.               >
  180.                 <CloseIcon style={styles.iconRemoveVoucherSelected} />
  181.               </IconButton>
  182.             </div>
  183.             <div style={styles.divider} />
  184.           </div>
  185.         );
  186.       });
  187.     }
  188.   };
  189.  
  190.   return (
  191.     <React.Fragment>
  192.       {!isEmptyArray(myVouchers) && (
  193.         <Paper variant='outlined' style={styles.paperVoucher}>
  194.           <Button
  195.             style={styles.buttonVoucher}
  196.             variant='outlined'
  197.             // disabled={handleDisableButton()}
  198.             onClick={() => {
  199.               handleSelectVoucher();
  200.             }}
  201.           >
  202.             <div style={styles.displayFlexAndAlignCenter}>
  203.               <CardGiftcardIcon style={styles.icon} />
  204.               <Typography style={styles.typography}>Use Voucher</Typography>
  205.             </div>
  206.             <ArrowForwardIosIcon style={styles.iconArrow} />
  207.           </Button>
  208.  
  209.           {renderSelectedVoucher()}
  210.         </Paper>
  211.       )}
  212.     </React.Fragment>
  213.   );
  214. };
  215.  
  216. export default RenderVoucher;
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement