first commit

This commit is contained in:
Nermin Karic
2026-11-22 21:49:17 +01:00
commit f5e2fcbf98
693 changed files with 82975 additions and 0 deletions

View File

@@ -0,0 +1,150 @@
"use client"
import React, { useState } from "react"
import { applyPromotions } from "@lib/data/cart"
import { convertToLocale } from "@lib/util/money"
import { HttpTypes } from "@medusajs/types"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Badge } from "@/components/ui/badge"
import { Trash2, Tag } from "lucide-react"
import ErrorMessage from "../error-message"
import { SubmitButton } from "../submit-button"
type DiscountCodeProps = {
cart: HttpTypes.StoreCart
}
const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
const [isOpen, setIsOpen] = useState(false)
const [errorMessage, setErrorMessage] = useState("")
const { promotions = [] } = cart
const removePromotionCode = async (code: string) => {
const validPromotions = promotions.filter(
(promotion) => promotion.code !== code
)
await applyPromotions(
validPromotions.filter((p) => p.code !== undefined).map((p) => p.code!)
)
}
const addPromotionCode = async (formData: FormData) => {
setErrorMessage("")
const code = formData.get("code")
if (!code) {
return
}
const input = document.getElementById("promotion-input") as HTMLInputElement
const codes = promotions
.filter((p) => p.code !== undefined)
.map((p) => p.code!)
codes.push(code.toString())
try {
await applyPromotions(codes)
} catch (e) {
setErrorMessage(e instanceof Error ? e.message : String(e))
}
if (input) {
input.value = ""
}
}
return (
<div className="w-full flex flex-col gap-2">
<form action={(a) => addPromotionCode(a)} className="w-full">
<button
onClick={() => setIsOpen(!isOpen)}
type="button"
className="text-xs font-medium text-primary hover:underline flex items-center gap-1.5 py-1"
data-testid="add-discount-button"
>
<Tag className="h-3.5 w-3.5" />
<span>{isOpen ? "Zatvori unos koda" : "Imate promo kod ili popust?"}</span>
</button>
{isOpen && (
<div className="flex flex-col gap-2 mt-2">
<div className="flex gap-2">
<Input
id="promotion-input"
name="code"
type="text"
placeholder="Unesite promo kod"
className="h-9 text-xs"
data-testid="discount-input"
/>
<SubmitButton
variant="secondary"
data-testid="discount-apply-button"
>
Primijeni
</SubmitButton>
</div>
<ErrorMessage
error={errorMessage}
data-testid="discount-error-message"
/>
</div>
)}
</form>
{promotions.length > 0 && (
<div className="flex flex-col gap-2 mt-2 pt-2 border-t border-border">
<span className="text-xs font-semibold text-foreground">
Primijenjeni popusti:
</span>
{promotions.map((promotion) => (
<div
key={promotion.id}
className="flex items-center justify-between text-xs"
data-testid="discount-row"
>
<div className="flex items-center gap-2">
<Badge variant={promotion.is_automatic ? "secondary" : "outline"} className="text-xs">
{promotion.code}
</Badge>
{promotion.application_method?.value !== undefined &&
promotion.application_method.currency_code !== undefined && (
<span className="text-muted-foreground">
({promotion.application_method.type === "percentage"
? `${promotion.application_method.value}%`
: convertToLocale({
amount: +promotion.application_method.value,
currency_code:
promotion.application_method.currency_code,
})})
</span>
)}
</div>
{!promotion.is_automatic && (
<button
type="button"
className="text-muted-foreground hover:text-destructive p-1"
onClick={() => {
if (promotion.code) {
removePromotionCode(promotion.code)
}
}}
data-testid="remove-discount-button"
aria-label="Ukloni promo kod"
>
<Trash2 className="h-3.5 w-3.5" />
</button>
)}
</div>
))}
</div>
)}
</div>
)
}
export default DiscountCode