|
| 1 | +import React, {useState} from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + reactExtension, |
| 5 | + POSBlock, |
| 6 | + Text, |
| 7 | + POSBlockRow, |
| 8 | + useApi, |
| 9 | + Button, |
| 10 | +} from '@shopify/ui-extensions-react/point-of-sale'; |
| 11 | + |
| 12 | +import {useLoyaltyPoints} from './useLoyaltyPoints'; |
| 13 | +import {applyDiscount} from './applyDiscount'; |
| 14 | + |
| 15 | +// For development purposes, we'll use a local server |
| 16 | +export const serverUrl = |
| 17 | + 'https://hrs-macintosh-graph-testimonials.trycloudflare.com'; |
| 18 | +// [START loyalty-points-block.discounts] |
| 19 | +// 1. Define discount tiers and available discounts |
| 20 | +const discountTiers = [ |
| 21 | + {pointsRequired: 100, discountValue: 5}, |
| 22 | + {pointsRequired: 200, discountValue: 10}, |
| 23 | + {pointsRequired: 300, discountValue: 15}, |
| 24 | +]; |
| 25 | +// [END loyalty-points-block.discounts] |
| 26 | +const LoyaltyPointsBlock = () => { |
| 27 | + // [START loyalty-points-block.api] |
| 28 | + // 2. Initialize API |
| 29 | + const api = useApi<'pos.customer-details.block.render'>(); |
| 30 | + const customerId = api.customer.id; |
| 31 | + const [pointsTotal, setPointsTotal] = useState<number | null>(null); |
| 32 | + // [END loyalty-points-block.api] |
| 33 | + |
| 34 | + // [START loyalty-points-block.use-loyalty-points] |
| 35 | + // 3. Pass setPointsTotal to useLoyaltyPoints to calculate the points total |
| 36 | + const {loading} = useLoyaltyPoints(api, customerId, setPointsTotal); |
| 37 | + // [END loyalty-points-block.use-loyalty-points] |
| 38 | + |
| 39 | + // [START loyalty-points-block.available-discounts] |
| 40 | + // 4. Filter available discounts based on points total |
| 41 | + const availableDiscounts = pointsTotal |
| 42 | + ? discountTiers.filter((tier) => pointsTotal >= tier.pointsRequired) |
| 43 | + : []; |
| 44 | + // [END loyalty-points-block.available-discounts] |
| 45 | + if (loading) { |
| 46 | + return <Text>Loading...</Text>; |
| 47 | + } |
| 48 | + |
| 49 | + if (pointsTotal === null) { |
| 50 | + return ( |
| 51 | + <POSBlock> |
| 52 | + <POSBlockRow> |
| 53 | + <Text color="TextWarning">Unable to fetch points total.</Text> |
| 54 | + </POSBlockRow> |
| 55 | + </POSBlock> |
| 56 | + ); |
| 57 | + } |
| 58 | + return ( |
| 59 | + <POSBlock> |
| 60 | + <POSBlockRow> |
| 61 | + <Text variant="headingLarge" color="TextSuccess"> |
| 62 | + {/* [START loyalty-points-block.display-points] */} |
| 63 | + {/* 5. Display the points total */} |
| 64 | + Point Balance:{pointsTotal} |
| 65 | + </Text> |
| 66 | + </POSBlockRow> |
| 67 | + {/* [END loyalty-points-block.display-points] */} |
| 68 | + {availableDiscounts.length > 0 ? ( |
| 69 | + <POSBlockRow> |
| 70 | + <Text variant="headingSmall">Available Discounts:</Text> |
| 71 | + {/* [START loyalty-points-block.display-discounts] */} |
| 72 | + {/* 6. Display available discounts as buttons, calling applyDiscount */} |
| 73 | + {availableDiscounts.map((tier, index) => ( |
| 74 | + <POSBlockRow key={`${tier.pointsRequired}-${index}`}> |
| 75 | + <Button |
| 76 | + title={`Redeem $${tier.discountValue} Discount (Use ${tier.pointsRequired} points)`} |
| 77 | + type="primary" |
| 78 | + onPress={() => |
| 79 | + applyDiscount( |
| 80 | + api, |
| 81 | + customerId, |
| 82 | + tier.discountValue, |
| 83 | + tier.pointsRequired, |
| 84 | + setPointsTotal, |
| 85 | + ) |
| 86 | + } |
| 87 | + /> |
| 88 | + </POSBlockRow> |
| 89 | + ))} |
| 90 | + </POSBlockRow> |
| 91 | + ) : ( |
| 92 | + <POSBlockRow> |
| 93 | + <Text variant="headingSmall" color="TextWarning"> |
| 94 | + No available discounts. |
| 95 | + </Text> |
| 96 | + </POSBlockRow> |
| 97 | + )} |
| 98 | + </POSBlock> |
| 99 | + ); |
| 100 | +}; |
| 101 | +// 7. Render the LoyaltyPointsBlock component at the appropriate target |
| 102 | +// [START loyalty-points-block.render] |
| 103 | +export default reactExtension('pos.customer-details.block.render', () => ( |
| 104 | + <LoyaltyPointsBlock /> |
| 105 | +)); |
| 106 | +// [END loyalty-points-block.render] |
0 commit comments