|
| 1 | +/* eslint-disable @typescript-eslint/restrict-template-expressions */ |
| 2 | +import { Button } from 'aws-amplify-react'; |
| 3 | +import { ApolloError } from '@apollo/client'; |
| 4 | +import { CircularProgress } from '@material-ui/core'; |
| 5 | +import { |
| 6 | + FulfillMutation, |
| 7 | + ReceiptInfoFragment, |
| 8 | + useFulfillMutation, |
| 9 | +} from '../../__generated__/types'; |
| 10 | +import ReceiptMenu from './SimplyMenu'; |
| 11 | + |
| 12 | +type Props = { |
| 13 | + purchase: ReceiptInfoFragment; |
| 14 | + displayStudentName: boolean; |
| 15 | + displayListingName: boolean; |
| 16 | + editPurchase: any; |
| 17 | +}; |
| 18 | + |
| 19 | +function determineMiddleText(displayListingName: boolean, displayStudentName: boolean) { |
| 20 | + if (displayListingName && displayStudentName) { |
| 21 | + return ' purchased '; |
| 22 | + } |
| 23 | + if (displayListingName) { |
| 24 | + return 'Purchased '; |
| 25 | + } |
| 26 | + return ''; |
| 27 | +} |
| 28 | + |
| 29 | +export function PurchaseCard({ |
| 30 | + purchase, |
| 31 | + displayListingName, |
| 32 | + displayStudentName, |
| 33 | + editPurchase, |
| 34 | +}: Props) { |
| 35 | + const middleText = determineMiddleText(displayListingName, displayStudentName); |
| 36 | + |
| 37 | + const onMutationCompleted = (data: FulfillMutation) => { |
| 38 | + console.log('Mutation Completed'); |
| 39 | + editPurchase(data.fulfillPurchase); |
| 40 | + }; |
| 41 | + |
| 42 | + const onMutationError = (e: ApolloError) => { |
| 43 | + console.log(e.message); |
| 44 | + }; |
| 45 | + |
| 46 | + const [fulfill, { loading: mutationLoading }] = useFulfillMutation({ |
| 47 | + onCompleted: onMutationCompleted, |
| 48 | + onError: onMutationError, |
| 49 | + }); |
| 50 | + |
| 51 | + const onClickFulfill = () => { |
| 52 | + fulfill({ |
| 53 | + variables: { |
| 54 | + fulfilled: true, |
| 55 | + course: purchase.course, |
| 56 | + receiptId: purchase.receiptId, |
| 57 | + }, |
| 58 | + }).catch((e) => console.log(e)); |
| 59 | + }; |
| 60 | + |
| 61 | + const onClickUnFulfill = () => { |
| 62 | + fulfill({ |
| 63 | + variables: { |
| 64 | + fulfilled: false, |
| 65 | + course: purchase.course, |
| 66 | + receiptId: purchase.receiptId, |
| 67 | + }, |
| 68 | + }).catch((e) => console.log(e)); |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="purchase-card"> |
| 73 | + <ReceiptMenu onClickUnfulfill={onClickUnFulfill} fulfilled={purchase.fulfilled} /> |
| 74 | + {!purchase.fulfilled ? ( |
| 75 | + <div> |
| 76 | + <Button |
| 77 | + style={{ |
| 78 | + outline: '0px', |
| 79 | + backgroundColor: 'green', |
| 80 | + border: '0px', |
| 81 | + float: 'right', |
| 82 | + color: 'grey', |
| 83 | + }} |
| 84 | + color="primary" |
| 85 | + aria-label="Fulfill" |
| 86 | + onClick={onClickFulfill} |
| 87 | + > |
| 88 | + <span>Fulfill </span> |
| 89 | + {mutationLoading ? <CircularProgress size={15} /> : <></>} |
| 90 | + </Button> |
| 91 | + </div> |
| 92 | + ) : ( |
| 93 | + <></> |
| 94 | + )} |
| 95 | + |
| 96 | + <h6>{`${ |
| 97 | + displayStudentName ? `${purchase.student.firstName} ${purchase.student.lastName}` : '' |
| 98 | + }${middleText}${displayListingName ? purchase.listingName : ''}`}</h6> |
| 99 | + <p>{`${purchase.purchaseDate}`}</p> |
| 100 | + {purchase.quantity > 1 ? <p>Quantity: {`${purchase.quantity}`}</p> : <></>} |
| 101 | + <p> |
| 102 | + Spent: {`${purchase.pointsSpent}`} Point{purchase.pointsSpent > 1 ? 's' : ''} |
| 103 | + </p> |
| 104 | + {purchase.note !== '' ? <p>Note: {`${purchase.note}`}</p> : <></>} |
| 105 | + </div> |
| 106 | + ); |
| 107 | +} |
0 commit comments