@cmssy/react 0.1.8 → 0.2.0
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/dist/client.cjs +665 -0
- package/dist/client.d.cts +63 -3
- package/dist/client.d.ts +63 -3
- package/dist/client.js +657 -2
- package/dist/{registry-e3O4s_bN.d.cts → commerce-queries-DV7PZlKS.d.cts} +57 -1
- package/dist/{registry-e3O4s_bN.d.ts → commerce-queries-DV7PZlKS.d.ts} +57 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/client.cjs
CHANGED
|
@@ -5,6 +5,9 @@ var react = require('react');
|
|
|
5
5
|
var jsxRuntime = require('react/jsx-runtime');
|
|
6
6
|
|
|
7
7
|
// src/registry.ts
|
|
8
|
+
function defineBlock(def) {
|
|
9
|
+
return def;
|
|
10
|
+
}
|
|
8
11
|
function buildBlockMap(blocks) {
|
|
9
12
|
const map = /* @__PURE__ */ Object.create(null);
|
|
10
13
|
for (const block of blocks) map[block.type] = block.component;
|
|
@@ -747,9 +750,671 @@ function CmssyLazyLayout({ load, ...props }) {
|
|
|
747
750
|
if (!blocks) return null;
|
|
748
751
|
return /* @__PURE__ */ jsxRuntime.jsx(CmssyEditableLayout, { ...props, blocks });
|
|
749
752
|
}
|
|
753
|
+
var CmssyAuthContext = react.createContext(null);
|
|
754
|
+
function CmssyAuthProvider({
|
|
755
|
+
children,
|
|
756
|
+
basePath = "/api/cmssy/auth",
|
|
757
|
+
initialUser
|
|
758
|
+
}) {
|
|
759
|
+
const seeded = initialUser !== void 0;
|
|
760
|
+
const [user, setUser] = react.useState(initialUser ?? null);
|
|
761
|
+
const [loading, setLoading] = react.useState(!seeded);
|
|
762
|
+
const generation = react.useRef(0);
|
|
763
|
+
const base = react.useMemo(() => basePath.replace(/\/+$/, ""), [basePath]);
|
|
764
|
+
const commitUser = react.useCallback((next) => {
|
|
765
|
+
generation.current += 1;
|
|
766
|
+
setUser(next);
|
|
767
|
+
}, []);
|
|
768
|
+
const fetchUser = react.useCallback(async () => {
|
|
769
|
+
try {
|
|
770
|
+
const res = await fetch(`${base}/me`, {
|
|
771
|
+
credentials: "same-origin",
|
|
772
|
+
cache: "no-store"
|
|
773
|
+
});
|
|
774
|
+
const data = await res.json();
|
|
775
|
+
return data.user ?? null;
|
|
776
|
+
} catch {
|
|
777
|
+
return null;
|
|
778
|
+
}
|
|
779
|
+
}, [base]);
|
|
780
|
+
react.useEffect(() => {
|
|
781
|
+
if (seeded) return;
|
|
782
|
+
const gen = generation.current;
|
|
783
|
+
void (async () => {
|
|
784
|
+
const next = await fetchUser();
|
|
785
|
+
if (gen === generation.current) {
|
|
786
|
+
setUser(next);
|
|
787
|
+
setLoading(false);
|
|
788
|
+
}
|
|
789
|
+
})();
|
|
790
|
+
}, [fetchUser, seeded]);
|
|
791
|
+
const postAction = react.useCallback(
|
|
792
|
+
async (action, body) => {
|
|
793
|
+
const res = await fetch(`${base}/${action}`, {
|
|
794
|
+
method: "POST",
|
|
795
|
+
credentials: "same-origin",
|
|
796
|
+
headers: { "content-type": "application/json" },
|
|
797
|
+
body: JSON.stringify(body)
|
|
798
|
+
});
|
|
799
|
+
return await res.json();
|
|
800
|
+
},
|
|
801
|
+
[base]
|
|
802
|
+
);
|
|
803
|
+
const signIn = react.useCallback(
|
|
804
|
+
async (identity, password) => {
|
|
805
|
+
const data = await postAction("sign-in", { identity, password });
|
|
806
|
+
if (data.ok && data.user) commitUser(data.user);
|
|
807
|
+
return { ok: Boolean(data.ok), message: data.message };
|
|
808
|
+
},
|
|
809
|
+
[postAction, commitUser]
|
|
810
|
+
);
|
|
811
|
+
const register = react.useCallback(
|
|
812
|
+
async (identity, password, fields2) => {
|
|
813
|
+
const data = await postAction("register", {
|
|
814
|
+
identity,
|
|
815
|
+
password,
|
|
816
|
+
fields: fields2 ?? {}
|
|
817
|
+
});
|
|
818
|
+
return { ok: Boolean(data.ok), message: data.message };
|
|
819
|
+
},
|
|
820
|
+
[postAction]
|
|
821
|
+
);
|
|
822
|
+
const signOut = react.useCallback(async () => {
|
|
823
|
+
try {
|
|
824
|
+
await postAction("sign-out", {});
|
|
825
|
+
} finally {
|
|
826
|
+
commitUser(null);
|
|
827
|
+
}
|
|
828
|
+
}, [postAction, commitUser]);
|
|
829
|
+
const refresh = react.useCallback(async () => {
|
|
830
|
+
const gen = generation.current;
|
|
831
|
+
const next = await fetchUser();
|
|
832
|
+
if (gen === generation.current) setUser(next);
|
|
833
|
+
}, [fetchUser]);
|
|
834
|
+
const value = react.useMemo(
|
|
835
|
+
() => ({ user, loading, signIn, register, signOut, refresh }),
|
|
836
|
+
[user, loading, signIn, register, signOut, refresh]
|
|
837
|
+
);
|
|
838
|
+
return /* @__PURE__ */ jsxRuntime.jsx(CmssyAuthContext.Provider, { value, children });
|
|
839
|
+
}
|
|
840
|
+
function useCmssyUser() {
|
|
841
|
+
const ctx = react.useContext(CmssyAuthContext);
|
|
842
|
+
if (!ctx) {
|
|
843
|
+
throw new Error("useCmssyUser must be used within <CmssyAuthProvider>");
|
|
844
|
+
}
|
|
845
|
+
return ctx;
|
|
846
|
+
}
|
|
847
|
+
var CmssyCommerceContext = react.createContext(null);
|
|
848
|
+
function CmssyCommerceProvider({
|
|
849
|
+
children,
|
|
850
|
+
basePath = "/api/cmssy/cart"
|
|
851
|
+
}) {
|
|
852
|
+
const base = react.useMemo(() => basePath.replace(/\/+$/, ""), [basePath]);
|
|
853
|
+
const generation = react.useRef(0);
|
|
854
|
+
const [cart, setCart] = react.useState(null);
|
|
855
|
+
const [loading, setLoading] = react.useState(true);
|
|
856
|
+
const [error, setError] = react.useState(null);
|
|
857
|
+
const post = react.useCallback(
|
|
858
|
+
async (action, body) => {
|
|
859
|
+
const res = await fetch(`${base}/${action}`, {
|
|
860
|
+
method: "POST",
|
|
861
|
+
credentials: "same-origin",
|
|
862
|
+
headers: { "content-type": "application/json" },
|
|
863
|
+
body: JSON.stringify(body)
|
|
864
|
+
});
|
|
865
|
+
const data = await res.json().catch(() => ({}));
|
|
866
|
+
if (!res.ok) {
|
|
867
|
+
throw new Error(
|
|
868
|
+
typeof data.message === "string" ? data.message : "Commerce request failed"
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
return data;
|
|
872
|
+
},
|
|
873
|
+
[base]
|
|
874
|
+
);
|
|
875
|
+
const runCart = react.useCallback(
|
|
876
|
+
async (action, body) => {
|
|
877
|
+
const gen = ++generation.current;
|
|
878
|
+
setError(null);
|
|
879
|
+
try {
|
|
880
|
+
const data = await post(action, body);
|
|
881
|
+
if (gen === generation.current) setCart(data.cart);
|
|
882
|
+
} catch (err) {
|
|
883
|
+
if (gen === generation.current) {
|
|
884
|
+
setError(
|
|
885
|
+
err instanceof Error ? err.message : "Commerce request failed"
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
throw err;
|
|
889
|
+
}
|
|
890
|
+
},
|
|
891
|
+
[post]
|
|
892
|
+
);
|
|
893
|
+
const refresh = react.useCallback(async () => {
|
|
894
|
+
setLoading(true);
|
|
895
|
+
try {
|
|
896
|
+
await runCart("cart", {});
|
|
897
|
+
} catch {
|
|
898
|
+
return;
|
|
899
|
+
} finally {
|
|
900
|
+
setLoading(false);
|
|
901
|
+
}
|
|
902
|
+
}, [runCart]);
|
|
903
|
+
react.useEffect(() => {
|
|
904
|
+
void refresh();
|
|
905
|
+
}, [refresh]);
|
|
906
|
+
const addToCart = react.useCallback(
|
|
907
|
+
(recordId, quantity = 1, options) => runCart("add", {
|
|
908
|
+
recordId,
|
|
909
|
+
quantity,
|
|
910
|
+
variantSelections: options?.variantSelections,
|
|
911
|
+
notes: options?.notes
|
|
912
|
+
}),
|
|
913
|
+
[runCart]
|
|
914
|
+
);
|
|
915
|
+
const updateItem = react.useCallback(
|
|
916
|
+
(itemId, quantity) => runCart("update", { itemId, quantity }),
|
|
917
|
+
[runCart]
|
|
918
|
+
);
|
|
919
|
+
const removeItem = react.useCallback(
|
|
920
|
+
(itemId) => runCart("remove", { itemId }),
|
|
921
|
+
[runCart]
|
|
922
|
+
);
|
|
923
|
+
const clearCart = react.useCallback(() => runCart("clear", {}), [runCart]);
|
|
924
|
+
const applyDiscount = react.useCallback(
|
|
925
|
+
(code) => runCart("apply-discount", { code }),
|
|
926
|
+
[runCart]
|
|
927
|
+
);
|
|
928
|
+
const removeDiscount = react.useCallback(
|
|
929
|
+
() => runCart("remove-discount", {}),
|
|
930
|
+
[runCart]
|
|
931
|
+
);
|
|
932
|
+
const checkout = react.useCallback(
|
|
933
|
+
async (customerEmail) => {
|
|
934
|
+
const gen = ++generation.current;
|
|
935
|
+
setError(null);
|
|
936
|
+
try {
|
|
937
|
+
const data = await post("checkout", {
|
|
938
|
+
customerEmail
|
|
939
|
+
});
|
|
940
|
+
if (gen === generation.current) setCart(null);
|
|
941
|
+
return data.order;
|
|
942
|
+
} catch (err) {
|
|
943
|
+
if (gen === generation.current) {
|
|
944
|
+
setError(err instanceof Error ? err.message : "Checkout failed");
|
|
945
|
+
}
|
|
946
|
+
throw err;
|
|
947
|
+
}
|
|
948
|
+
},
|
|
949
|
+
[post]
|
|
950
|
+
);
|
|
951
|
+
const fetchProduct = react.useCallback(
|
|
952
|
+
async (modelSlug, filter) => {
|
|
953
|
+
const data = await post("product", {
|
|
954
|
+
modelSlug,
|
|
955
|
+
filter
|
|
956
|
+
});
|
|
957
|
+
return data.product;
|
|
958
|
+
},
|
|
959
|
+
[post]
|
|
960
|
+
);
|
|
961
|
+
const value = react.useMemo(
|
|
962
|
+
() => ({
|
|
963
|
+
cart,
|
|
964
|
+
loading,
|
|
965
|
+
error,
|
|
966
|
+
addToCart,
|
|
967
|
+
updateItem,
|
|
968
|
+
removeItem,
|
|
969
|
+
clearCart,
|
|
970
|
+
applyDiscount,
|
|
971
|
+
removeDiscount,
|
|
972
|
+
checkout,
|
|
973
|
+
refresh,
|
|
974
|
+
fetchProduct
|
|
975
|
+
}),
|
|
976
|
+
[
|
|
977
|
+
cart,
|
|
978
|
+
loading,
|
|
979
|
+
error,
|
|
980
|
+
addToCart,
|
|
981
|
+
updateItem,
|
|
982
|
+
removeItem,
|
|
983
|
+
clearCart,
|
|
984
|
+
applyDiscount,
|
|
985
|
+
removeDiscount,
|
|
986
|
+
checkout,
|
|
987
|
+
refresh,
|
|
988
|
+
fetchProduct
|
|
989
|
+
]
|
|
990
|
+
);
|
|
991
|
+
return /* @__PURE__ */ jsxRuntime.jsx(CmssyCommerceContext.Provider, { value, children });
|
|
992
|
+
}
|
|
993
|
+
function useCart() {
|
|
994
|
+
const ctx = react.useContext(CmssyCommerceContext);
|
|
995
|
+
if (!ctx) {
|
|
996
|
+
throw new Error("useCart must be used within <CmssyCommerceProvider>");
|
|
997
|
+
}
|
|
998
|
+
return ctx;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// src/fields.ts
|
|
1002
|
+
function control(type) {
|
|
1003
|
+
return (opts = {}) => ({
|
|
1004
|
+
type,
|
|
1005
|
+
label: opts.label ?? "",
|
|
1006
|
+
...opts
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
var fields = {
|
|
1010
|
+
singleLine: control("singleLine"),
|
|
1011
|
+
multiLine: control("multiLine"),
|
|
1012
|
+
richText: control("richText"),
|
|
1013
|
+
numeric: control("numeric"),
|
|
1014
|
+
date: control("date"),
|
|
1015
|
+
media: control("media"),
|
|
1016
|
+
link: control("link"),
|
|
1017
|
+
select: control("select"),
|
|
1018
|
+
multiselect: control("multiselect"),
|
|
1019
|
+
boolean: control("boolean"),
|
|
1020
|
+
color: control("color"),
|
|
1021
|
+
repeater: control("repeater")
|
|
1022
|
+
};
|
|
1023
|
+
|
|
1024
|
+
// src/commerce/money.ts
|
|
1025
|
+
var ZERO_DECIMAL = /* @__PURE__ */ new Set([
|
|
1026
|
+
"BIF",
|
|
1027
|
+
"CLP",
|
|
1028
|
+
"DJF",
|
|
1029
|
+
"GNF",
|
|
1030
|
+
"JPY",
|
|
1031
|
+
"KMF",
|
|
1032
|
+
"KRW",
|
|
1033
|
+
"MGA",
|
|
1034
|
+
"PYG",
|
|
1035
|
+
"RWF",
|
|
1036
|
+
"UGX",
|
|
1037
|
+
"VND",
|
|
1038
|
+
"VUV",
|
|
1039
|
+
"XAF",
|
|
1040
|
+
"XOF",
|
|
1041
|
+
"XPF"
|
|
1042
|
+
]);
|
|
1043
|
+
var THREE_DECIMAL = /* @__PURE__ */ new Set([
|
|
1044
|
+
"BHD",
|
|
1045
|
+
"IQD",
|
|
1046
|
+
"JOD",
|
|
1047
|
+
"KWD",
|
|
1048
|
+
"LYD",
|
|
1049
|
+
"OMR",
|
|
1050
|
+
"TND"
|
|
1051
|
+
]);
|
|
1052
|
+
function fractionDigits(currency) {
|
|
1053
|
+
const code = currency.toUpperCase();
|
|
1054
|
+
if (ZERO_DECIMAL.has(code)) return 0;
|
|
1055
|
+
if (THREE_DECIMAL.has(code)) return 3;
|
|
1056
|
+
return 2;
|
|
1057
|
+
}
|
|
1058
|
+
function fromMinorUnits(minor, currency) {
|
|
1059
|
+
return minor / 10 ** fractionDigits(currency);
|
|
1060
|
+
}
|
|
1061
|
+
function formatPrice(minor, currency) {
|
|
1062
|
+
if (!Number.isFinite(minor)) return "";
|
|
1063
|
+
const code = currency ?? "USD";
|
|
1064
|
+
const amount = fromMinorUnits(minor, code);
|
|
1065
|
+
try {
|
|
1066
|
+
return new Intl.NumberFormat(void 0, {
|
|
1067
|
+
style: "currency",
|
|
1068
|
+
currency: code
|
|
1069
|
+
}).format(amount);
|
|
1070
|
+
} catch {
|
|
1071
|
+
return `${amount.toFixed(fractionDigits(code))} ${code}`;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
function deriveAxes(variants) {
|
|
1075
|
+
const axes = /* @__PURE__ */ new Map();
|
|
1076
|
+
for (const variant of variants) {
|
|
1077
|
+
for (const option of variant.selectedOptions) {
|
|
1078
|
+
if (!axes.has(option.name)) axes.set(option.name, /* @__PURE__ */ new Set());
|
|
1079
|
+
axes.get(option.name).add(option.value);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
return [...axes.entries()].map(([name, values]) => ({
|
|
1083
|
+
name,
|
|
1084
|
+
values: [...values]
|
|
1085
|
+
}));
|
|
1086
|
+
}
|
|
1087
|
+
function matchVariant(variants, selections) {
|
|
1088
|
+
return variants.find(
|
|
1089
|
+
(variant) => variant.selectedOptions.every(
|
|
1090
|
+
(opt) => selections[opt.name] === opt.value
|
|
1091
|
+
)
|
|
1092
|
+
) ?? null;
|
|
1093
|
+
}
|
|
1094
|
+
function ProductComponent({ content }) {
|
|
1095
|
+
const c = content;
|
|
1096
|
+
const { fetchProduct, addToCart } = useCart();
|
|
1097
|
+
const [product, setProduct] = react.useState(null);
|
|
1098
|
+
const [loading, setLoading] = react.useState(true);
|
|
1099
|
+
const [selections, setSelections] = react.useState({});
|
|
1100
|
+
const [adding, setAdding] = react.useState(false);
|
|
1101
|
+
const [added, setAdded] = react.useState(false);
|
|
1102
|
+
const [addError, setAddError] = react.useState(null);
|
|
1103
|
+
const modelSlug = c.modelSlug ?? "products";
|
|
1104
|
+
const slugField = c.slugField ?? "slug";
|
|
1105
|
+
const slug = c.slug ?? "";
|
|
1106
|
+
react.useEffect(() => {
|
|
1107
|
+
let active = true;
|
|
1108
|
+
setLoading(true);
|
|
1109
|
+
void (async () => {
|
|
1110
|
+
try {
|
|
1111
|
+
const result = slug ? await fetchProduct(modelSlug, { [slugField]: slug }) : null;
|
|
1112
|
+
if (active) setProduct(result);
|
|
1113
|
+
} finally {
|
|
1114
|
+
if (active) setLoading(false);
|
|
1115
|
+
}
|
|
1116
|
+
})();
|
|
1117
|
+
return () => {
|
|
1118
|
+
active = false;
|
|
1119
|
+
};
|
|
1120
|
+
}, [fetchProduct, modelSlug, slugField, slug]);
|
|
1121
|
+
const axes = react.useMemo(
|
|
1122
|
+
() => product ? deriveAxes(product.variants) : [],
|
|
1123
|
+
[product]
|
|
1124
|
+
);
|
|
1125
|
+
const variant = react.useMemo(
|
|
1126
|
+
() => product ? matchVariant(product.variants, selections) : null,
|
|
1127
|
+
[product, selections]
|
|
1128
|
+
);
|
|
1129
|
+
if (loading) {
|
|
1130
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-cmssy-product": "loading", children: "Loading\u2026" });
|
|
1131
|
+
}
|
|
1132
|
+
if (!product) {
|
|
1133
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-cmssy-product": "not-found", children: "Product not found" });
|
|
1134
|
+
}
|
|
1135
|
+
const data = product.data;
|
|
1136
|
+
const name = String(data[c.nameField ?? "name"] ?? "");
|
|
1137
|
+
const imageUrl = c.imageField ? data[c.imageField] : null;
|
|
1138
|
+
const hasVariants = product.variants.length > 0;
|
|
1139
|
+
const currency = data.currency ?? "USD";
|
|
1140
|
+
const priceMinor = hasVariants ? variant?.price : Number(data[c.priceField ?? "price"] ?? 0);
|
|
1141
|
+
const showPrice = priceMinor != null && Number.isFinite(priceMinor);
|
|
1142
|
+
const allAxesSelected = axes.every((axis) => selections[axis.name]);
|
|
1143
|
+
const outOfStock = variant != null && variant.inventory != null && variant.inventory <= 0;
|
|
1144
|
+
const canAdd = hasVariants ? Boolean(variant) && !outOfStock : true;
|
|
1145
|
+
async function onAdd() {
|
|
1146
|
+
if (!product) return;
|
|
1147
|
+
setAdding(true);
|
|
1148
|
+
setAdded(false);
|
|
1149
|
+
setAddError(null);
|
|
1150
|
+
try {
|
|
1151
|
+
await addToCart(
|
|
1152
|
+
product.id,
|
|
1153
|
+
1,
|
|
1154
|
+
hasVariants ? { variantSelections: selections } : void 0
|
|
1155
|
+
);
|
|
1156
|
+
setAdded(true);
|
|
1157
|
+
} catch (err) {
|
|
1158
|
+
setAddError(err instanceof Error ? err.message : "Could not add to cart");
|
|
1159
|
+
} finally {
|
|
1160
|
+
setAdding(false);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { "data-cmssy-product": product.id, children: [
|
|
1164
|
+
imageUrl ? /* @__PURE__ */ jsxRuntime.jsx("img", { src: imageUrl, alt: name }) : null,
|
|
1165
|
+
/* @__PURE__ */ jsxRuntime.jsx("h3", { "data-cmssy-product-name": true, children: name }),
|
|
1166
|
+
showPrice ? /* @__PURE__ */ jsxRuntime.jsx("p", { "data-cmssy-product-price": true, children: formatPrice(priceMinor, currency) }) : null,
|
|
1167
|
+
axes.map((axis) => /* @__PURE__ */ jsxRuntime.jsxs("label", { "data-cmssy-variant-axis": axis.name, children: [
|
|
1168
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: axis.name }),
|
|
1169
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
1170
|
+
"select",
|
|
1171
|
+
{
|
|
1172
|
+
value: selections[axis.name] ?? "",
|
|
1173
|
+
onChange: (e) => setSelections((prev) => ({
|
|
1174
|
+
...prev,
|
|
1175
|
+
[axis.name]: e.target.value
|
|
1176
|
+
})),
|
|
1177
|
+
children: [
|
|
1178
|
+
/* @__PURE__ */ jsxRuntime.jsx("option", { value: "", children: "\u2014" }),
|
|
1179
|
+
axis.values.map((v) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: v, children: v }, v))
|
|
1180
|
+
]
|
|
1181
|
+
}
|
|
1182
|
+
)
|
|
1183
|
+
] }, axis.name)),
|
|
1184
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1185
|
+
"button",
|
|
1186
|
+
{
|
|
1187
|
+
type: "button",
|
|
1188
|
+
onClick: onAdd,
|
|
1189
|
+
disabled: adding || !canAdd || hasVariants && !allAxesSelected,
|
|
1190
|
+
"data-cmssy-add-to-cart": true,
|
|
1191
|
+
children: adding ? "Adding\u2026" : outOfStock ? "Out of stock" : added ? "Added" : "Add to cart"
|
|
1192
|
+
}
|
|
1193
|
+
),
|
|
1194
|
+
addError ? /* @__PURE__ */ jsxRuntime.jsx("p", { "data-cmssy-product-error": true, children: addError }) : null
|
|
1195
|
+
] });
|
|
1196
|
+
}
|
|
1197
|
+
var productBlock = defineBlock({
|
|
1198
|
+
type: "product",
|
|
1199
|
+
label: "Product",
|
|
1200
|
+
category: "Commerce",
|
|
1201
|
+
props: {
|
|
1202
|
+
modelSlug: fields.singleLine({ label: "Model slug" }),
|
|
1203
|
+
slugField: fields.singleLine({ label: "Slug field" }),
|
|
1204
|
+
slug: fields.singleLine({ label: "Product slug" }),
|
|
1205
|
+
nameField: fields.singleLine({ label: "Name field" }),
|
|
1206
|
+
priceField: fields.singleLine({ label: "Price field" }),
|
|
1207
|
+
imageField: fields.singleLine({ label: "Image field" })
|
|
1208
|
+
},
|
|
1209
|
+
component: ProductComponent
|
|
1210
|
+
});
|
|
1211
|
+
function CartComponent() {
|
|
1212
|
+
const {
|
|
1213
|
+
cart,
|
|
1214
|
+
loading,
|
|
1215
|
+
error,
|
|
1216
|
+
updateItem,
|
|
1217
|
+
removeItem,
|
|
1218
|
+
applyDiscount,
|
|
1219
|
+
removeDiscount
|
|
1220
|
+
} = useCart();
|
|
1221
|
+
const [code, setCode] = react.useState("");
|
|
1222
|
+
const [busy, setBusy] = react.useState(false);
|
|
1223
|
+
if (loading) {
|
|
1224
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-cmssy-cart": "loading", children: "Loading\u2026" });
|
|
1225
|
+
}
|
|
1226
|
+
if (!cart || cart.items.length === 0) {
|
|
1227
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-cmssy-cart": "empty", children: "Your cart is empty." });
|
|
1228
|
+
}
|
|
1229
|
+
const currency = cart.currency ?? "USD";
|
|
1230
|
+
async function withBusy(fn) {
|
|
1231
|
+
setBusy(true);
|
|
1232
|
+
try {
|
|
1233
|
+
await fn();
|
|
1234
|
+
} catch {
|
|
1235
|
+
setBusy(false);
|
|
1236
|
+
} finally {
|
|
1237
|
+
setBusy(false);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { "data-cmssy-cart": cart.id, children: [
|
|
1241
|
+
error ? /* @__PURE__ */ jsxRuntime.jsx("p", { "data-cmssy-cart-error": true, children: error }) : null,
|
|
1242
|
+
/* @__PURE__ */ jsxRuntime.jsx("ul", { "data-cmssy-cart-items": true, children: cart.items.map((item) => /* @__PURE__ */ jsxRuntime.jsxs("li", { "data-cmssy-cart-item": item.id, children: [
|
|
1243
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { "data-cmssy-item-name": true, children: item.snapshot.name }),
|
|
1244
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1245
|
+
"input",
|
|
1246
|
+
{
|
|
1247
|
+
type: "number",
|
|
1248
|
+
min: "1",
|
|
1249
|
+
value: item.quantity,
|
|
1250
|
+
disabled: busy,
|
|
1251
|
+
onChange: (e) => {
|
|
1252
|
+
const qty = Number(e.target.value);
|
|
1253
|
+
if (Number.isInteger(qty) && qty >= 1) {
|
|
1254
|
+
void withBusy(() => updateItem(item.id, qty));
|
|
1255
|
+
}
|
|
1256
|
+
},
|
|
1257
|
+
"data-cmssy-item-qty": true
|
|
1258
|
+
}
|
|
1259
|
+
),
|
|
1260
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { "data-cmssy-item-price": true, children: formatPrice(item.snapshot.price * item.quantity, currency) }),
|
|
1261
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1262
|
+
"button",
|
|
1263
|
+
{
|
|
1264
|
+
type: "button",
|
|
1265
|
+
disabled: busy,
|
|
1266
|
+
onClick: () => void withBusy(() => removeItem(item.id)),
|
|
1267
|
+
"data-cmssy-item-remove": true,
|
|
1268
|
+
children: "Remove"
|
|
1269
|
+
}
|
|
1270
|
+
)
|
|
1271
|
+
] }, item.id)) }),
|
|
1272
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { "data-cmssy-cart-discount": true, children: cart.appliedDiscount ? /* @__PURE__ */ jsxRuntime.jsxs("p", { children: [
|
|
1273
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: cart.appliedDiscount.code }),
|
|
1274
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1275
|
+
"button",
|
|
1276
|
+
{
|
|
1277
|
+
type: "button",
|
|
1278
|
+
disabled: busy,
|
|
1279
|
+
onClick: () => void withBusy(() => removeDiscount()),
|
|
1280
|
+
children: "Remove"
|
|
1281
|
+
}
|
|
1282
|
+
)
|
|
1283
|
+
] }) : /* @__PURE__ */ jsxRuntime.jsxs("p", { children: [
|
|
1284
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1285
|
+
"input",
|
|
1286
|
+
{
|
|
1287
|
+
type: "text",
|
|
1288
|
+
value: code,
|
|
1289
|
+
placeholder: "Discount code",
|
|
1290
|
+
onChange: (e) => setCode(e.target.value),
|
|
1291
|
+
disabled: busy
|
|
1292
|
+
}
|
|
1293
|
+
),
|
|
1294
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1295
|
+
"button",
|
|
1296
|
+
{
|
|
1297
|
+
type: "button",
|
|
1298
|
+
disabled: busy || !code.trim(),
|
|
1299
|
+
onClick: () => void withBusy(async () => {
|
|
1300
|
+
await applyDiscount(code.trim());
|
|
1301
|
+
setCode("");
|
|
1302
|
+
}),
|
|
1303
|
+
children: "Apply"
|
|
1304
|
+
}
|
|
1305
|
+
)
|
|
1306
|
+
] }) }),
|
|
1307
|
+
/* @__PURE__ */ jsxRuntime.jsxs("dl", { "data-cmssy-cart-totals": true, children: [
|
|
1308
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
1309
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Subtotal" }),
|
|
1310
|
+
/* @__PURE__ */ jsxRuntime.jsx("dd", { children: formatPrice(cart.subtotal, currency) })
|
|
1311
|
+
] }),
|
|
1312
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
1313
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Total" }),
|
|
1314
|
+
/* @__PURE__ */ jsxRuntime.jsx("dd", { "data-cmssy-cart-total": true, children: formatPrice(cart.discountedTotal, currency) })
|
|
1315
|
+
] })
|
|
1316
|
+
] })
|
|
1317
|
+
] });
|
|
1318
|
+
}
|
|
1319
|
+
var cartBlock = defineBlock({
|
|
1320
|
+
type: "cart",
|
|
1321
|
+
label: "Cart",
|
|
1322
|
+
category: "Commerce",
|
|
1323
|
+
props: {},
|
|
1324
|
+
component: CartComponent
|
|
1325
|
+
});
|
|
1326
|
+
var EMAIL_RE = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;
|
|
1327
|
+
function CheckoutComponent({ content }) {
|
|
1328
|
+
const c = content;
|
|
1329
|
+
const { cart, loading, checkout } = useCart();
|
|
1330
|
+
const [email, setEmail] = react.useState("");
|
|
1331
|
+
const [busy, setBusy] = react.useState(false);
|
|
1332
|
+
const [error, setError] = react.useState(null);
|
|
1333
|
+
const [order, setOrder] = react.useState(null);
|
|
1334
|
+
if (order) {
|
|
1335
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { "data-cmssy-checkout": "done", children: [
|
|
1336
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { children: c.successMessage ?? "Order placed - awaiting payment." }),
|
|
1337
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { "data-cmssy-order-id": true, children: order.id }),
|
|
1338
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { "data-cmssy-order-total": true, children: formatPrice(order.total, order.currency) })
|
|
1339
|
+
] });
|
|
1340
|
+
}
|
|
1341
|
+
if (loading) {
|
|
1342
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-cmssy-checkout": "loading", children: "Loading\u2026" });
|
|
1343
|
+
}
|
|
1344
|
+
if (!cart || cart.items.length === 0) {
|
|
1345
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-cmssy-checkout": "empty", children: "Your cart is empty." });
|
|
1346
|
+
}
|
|
1347
|
+
const emailValid = EMAIL_RE.test(email.trim());
|
|
1348
|
+
async function onSubmit() {
|
|
1349
|
+
setBusy(true);
|
|
1350
|
+
setError(null);
|
|
1351
|
+
try {
|
|
1352
|
+
setOrder(await checkout(email.trim()));
|
|
1353
|
+
} catch (err) {
|
|
1354
|
+
setError(err instanceof Error ? err.message : "Checkout failed");
|
|
1355
|
+
} finally {
|
|
1356
|
+
setBusy(false);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
1360
|
+
"form",
|
|
1361
|
+
{
|
|
1362
|
+
"data-cmssy-checkout": cart.id,
|
|
1363
|
+
onSubmit: (e) => {
|
|
1364
|
+
e.preventDefault();
|
|
1365
|
+
if (emailValid && !busy) void onSubmit();
|
|
1366
|
+
},
|
|
1367
|
+
children: [
|
|
1368
|
+
error ? /* @__PURE__ */ jsxRuntime.jsx("p", { "data-cmssy-checkout-error": true, children: error }) : null,
|
|
1369
|
+
/* @__PURE__ */ jsxRuntime.jsxs("label", { children: [
|
|
1370
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: "Email" }),
|
|
1371
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1372
|
+
"input",
|
|
1373
|
+
{
|
|
1374
|
+
type: "email",
|
|
1375
|
+
value: email,
|
|
1376
|
+
onChange: (e) => setEmail(e.target.value),
|
|
1377
|
+
required: true,
|
|
1378
|
+
"data-cmssy-checkout-email": true
|
|
1379
|
+
}
|
|
1380
|
+
)
|
|
1381
|
+
] }),
|
|
1382
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { "data-cmssy-checkout-total": true, children: formatPrice(cart.discountedTotal, cart.currency ?? "USD") }),
|
|
1383
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1384
|
+
"button",
|
|
1385
|
+
{
|
|
1386
|
+
type: "submit",
|
|
1387
|
+
disabled: busy || !emailValid,
|
|
1388
|
+
"data-cmssy-checkout-submit": true,
|
|
1389
|
+
children: busy ? "Placing order\u2026" : "Place order"
|
|
1390
|
+
}
|
|
1391
|
+
)
|
|
1392
|
+
]
|
|
1393
|
+
}
|
|
1394
|
+
);
|
|
1395
|
+
}
|
|
1396
|
+
var checkoutBlock = defineBlock({
|
|
1397
|
+
type: "checkout",
|
|
1398
|
+
label: "Checkout",
|
|
1399
|
+
category: "Commerce",
|
|
1400
|
+
props: {
|
|
1401
|
+
successMessage: fields.singleLine({ label: "Success message" })
|
|
1402
|
+
},
|
|
1403
|
+
component: CheckoutComponent
|
|
1404
|
+
});
|
|
750
1405
|
|
|
1406
|
+
exports.CmssyAuthProvider = CmssyAuthProvider;
|
|
1407
|
+
exports.CmssyCommerceProvider = CmssyCommerceProvider;
|
|
751
1408
|
exports.CmssyEditableLayout = CmssyEditableLayout;
|
|
752
1409
|
exports.CmssyEditablePage = CmssyEditablePage;
|
|
753
1410
|
exports.CmssyLazyEditor = CmssyLazyEditor;
|
|
754
1411
|
exports.CmssyLazyLayout = CmssyLazyLayout;
|
|
1412
|
+
exports.cartBlock = cartBlock;
|
|
1413
|
+
exports.checkoutBlock = checkoutBlock;
|
|
1414
|
+
exports.formatPrice = formatPrice;
|
|
1415
|
+
exports.fractionDigits = fractionDigits;
|
|
1416
|
+
exports.fromMinorUnits = fromMinorUnits;
|
|
1417
|
+
exports.productBlock = productBlock;
|
|
1418
|
+
exports.useCart = useCart;
|
|
1419
|
+
exports.useCmssyUser = useCmssyUser;
|
|
755
1420
|
exports.useEditBridge = useEditBridge;
|