@betterstore/react 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,7 @@ import type { CheckoutFormData } from "./checkout-schema";
|
|
|
3
3
|
interface CheckoutFormProps {
|
|
4
4
|
checkoutId: string;
|
|
5
5
|
onComplete?: (data: CheckoutFormData) => void;
|
|
6
|
+
cancelUrl: string;
|
|
6
7
|
}
|
|
7
|
-
export default function CheckoutForm({ checkoutId, onComplete, }: CheckoutFormProps): React.JSX.Element;
|
|
8
|
+
export default function CheckoutForm({ checkoutId, onComplete, cancelUrl, }: CheckoutFormProps): React.JSX.Element;
|
|
8
9
|
export {};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
declare function CheckoutEmbed({ checkoutId }: {
|
|
2
|
+
declare function CheckoutEmbed({ checkoutId, cancelUrl, successUrl, }: {
|
|
3
3
|
checkoutId: string;
|
|
4
|
+
cancelUrl: string;
|
|
5
|
+
successUrl: string;
|
|
4
6
|
}): React.JSX.Element;
|
|
5
7
|
declare const _default: React.MemoExoticComponent<typeof CheckoutEmbed>;
|
|
6
8
|
export default _default;
|
package/dist/index.cjs.js
CHANGED
|
@@ -838,15 +838,15 @@ function useLocalStorage(key, initialValue) {
|
|
|
838
838
|
try {
|
|
839
839
|
// Get from local storage by key
|
|
840
840
|
const item = window.localStorage.getItem(key);
|
|
841
|
-
//
|
|
842
|
-
|
|
841
|
+
// Only set the value if we found something in localStorage
|
|
842
|
+
if (item) {
|
|
843
|
+
setStoredValue(JSON.parse(item));
|
|
844
|
+
}
|
|
843
845
|
}
|
|
844
846
|
catch (error) {
|
|
845
|
-
// If error, just use the initialValue
|
|
846
847
|
console.error(error);
|
|
847
|
-
setStoredValue(initialValue);
|
|
848
848
|
}
|
|
849
|
-
}, [
|
|
849
|
+
}, []); // Only run on mount
|
|
850
850
|
// Return a wrapped version of useState's setter function that
|
|
851
851
|
// persists the new value to localStorage.
|
|
852
852
|
const setValue = (value) => {
|
|
@@ -868,8 +868,8 @@ function useLocalStorage(key, initialValue) {
|
|
|
868
868
|
return [storedValue, setValue];
|
|
869
869
|
}
|
|
870
870
|
|
|
871
|
-
function CheckoutForm$2({ checkoutId, onComplete, }) {
|
|
872
|
-
const [step, setStep] = React.useState("
|
|
871
|
+
function CheckoutForm$2({ checkoutId, onComplete, cancelUrl, }) {
|
|
872
|
+
const [step, setStep] = React.useState("customer");
|
|
873
873
|
const [formData, setFormData] = useLocalStorage(`checkout-${checkoutId}`, {});
|
|
874
874
|
// Helper function to format address for display
|
|
875
875
|
const formatAddress = (address) => {
|
|
@@ -906,8 +906,9 @@ function CheckoutForm$2({ checkoutId, onComplete, }) {
|
|
|
906
906
|
};
|
|
907
907
|
// Navigate back to previous step
|
|
908
908
|
const handleBack = () => {
|
|
909
|
-
if (step === "customer")
|
|
910
|
-
|
|
909
|
+
if (step === "customer") {
|
|
910
|
+
window.location.replace(cancelUrl);
|
|
911
|
+
}
|
|
911
912
|
if (step === "shipping")
|
|
912
913
|
setStep("customer");
|
|
913
914
|
if (step === "payment")
|
|
@@ -963,36 +964,45 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, }) {
|
|
|
963
964
|
React.createElement("span", { className: "text-2xl font-bold" }, formatPrice(total)))))));
|
|
964
965
|
}
|
|
965
966
|
|
|
966
|
-
function CheckoutEmbed({ checkoutId }) {
|
|
967
|
+
function CheckoutEmbed({ checkoutId, cancelUrl, successUrl, }) {
|
|
967
968
|
const [checkout, setCheckout] = React.useState(null);
|
|
968
969
|
const [loading, setLoading] = React.useState(true);
|
|
969
970
|
React.useEffect(() => {
|
|
971
|
+
let mounted = true; // Add mounted flag for cleanup
|
|
970
972
|
function fetchCheckout() {
|
|
971
973
|
return __awaiter(this, void 0, void 0, function* () {
|
|
972
974
|
try {
|
|
973
975
|
const response = yield fetch(`/api/betterstore/checkout?checkoutId=${checkoutId}`);
|
|
974
976
|
const data = yield response.json();
|
|
975
|
-
|
|
977
|
+
if (mounted) {
|
|
978
|
+
// Only update state if component is still mounted
|
|
979
|
+
setCheckout(data);
|
|
980
|
+
setLoading(false);
|
|
981
|
+
}
|
|
976
982
|
}
|
|
977
983
|
catch (error) {
|
|
978
984
|
console.error("Failed to fetch checkout:", error);
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
985
|
+
if (mounted) {
|
|
986
|
+
setLoading(false);
|
|
987
|
+
}
|
|
982
988
|
}
|
|
983
989
|
});
|
|
984
990
|
}
|
|
985
991
|
fetchCheckout();
|
|
986
|
-
|
|
992
|
+
return () => {
|
|
993
|
+
mounted = false; // Cleanup
|
|
994
|
+
};
|
|
995
|
+
}, [checkoutId]); // Only re-run if checkoutId changes
|
|
987
996
|
const handleComplete = (formData) => {
|
|
988
997
|
console.log("Checkout complete:", formData);
|
|
998
|
+
console.log("Success URL:", successUrl);
|
|
989
999
|
// Here you would typically send the completed form data to your API
|
|
990
1000
|
};
|
|
991
1001
|
if (!checkout && !loading) {
|
|
992
1002
|
return React.createElement("div", null, "Checkout not found");
|
|
993
1003
|
}
|
|
994
1004
|
return (React.createElement("div", { className: "grid md:grid-cols-2 gap-4" },
|
|
995
|
-
React.createElement("div", null, loading ? (React.createElement("div", { className: "text-white" }, "Loading...")) : (React.createElement(CheckoutForm$2, { checkoutId: checkoutId, onComplete: handleComplete }))),
|
|
1005
|
+
React.createElement("div", null, loading ? (React.createElement("div", { className: "text-white" }, "Loading...")) : (React.createElement(CheckoutForm$2, { cancelUrl: cancelUrl, checkoutId: checkoutId, onComplete: handleComplete }))),
|
|
996
1006
|
React.createElement("div", null, loading ? (React.createElement("div", null, "Loading...")) : (React.createElement(CheckoutSummary, { currency: checkout.currency, lineItems: checkout.lineItems, shipping: checkout === null || checkout === void 0 ? void 0 : checkout.shipping, tax: checkout === null || checkout === void 0 ? void 0 : checkout.tax })))));
|
|
997
1007
|
}
|
|
998
1008
|
var index$1 = React.memo(CheckoutEmbed);
|
package/dist/index.mjs
CHANGED
|
@@ -814,15 +814,15 @@ function useLocalStorage(key, initialValue) {
|
|
|
814
814
|
try {
|
|
815
815
|
// Get from local storage by key
|
|
816
816
|
const item = window.localStorage.getItem(key);
|
|
817
|
-
//
|
|
818
|
-
|
|
817
|
+
// Only set the value if we found something in localStorage
|
|
818
|
+
if (item) {
|
|
819
|
+
setStoredValue(JSON.parse(item));
|
|
820
|
+
}
|
|
819
821
|
}
|
|
820
822
|
catch (error) {
|
|
821
|
-
// If error, just use the initialValue
|
|
822
823
|
console.error(error);
|
|
823
|
-
setStoredValue(initialValue);
|
|
824
824
|
}
|
|
825
|
-
}, [
|
|
825
|
+
}, []); // Only run on mount
|
|
826
826
|
// Return a wrapped version of useState's setter function that
|
|
827
827
|
// persists the new value to localStorage.
|
|
828
828
|
const setValue = (value) => {
|
|
@@ -844,8 +844,8 @@ function useLocalStorage(key, initialValue) {
|
|
|
844
844
|
return [storedValue, setValue];
|
|
845
845
|
}
|
|
846
846
|
|
|
847
|
-
function CheckoutForm$2({ checkoutId, onComplete, }) {
|
|
848
|
-
const [step, setStep] = useState("
|
|
847
|
+
function CheckoutForm$2({ checkoutId, onComplete, cancelUrl, }) {
|
|
848
|
+
const [step, setStep] = useState("customer");
|
|
849
849
|
const [formData, setFormData] = useLocalStorage(`checkout-${checkoutId}`, {});
|
|
850
850
|
// Helper function to format address for display
|
|
851
851
|
const formatAddress = (address) => {
|
|
@@ -882,8 +882,9 @@ function CheckoutForm$2({ checkoutId, onComplete, }) {
|
|
|
882
882
|
};
|
|
883
883
|
// Navigate back to previous step
|
|
884
884
|
const handleBack = () => {
|
|
885
|
-
if (step === "customer")
|
|
886
|
-
|
|
885
|
+
if (step === "customer") {
|
|
886
|
+
window.location.replace(cancelUrl);
|
|
887
|
+
}
|
|
887
888
|
if (step === "shipping")
|
|
888
889
|
setStep("customer");
|
|
889
890
|
if (step === "payment")
|
|
@@ -939,36 +940,45 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, }) {
|
|
|
939
940
|
React__default.createElement("span", { className: "text-2xl font-bold" }, formatPrice(total)))))));
|
|
940
941
|
}
|
|
941
942
|
|
|
942
|
-
function CheckoutEmbed({ checkoutId }) {
|
|
943
|
+
function CheckoutEmbed({ checkoutId, cancelUrl, successUrl, }) {
|
|
943
944
|
const [checkout, setCheckout] = useState(null);
|
|
944
945
|
const [loading, setLoading] = useState(true);
|
|
945
946
|
useEffect(() => {
|
|
947
|
+
let mounted = true; // Add mounted flag for cleanup
|
|
946
948
|
function fetchCheckout() {
|
|
947
949
|
return __awaiter(this, void 0, void 0, function* () {
|
|
948
950
|
try {
|
|
949
951
|
const response = yield fetch(`/api/betterstore/checkout?checkoutId=${checkoutId}`);
|
|
950
952
|
const data = yield response.json();
|
|
951
|
-
|
|
953
|
+
if (mounted) {
|
|
954
|
+
// Only update state if component is still mounted
|
|
955
|
+
setCheckout(data);
|
|
956
|
+
setLoading(false);
|
|
957
|
+
}
|
|
952
958
|
}
|
|
953
959
|
catch (error) {
|
|
954
960
|
console.error("Failed to fetch checkout:", error);
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
961
|
+
if (mounted) {
|
|
962
|
+
setLoading(false);
|
|
963
|
+
}
|
|
958
964
|
}
|
|
959
965
|
});
|
|
960
966
|
}
|
|
961
967
|
fetchCheckout();
|
|
962
|
-
|
|
968
|
+
return () => {
|
|
969
|
+
mounted = false; // Cleanup
|
|
970
|
+
};
|
|
971
|
+
}, [checkoutId]); // Only re-run if checkoutId changes
|
|
963
972
|
const handleComplete = (formData) => {
|
|
964
973
|
console.log("Checkout complete:", formData);
|
|
974
|
+
console.log("Success URL:", successUrl);
|
|
965
975
|
// Here you would typically send the completed form data to your API
|
|
966
976
|
};
|
|
967
977
|
if (!checkout && !loading) {
|
|
968
978
|
return React__default.createElement("div", null, "Checkout not found");
|
|
969
979
|
}
|
|
970
980
|
return (React__default.createElement("div", { className: "grid md:grid-cols-2 gap-4" },
|
|
971
|
-
React__default.createElement("div", null, loading ? (React__default.createElement("div", { className: "text-white" }, "Loading...")) : (React__default.createElement(CheckoutForm$2, { checkoutId: checkoutId, onComplete: handleComplete }))),
|
|
981
|
+
React__default.createElement("div", null, loading ? (React__default.createElement("div", { className: "text-white" }, "Loading...")) : (React__default.createElement(CheckoutForm$2, { cancelUrl: cancelUrl, checkoutId: checkoutId, onComplete: handleComplete }))),
|
|
972
982
|
React__default.createElement("div", null, loading ? (React__default.createElement("div", null, "Loading...")) : (React__default.createElement(CheckoutSummary, { currency: checkout.currency, lineItems: checkout.lineItems, shipping: checkout === null || checkout === void 0 ? void 0 : checkout.shipping, tax: checkout === null || checkout === void 0 ? void 0 : checkout.tax })))));
|
|
973
983
|
}
|
|
974
984
|
var index$1 = memo(CheckoutEmbed);
|