Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize dev docs for loyalty extension #2486

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from

Conversation

merkoyep
Copy link
Contributor

@merkoyep merkoyep commented Nov 21, 2024

Background

Accompanying PR
Resolves https://github.com/Shopify/pos-next-react-native/issues/42764

As part of the deprecating legacy extensions project, this PR aims to educate developers on how to create a loyalty extension following the deprecation of legacy UI Extensions. This PR will show developers create a tutorial that showcases how to build a loyalty extension using POS UI Extensions.

Solution

This PR uses generated docs to create documentation to be hosted on shopify dev docs.

The tutorial shows developers how to do the following:

  • Calculate a total point balance by querying the Shopify Admin Api for the sum of a customer's spending at a store.
  • Create a database to track point redemptions.
  • Calculate a point balance by using the total spent converted to points, then subtracting total point redemptions.
  • Utilize routes to interface the app's backend with the frontend.
  • Scaffold a UI extension to render a customer's point balance, and apply discounts using the POS Cart api.
LoyaltyExtensionDemo.mov

🎩

  • Please review changes on Spin

Checklist

  • I have 🎩'd these changes
  • I have updated relevant documentation

@merkoyep merkoyep marked this pull request as ready for review November 21, 2024 22:16
Copy link
Contributor

@NathanJolly NathanJolly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bravo. What a useful and thorough guide for our users.

Comment on lines +39 to +41
if (loading) {
return <Text>Loading...</Text>;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this end up looking like in a Block extension? Does it even work? I think you may need to wrap this in a POSBlock at least, and perhaps a POSBlockRow, too?

<POSBlockRow>
<Text variant="headingLarge" color="TextSuccess">
{/* 5. Display the point balance */}
Point Balance:{pointBalance}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing a space?

<Text variant="headingSmall">Available Discounts:</Text>
{/* 6. Display available discounts as buttons, calling applyDiscount */}
{availableDiscounts.map((tier) => (
<POSBlockRow key={tier.pointsRequired}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try a List component here? Just wondering if it works and if it looks better than stacking POSBlockRows

import type {useApi} from '@shopify/ui-extensions-react/point-of-sale';
import {serverUrl} from './LoyaltyPointsBlock';
export const applyDiscount = async (
api: ReturnType<typeof useApi>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does ReturnType do here? I think UI Extensions exports the correct type somewhere. I think it's ApiForRenderExtension<Target>

throw new Error(`Failed to deduct points: ${errorText}`);
}
// 3. Update the point balance in the state
setPointBalance((prev) => (prev !== null ? prev - pointsToDeduct : null));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good practice to let the server respond with the updated total here, so the math isn't being done in two places.

anchorLink: 'shopify-admin-api',
title: 'Fetch total points spent',
sectionContent: `
To calculate the point balance, we will be calculating the total amount a customer has spent, then subtracting any point redemptions made. In future iterations of POS UI Extensions, you will be able to calculate total points in a more direct way.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To calculate the point balance, we will be calculating the total amount a customer has spent, then subtracting any point redemptions made. In future iterations of POS UI Extensions, you will be able to calculate total points in a more direct way.
To calculate the point balance, we will be calculating the total amount a customer has spent, then subtracting any point redemptions made.

I think this is plenty. Partners can figure out better ways if they want to ;)

Comment on lines +65 to +69
1. Authenticate the request.
2. Format the customer ID for the Shopify Admin API.
3. Fetch the customer's orders.
4. Parse the response and handle any errors.
5. Calculate the total amount spent and return the value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In markdown, you can number these all as 1. and the markdown engine will number them for you. Can be useful if you ever want to add a step in the middle. You don't have to re-number them.

Suggested change
1. Authenticate the request.
2. Format the customer ID for the Shopify Admin API.
3. Fetch the customer's orders.
4. Parse the response and handle any errors.
5. Calculate the total amount spent and return the value.
1. Authenticate the request.
1. Format the customer ID for the Shopify Admin API.
1. Fetch the customer's orders.
1. Parse the response and handle any errors.
1. Calculate the total amount spent and return the value.

anchorLink: 'apply-discount',
title: 'Add hooks and helpers',
sectionContent: `
Lets create a function to handle the logic of applying a discount to an order.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Lets create a function to handle the logic of applying a discount to an order.
Create a function to handle the logic of applying a discount to an order.

type: 'note',
title: 'Testing tip',
sectionContent:
"Use your browser's developer tools to monitor network requests and check for any CORS or authentication issues.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can link to the debugging page here

{
title: 'Routes are not working?',
description: `
Check that your routes are correctly defined. If you are using Remix, check that your routes are correctly defined in the \`routes\` directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can link to the Remix routing docs here!

@js-goupil
Copy link
Contributor

I noticed in the video, that the gif you provided has an RN error. We probably don't want to be surfacing any POS implementation details publicly, and it doesn't look as polished. Could we replace the gif with one recorded using an internal build, or even an app store build?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants