Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion plugins/payments-demo/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"no_providers": "No payment provider is configured yet, so this item cannot be bought.",
"buy": "Buy now",
"owned": "You own this item",
"sign_in": "Sign in to buy",
"view_purchase": "View purchase"
},
"purchase": {
Expand Down
24 changes: 10 additions & 14 deletions plugins/payments-demo/src/pages/shop-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
import { Skeleton } from "@vitnode/core/components/ui/skeleton";
import { definePluginRoute } from "@vitnode/core/routing";
import { useSessionQuery } from "@vitnode/core/tanstack/auth";
import { HrefLink } from "@vitnode/core/tanstack/link";
import { CheckIcon, CreditCardIcon, LogInIcon } from "lucide-react";
import { HrefLink, useHrefNavigate } from "@vitnode/core/tanstack/link";
import { CheckIcon, CreditCardIcon } from "lucide-react";
import React from "react";
import { useLocale, useTranslations } from "use-intl";

Expand All @@ -41,6 +41,7 @@ const ShopPage = () => {
const t = useTranslations("payments-demo");
const locale = useLocale();
const session = useSessionQuery();
const navigate = useHrefNavigate();
const signedIn = !!session.data?.user;
const catalog = useQuery(catalogQueryOptions());
const purchases = useQuery({
Expand All @@ -58,6 +59,11 @@ const ShopPage = () => {
entry => entry.itemId === item?.id,
);

// A guest signs in first and comes back here to buy.
const onSignInToBuy = async () => {
await navigate("/login?returnTo=%2Fpayments-demo");
};

const onBuy = async () => {
if (!item || !selectedProvider) return;
setIsPending(true);
Expand Down Expand Up @@ -156,25 +162,15 @@ const ShopPage = () => {
<CheckIcon aria-hidden="true" />
{t("shop.owned")}
</Badge>
) : signedIn ? (
) : (
<Button
disabled={!selectedProvider}
isLoading={isPending}
onClick={onBuy}
onClick={signedIn ? onBuy : onSignInToBuy}
>
<CreditCardIcon aria-hidden="true" />
{t("shop.buy")}
</Button>
) : (
<HrefLink
className="text-primary font-medium underline-offset-4 hover:underline"
href="/login"
>
<span className="inline-flex items-center gap-2">
<LogInIcon aria-hidden="true" className="size-4" />
{t("shop.sign_in")}
</span>
</HrefLink>
)}

{purchase && (
Expand Down
Loading