generated from cameronking4/VapiBlocks
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathphone.tsx
More file actions
179 lines (165 loc) · 6.05 KB
/
phone.tsx
File metadata and controls
179 lines (165 loc) · 6.05 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"use client";
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuSeparator, DropdownMenuGroup, DropdownMenuItem } from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import React, { SetStateAction, JSX, SVGProps, useState, useRef, useEffect } from "react";
import { Toaster, toast } from 'sonner';
const phoneNumberId = process.env.NEXT_PUBLIC_VAPI_PHONE_ID;
const assistantId = process.env.NEXT_PUBLIC_VAPI_ASSISTANT_ID;
export default function PhoneInputForm() {
const [countryCode, setCountryCode] = useState("+1");
const [phoneNumber, setPhoneNumber] = useState(Array(10).fill(""));
const [error, setError] = useState("");
const inputRefs = useRef<(HTMLInputElement | null)[]>([]);
const handleCountrySelect = (code: SetStateAction<string>) => {
setCountryCode(code);
};
const handlePhoneNumberChange = (value: string, index: number) => {
if (!/^[0-9]$/.test(value)) {
return;
}
const newPhoneNumber = [...phoneNumber];
newPhoneNumber[index] = value;
setPhoneNumber(newPhoneNumber);
// Move focus to the next input field
if (value && index < inputRefs.current.length - 1) {
inputRefs.current[index + 1]?.focus();
}
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>, index: number) => {
if (e.key === 'Backspace') {
const newPhoneNumber = [...phoneNumber];
if (newPhoneNumber[index]) {
newPhoneNumber[index] = "";
setPhoneNumber(newPhoneNumber);
} else if (index > 0) {
inputRefs.current[index - 1]?.focus();
newPhoneNumber[index - 1] = "";
setPhoneNumber(newPhoneNumber);
}
}
};
const validatePhoneNumber = (number: any[]) => {
return number.every((digit: string) => /^[0-9]$/.test(digit));
};
const handleSubmit = (e: { preventDefault: () => void; }) => {
e.preventDefault();
const strippedPhoneNumber = countryCode + phoneNumber.join('').replace(/\s+/g, '');
if (validatePhoneNumber(phoneNumber)) {
setError("");
makeCall(strippedPhoneNumber);
toast.success(`Dialing ${strippedPhoneNumber}`);
console.log(`Phone number: ${strippedPhoneNumber}`);
} else {
setError("Please enter a valid 10-digit phone number.");
}
};
const makeCall = async (number : string) => {
console.log("Making phone call");
const response = await fetch('/api/vapi/make-call', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
phoneNumberId: phoneNumberId,
assistantId: assistantId,
customerNumber: number,
}),
});
const result = await response.json();
console.log(result);
};
useEffect(() => {
inputRefs.current = inputRefs.current.slice(0, phoneNumber.length);
}, [phoneNumber.length]);
return (
<>
<Toaster position="bottom-right" /> {/* Add the Toaster component */}
<form onSubmit={handleSubmit} className="mx-auto px-2 items-center">
<p className="mb-2">Enter your phone number to get called by AI Blocks.</p>
<div className="flex items-center space-x-2 mb-2">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="text-center w-full sm:w-auto hidden sm:flex" style={{ borderRadius: 0 }}>
<span>{countryCode}</span>
<ChevronDownIcon className="w-4 h-4 ml-1" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-full p-2">
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem onClick={() => handleCountrySelect("+1")}>
<span>+1 United States</span>
</DropdownMenuItem>
<DropdownMenuItem onClick={() => handleCountrySelect("+1")}>
<span>+1 Canada</span>
</DropdownMenuItem>
<DropdownMenuItem onClick={() => handleCountrySelect("+44")}>
<span>+44 United Kingdom</span>
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
<div className="relative flex-1 flex space-x-1 phone-input-container">
{phoneNumber.map((digit, index) => (
<React.Fragment key={index}>
<Input
type="text"
value={digit}
onChange={(e) => handlePhoneNumberChange(e.target.value, index)}
onKeyDown={(e) => handleKeyDown(e, index)}
maxLength={1}
className="w-8 px-2 text-center phone-input"
ref={(el) => {
inputRefs.current[index] = el;
}}
style={{ borderRadius: 0 }} // Makes the input rectangular
/>
{(index === 2 || index === 5) && <span className="dash">-</span>}
</React.Fragment>
))}
</div>
</div>
{error && <p className="text-red-500 text-sm mb-2">{error}</p>}
<Button type="submit" size="sm" className="w-full" style={{ borderRadius: 0 }}>
Submit
</Button>
</form>
<style jsx>{`
.phone-input-container {
display: flex;
align-items: center;
justify-content: space-between;
}
.dash {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.phone-input + .dash {
margin: 0 2px;
}
`}</style>
</>
);
}
function ChevronDownIcon(props: JSX.IntrinsicAttributes & SVGProps<SVGSVGElement>) {
return (
<svg
{...props}
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m6 9 6 6 6-6" />
</svg>
);
}