style: clean up code formatting and remove trailing whitespace
refactor: reorganize imports and fix type imports
This commit is contained in:
1 parent
d3704602fd
commit
47c04de6fe
232 files changed
+7140
-4798
No files matched your search
@@ -1,6 +1,9 @@
|
||||
"use client";
|
||||
|
||||
import { Coins, DollarSign, Loader2, RefreshCw } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -8,13 +11,10 @@ import {
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { DollarSign, Coins, RefreshCw, Loader2 } from "lucide-react";
|
||||
import { useSession } from "@/lib/hooks/use-session";
|
||||
import { Input } from "@/components/ui/input";
|
||||
|
||||
interface Listing {
|
||||
listing_id: number;
|
||||
@@ -37,7 +37,12 @@ interface PurchaseModalProps {
|
||||
onSuccess?: () => void;
|
||||
}
|
||||
|
||||
export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: PurchaseModalProps) {
|
||||
export function PurchaseModal({
|
||||
open,
|
||||
onOpenChange,
|
||||
listing,
|
||||
onSuccess,
|
||||
}: PurchaseModalProps) {
|
||||
const { user } = useSession();
|
||||
const [selectedMethod, setSelectedMethod] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
@@ -53,7 +58,10 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedMethod && !["pix", "tibia_coins", "gold_coins"].includes(selectedMethod)) {
|
||||
if (
|
||||
selectedMethod &&
|
||||
!["pix", "tibia_coins", "gold_coins"].includes(selectedMethod)
|
||||
) {
|
||||
setCustomOfferValue("");
|
||||
}
|
||||
}, [selectedMethod]);
|
||||
@@ -61,13 +69,19 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
if (!listing) return null;
|
||||
|
||||
// Determinar métodos de pagamento disponíveis
|
||||
const availableMethods: Array<{ type: string; label: string; value: string; icon: any }> = [];
|
||||
|
||||
const availableMethods: Array<{
|
||||
type: string;
|
||||
label: string;
|
||||
value: string;
|
||||
icon: any;
|
||||
}> = [];
|
||||
|
||||
if (listing.price_pix) {
|
||||
const price = typeof listing.price_pix === 'string'
|
||||
? parseFloat(listing.price_pix)
|
||||
: listing.price_pix;
|
||||
if (!isNaN(price)) {
|
||||
const price =
|
||||
typeof listing.price_pix === "string"
|
||||
? parseFloat(listing.price_pix)
|
||||
: listing.price_pix;
|
||||
if (!Number.isNaN(price)) {
|
||||
availableMethods.push({
|
||||
type: "pix",
|
||||
label: "PIX",
|
||||
@@ -78,10 +92,11 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
}
|
||||
|
||||
if (listing.price_tibia_coins) {
|
||||
const price = typeof listing.price_tibia_coins === 'string'
|
||||
? parseInt(listing.price_tibia_coins, 10)
|
||||
: listing.price_tibia_coins;
|
||||
if (!isNaN(price)) {
|
||||
const price =
|
||||
typeof listing.price_tibia_coins === "string"
|
||||
? parseInt(listing.price_tibia_coins, 10)
|
||||
: listing.price_tibia_coins;
|
||||
if (!Number.isNaN(price)) {
|
||||
availableMethods.push({
|
||||
type: "tibia_coins",
|
||||
label: "Tibia Coins",
|
||||
@@ -92,10 +107,11 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
}
|
||||
|
||||
if (listing.price_gold_coins) {
|
||||
const price = typeof listing.price_gold_coins === 'string'
|
||||
? parseInt(listing.price_gold_coins, 10)
|
||||
: listing.price_gold_coins;
|
||||
if (!isNaN(price)) {
|
||||
const price =
|
||||
typeof listing.price_gold_coins === "string"
|
||||
? parseInt(listing.price_gold_coins, 10)
|
||||
: listing.price_gold_coins;
|
||||
if (!Number.isNaN(price)) {
|
||||
availableMethods.push({
|
||||
type: "gold_coins",
|
||||
label: "Gold Coins",
|
||||
@@ -105,7 +121,11 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
}
|
||||
}
|
||||
|
||||
if (listing.trade_item_id && listing.trade_item_name && listing.trade_item_quantity) {
|
||||
if (
|
||||
listing.trade_item_id &&
|
||||
listing.trade_item_name &&
|
||||
listing.trade_item_quantity
|
||||
) {
|
||||
availableMethods.push({
|
||||
type: "trade",
|
||||
label: "Troca",
|
||||
@@ -114,7 +134,10 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
});
|
||||
}
|
||||
|
||||
const formatOffer = (amount: number | string | null, currency: string | null) => {
|
||||
const formatOffer = (
|
||||
amount: number | string | null,
|
||||
currency: string | null,
|
||||
) => {
|
||||
if (amount === null || amount === undefined || !currency) {
|
||||
return null;
|
||||
}
|
||||
@@ -136,7 +159,7 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
|
||||
const highestOfferLabel = formatOffer(
|
||||
listing.highest_custom_offer_amount ?? null,
|
||||
listing.highest_custom_offer_currency ?? null
|
||||
listing.highest_custom_offer_currency ?? null,
|
||||
);
|
||||
|
||||
// Se houver apenas um método, selecionar automaticamente
|
||||
@@ -167,20 +190,28 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
payment_method: selectedMethod,
|
||||
};
|
||||
|
||||
if (hasCustomOffer && ["pix", "tibia_coins", "gold_coins"].includes(selectedMethod)) {
|
||||
if (
|
||||
hasCustomOffer &&
|
||||
["pix", "tibia_coins", "gold_coins"].includes(selectedMethod)
|
||||
) {
|
||||
const offerNumber = Number(customOfferValue);
|
||||
payload.custom_offer_amount =
|
||||
selectedMethod === "pix" ? Number(offerNumber.toFixed(2)) : Math.floor(offerNumber);
|
||||
selectedMethod === "pix"
|
||||
? Number(offerNumber.toFixed(2))
|
||||
: Math.floor(offerNumber);
|
||||
payload.custom_offer_currency = selectedMethod;
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/listings/${listing.listing_id}/purchase`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
const response = await fetch(
|
||||
`/api/listings/${listing.listing_id}/purchase`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
@@ -236,10 +267,16 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm font-medium">Valores anunciados pelo vendedor</p>
|
||||
<p className="text-sm font-medium">
|
||||
Valores anunciados pelo vendedor
|
||||
</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{availableMethods.map((method) => (
|
||||
<Badge key={`original-${method.type}`} variant="secondary" className="gap-1">
|
||||
<Badge
|
||||
key={`original-${method.type}`}
|
||||
variant="secondary"
|
||||
className="gap-1"
|
||||
>
|
||||
{method.label}: {method.value}
|
||||
</Badge>
|
||||
))}
|
||||
@@ -287,15 +324,21 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
) : availableMethods.length === 1 ? (
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm font-medium">Método de Pagamento</Label>
|
||||
<div className={`p-4 rounded-lg border-2 ${getMethodClassName(availableMethods[0].type)}`}>
|
||||
<div
|
||||
className={`p-4 rounded-lg border-2 ${getMethodClassName(availableMethods[0].type)}`}
|
||||
>
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
{(() => {
|
||||
const Icon = availableMethods[0].icon;
|
||||
return <Icon className="h-4 w-4" />;
|
||||
})()}
|
||||
<span className="font-medium">{availableMethods[0].label}</span>
|
||||
<span className="font-medium">
|
||||
{availableMethods[0].label}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-sm opacity-90">{availableMethods[0].value}</p>
|
||||
<p className="text-sm opacity-90">
|
||||
{availableMethods[0].value}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
@@ -308,7 +351,8 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
["pix", "tibia_coins", "gold_coins"].includes(selectedMethod) && (
|
||||
<div className="space-y-2">
|
||||
<Label className="text-sm font-medium">
|
||||
Propor valor personalizado ({selectedMethod === "pix" ? "R$" : "quantidade"})
|
||||
Propor valor personalizado (
|
||||
{selectedMethod === "pix" ? "R$" : "quantidade"})
|
||||
</Label>
|
||||
<Input
|
||||
type="number"
|
||||
@@ -316,11 +360,13 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
step={selectedMethod === "pix" ? "0.01" : "1"}
|
||||
value={customOfferValue}
|
||||
onChange={(event) => setCustomOfferValue(event.target.value)}
|
||||
placeholder={selectedMethod === "pix" ? "Ex.: 199.90" : "Ex.: 1200"}
|
||||
placeholder={
|
||||
selectedMethod === "pix" ? "Ex.: 199.90" : "Ex.: 1200"
|
||||
}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Esse valor será enviado ao vendedor como sua proposta. Ele pode aceitar, negar ou
|
||||
responder.
|
||||
Esse valor será enviado ao vendedor como sua proposta. Ele
|
||||
pode aceitar, negar ou responder.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
@@ -344,7 +390,9 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handlePurchase}
|
||||
disabled={loading || !selectedMethod || availableMethods.length === 0}
|
||||
disabled={
|
||||
loading || !selectedMethod || availableMethods.length === 0
|
||||
}
|
||||
>
|
||||
{loading ? (
|
||||
<>
|
||||
@@ -361,4 +409,3 @@ export function PurchaseModal({ open, onOpenChange, listing, onSuccess }: Purcha
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user