import { useState } from "react";
import { Link } from "react-router";
import { formatChf } from "../shop/money";
import { useShop } from "../shop/useShop";
import "./ShopDrawer.css";
const paymentMethods = [
{ key: "Bill", badge: "BILL", label: "Bill" },
{ key: "Card", badge: "CARD", label: "Card" },
{ key: "Twint", badge: "TW", label: "Twint" },
{ key: "PayPal", badge: "PP", label: "PayPal" },
];
const notificationLabels = [
["drops_enabled", "New Drops"],
["restocks_enabled", "Restocks"],
["small_batch_enabled", "Small Batch Releases"],
["discovery_enabled", "Discovery Set Updates"],
];
function Field({ label, value, onChange, type = "text", readOnly = false }) {
return (
);
}
function AuthPanel() {
const { busy, error, login, register } = useShop();
const [mode, setMode] = useState("login");
const [form, setForm] = useState({
first_name: "",
surname: "",
email: "",
password: "",
});
const update = (key, value) => setForm((current) => ({ ...current, [key]: value }));
const submit = (event) => {
event.preventDefault();
if (mode === "login") {
login({ email: form.email, password: form.password }).catch(() => {});
return;
}
register(form).catch(() => {});
};
return (
);
}
function CartPanel() {
const {
cart,
checkout,
removeCartItem,
updateCartQuantity,
busy,
error,
user,
} = useShop();
const [address, setAddress] = useState(() => ({
street_name: user?.street_name || "",
house_number: user?.house_number || "",
zip_code: user?.zip_code || "",
city: user?.city || "",
}));
const [paymentMethod, setPaymentMethod] = useState("Bill");
const updateAddress = (key, value) =>
setAddress((current) => ({ ...current, [key]: value }));
const submit = (event) => {
event.preventDefault();
checkout({ ...address, payment_method: paymentMethod }).catch(() => {});
};
return (
);
}
function RequirementRow({ label, met, children }) {
return (
{label}
{children || (met ? "met" : "open")}
);
}
function ProfilePanel() {
const {
busy,
error,
logout,
orders,
removeProductSubscription,
updateNotifications,
updateProfile,
user,
closePanel,
} = useShop();
const [editing, setEditing] = useState(false);
const [form, setForm] = useState(user || {});
const update = (key, value) => setForm((current) => ({ ...current, [key]: value }));
const notifications = user?.notifications || {};
const restockSubscriptions = (user?.productSubscriptions || []).filter(
(subscription) => subscription.type === "restock"
);
const loyalty = user?.loyaltyStatus || {
hasDiscoverySet: false,
hasFullSize: false,
purchases: 0,
spent_cents: 0,
unlocked: false,
};
const save = () => {
updateProfile({
first_name: form.first_name,
surname: form.surname,
street_name: form.street_name,
house_number: form.house_number,
zip_code: form.zip_code,
city: form.city,
birthdate: form.birthdate,
})
.then(() => setEditing(false))
.catch(() => {});
};
const togglePreference = (key) => {
updateNotifications({ ...notifications, [key]: !notifications[key] }).catch(() => {});
};
return (
PROFILE
Hi, {user.first_name}
PROFILE INFORMATION
{!editing && (
)}
{editing ? (
<>
update("first_name", value)} />
update("surname", value)} />
update("street_name", value)} />
update("house_number", value)} />
update("zip_code", value)} />
update("city", value)} />
update("birthdate", value)} />
>
) : (
)}
DISCOUNT STATUS
{user.discoveryStatus}
{user.sampleCredits?.length > 0 && (
{user.sampleCredits.map((credit) => (
{credit.slug}: {credit.status}
))}
)}
DROP / RESTOCK PREFERENCES
{notificationLabels.map(([key, label]) => (
))}
Subscribed Restocks
{restockSubscriptions.length === 0 ? (
No product-specific restock updates yet.
) : (
restockSubscriptions.map((subscription) => (
{subscription.name}
{subscription.size_label}
))
)}
SMALL BATCH ACCESS
{loyalty.unlocked ? "Unlocked" : "Locked"}
= 3}>
{loyalty.purchases}/3 Purchases
50000}>
{formatChf(loyalty.spent_cents)} / CHF 500+
Small Batch ansehen
PURCHASES
{orders.length === 0 ? (
No orders yet.
) : (
{orders.map((order) => (
Order #{order.id}
{new Date(order.created_at).toLocaleDateString("de-CH")}
{order.items.map((item) => `${item.quantity} x ${item.product.name}`).join(", ")}
{formatChf(order.total_cents)}
))}
)}
{error &&
{error}
}
);
}
function ReadBlock({ label, value }) {
return (
{label}
{value}
);
}
function ShopDrawer() {
const { closePanel, panelOpen, panelType, user } = useShop();
return (
<>
>
);
}
export default ShopDrawer;