|
| 1 | +import React 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 | +import {useFetchLoyaltyPoints} from './useFetchLoyaltyPoints'; |
| 12 | + |
| 13 | +export const DISCOUNT_NAME = 'Loyalty-Direct'; |
| 14 | + |
| 15 | +// [START customer-details.block.component] |
| 16 | +// 2. Implement the `CustomerDetailsBlock` component |
| 17 | +const CustomerDetailsBlock = () => { |
| 18 | + // [END customer-details.block.component] |
| 19 | + |
| 20 | + // [START customer-details.block.api] |
| 21 | + // 3. Setup the api |
| 22 | + const api = useApi<'pos.customer-details.block.render'>(); |
| 23 | + // [END customer-details.block.api] |
| 24 | + |
| 25 | + // [START customer-details.block.fetch-loyalty-points] |
| 26 | + // 4. Fetch the loyalty points and filter available discounts using the useFetchLoyaltyPoints hook |
| 27 | + const {loading, pointsTotal, availableDiscounts} = useFetchLoyaltyPoints( |
| 28 | + api.customer.id, |
| 29 | + ); |
| 30 | + // [END customer-details.block.fetch-loyalty-points] |
| 31 | + |
| 32 | + // [START customer-details.block.apply-discount] |
| 33 | + // 5. Implement the applyDiscount function |
| 34 | + const applyDiscount = (discountValue: number, pointsRequired: number) => { |
| 35 | + if (pointsTotal) { |
| 36 | + api.cart.applyCartDiscount( |
| 37 | + 'Percentage', |
| 38 | + `${DISCOUNT_NAME}.${pointsTotal - pointsRequired}`, |
| 39 | + String(discountValue), |
| 40 | + ); |
| 41 | + |
| 42 | + api.toast.show(`${discountValue}% off applied`); |
| 43 | + } else { |
| 44 | + api.toast.show(`Error applying discount`); |
| 45 | + } |
| 46 | + }; |
| 47 | + // [END customer-details.block.apply-discount] |
| 48 | + |
| 49 | + // [START customer-details.block.loading-error] |
| 50 | + // 7. Handle error and loading states |
| 51 | + if (loading) { |
| 52 | + return ( |
| 53 | + <POSBlock> |
| 54 | + <POSBlockRow> |
| 55 | + <Text>Loading...</Text> |
| 56 | + </POSBlockRow> |
| 57 | + </POSBlock> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + if (pointsTotal === null) { |
| 62 | + return ( |
| 63 | + <POSBlock> |
| 64 | + <POSBlockRow> |
| 65 | + <Text color="TextWarning">Unable to fetch loyalty points.</Text> |
| 66 | + </POSBlockRow> |
| 67 | + </POSBlock> |
| 68 | + ); |
| 69 | + } |
| 70 | + // [END customer-details.block.loading-error] |
| 71 | + |
| 72 | + // [START customer-details.block.render-implementation] |
| 73 | + // 6. Render the CustomerDetailsBlock component to display available points and discounts |
| 74 | + return ( |
| 75 | + <POSBlock> |
| 76 | + <POSBlockRow> |
| 77 | + <Text |
| 78 | + variant="body" |
| 79 | + color={pointsTotal > 0 ? 'TextSuccess' : 'TextWarning'} |
| 80 | + > |
| 81 | + Point balance: {pointsTotal} |
| 82 | + </Text> |
| 83 | + </POSBlockRow> |
| 84 | + {availableDiscounts.length > 0 ? ( |
| 85 | + <POSBlockRow> |
| 86 | + <Text variant="body">Available Discounts:</Text> |
| 87 | + {availableDiscounts.map((tier, index) => ( |
| 88 | + <POSBlockRow key={`${tier.pointsRequired}-${index}`}> |
| 89 | + <Button |
| 90 | + title={`Redeem $${tier.discountValue} discount (${tier.pointsRequired} points)`} |
| 91 | + type="primary" |
| 92 | + onPress={() => |
| 93 | + applyDiscount(tier.discountValue, tier.pointsRequired) |
| 94 | + } |
| 95 | + /> |
| 96 | + </POSBlockRow> |
| 97 | + ))} |
| 98 | + </POSBlockRow> |
| 99 | + ) : ( |
| 100 | + <POSBlockRow> |
| 101 | + <Text variant="body" color="TextWarning"> |
| 102 | + No available discounts. |
| 103 | + </Text> |
| 104 | + </POSBlockRow> |
| 105 | + )} |
| 106 | + </POSBlock> |
| 107 | + ); |
| 108 | + // [END customer-details.block.render-implementation] |
| 109 | +}; |
| 110 | + |
| 111 | +// [START customer-details.block.render-extension] |
| 112 | +// 1. Render the CustomerDetailsBlock component at the `pos.customer-details.block.render` target |
| 113 | +export default reactExtension('pos.customer-details.block.render', () => ( |
| 114 | + <CustomerDetailsBlock /> |
| 115 | +)); |
| 116 | +// [END customer-details.block.render-extension] |
0 commit comments