-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListingActions.tsx
More file actions
114 lines (98 loc) · 3.22 KB
/
ListingActions.tsx
File metadata and controls
114 lines (98 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"use client";
import { useState } from "react";
import { DollarSign } from "lucide-react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { MakeOfferModal } from "@/components/listings/offer/MakeOfferModal";
import { PhoneInputModal } from "@/components/listings/offer/PhoneInputModal";
import { VerificationCodeModal } from "@/components/listings/offer/VerificationCodeModal";
import { Button } from "@/components/ui/button";
import { getPhoneStatus } from "@/lib/actions";
interface Props {
listingId: number;
listingPrice: number;
listingOwnerLabel: string;
priceLabel?: string;
isOwner?: boolean;
}
type ModalState = "none" | "phone-input" | "verification" | "offer";
export const ListingActions = ({
listingId,
listingPrice,
priceLabel,
listingOwnerLabel,
isOwner = false,
}: Props) => {
const [modalState, setModalState] = useState<ModalState>("none");
const [pendingPhoneNumber, setPendingPhoneNumber] = useState<string>("");
const [isChangingPhone, setIsChangingPhone] = useState<boolean>(false);
const queryClient = useQueryClient();
const { data: phoneStatus } = useQuery({
queryKey: ["phoneStatus"],
queryFn: getPhoneStatus,
});
if (isOwner) {
return null;
}
const handleMakeOfferClick = () => {
if (!phoneStatus) return;
if (!phoneStatus.phone_number || !phoneStatus.phone_verified) {
// no phone number or not verified - start with phone input
// if we already have a pending phone number, go to verification
if (pendingPhoneNumber) {
setModalState("verification");
} else {
setModalState("phone-input");
}
} else {
// phone verified - show create offer modal
setModalState("offer");
}
};
const handlePhoneCodeSent = (phoneNumber: string) => {
setPendingPhoneNumber(phoneNumber);
setIsChangingPhone(false);
setModalState("verification");
};
const handlePhoneVerified = () => {
queryClient.invalidateQueries({ queryKey: ["phoneStatus"] });
setPendingPhoneNumber("");
setModalState("offer");
};
const handleChangePhone = () => {
setIsChangingPhone(true);
setModalState("phone-input");
};
return (
<>
<Button
onClick={handleMakeOfferClick}
className="bg-brand hover:bg-brand-hover h-12 w-full cursor-pointer text-base text-white"
>
<DollarSign className="mr-2 h-5 w-5" />
Make an Offer
</Button>
<PhoneInputModal
isOpen={modalState === "phone-input"}
onClose={() => setModalState("none")}
onCodeSent={handlePhoneCodeSent}
listingOwnerLabel={listingOwnerLabel}
initialPhoneNumber={isChangingPhone ? "" : phoneStatus?.phone_number || ""}
/>
<VerificationCodeModal
isOpen={modalState === "verification"}
onClose={() => setModalState("none")}
phoneNumber={pendingPhoneNumber}
onVerified={handlePhoneVerified}
/>
<MakeOfferModal
isOpen={modalState === "offer"}
onClose={() => setModalState("none")}
listingId={listingId}
listingPrice={listingPrice}
listingOwnerLabel={listingOwnerLabel}
priceLabel={priceLabel}
onChangePhone={handleChangePhone}
/>
</>
);
};