Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 127 additions & 120 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@telegram-apps/telegram-ui": "^2.1.8",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"countries-list": "^3.1.1",
"date-fns": "^4.1.0",
"framer-motion": "^11.3.31",
"lucide-react": "^0.525.0",
Expand Down
49 changes: 42 additions & 7 deletions src/app/subscription/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import { cn } from "@/lib/cn";
import LoadingPage, { LoadingSpinner } from "@/components/LoadingPage";
import ListItemSkeleton from "@/components/skeletons/ListItemSkeleton";
import Image from "next/image";
import { Button, Input } from "@telegram-apps/telegram-ui";
import { Button, Input, Select } from "@telegram-apps/telegram-ui";
import { usePayHereRedirect } from "@/lib/hooks";
import { createUserSchema, type CreateUserFormData } from "@/lib/validations";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { countries as countryData } from "countries-list";

const countryOptions = Object.values(countryData)
.map((c) => c.name)
.sort((a, b) => a.localeCompare(b));

export default function SubscriptionPage() {
const router = useRouter();
Expand All @@ -31,6 +36,7 @@ export default function SubscriptionPage() {
const {
register,
handleSubmit,
setValue,
formState: { errors, isValid },
} = useForm<CreateUserFormData>({
resolver: zodResolver(createUserSchema),
Expand All @@ -39,7 +45,7 @@ export default function SubscriptionPage() {
first_name: "",
last_name: "",
email: "",
phone: "",
phone: "+94",
address: "",
city: "",
country: "",
Expand Down Expand Up @@ -102,6 +108,22 @@ export default function SubscriptionPage() {
fetchPackages();
}, []);

useEffect(() => {
const detectCountry = async () => {
try {
const res = await fetch("https://ipapi.co/json/");
const data = await res.json();
if (data?.country_name && countryOptions.includes(data.country_name)) {
setValue("country", data.country_name);
}
} catch (err) {
console.error("Failed to detect country:", err);
}
};

detectCountry();
}, [setValue]);

// Handle Telegram back button
useEffect(() => {
if (showRegistrationForm) {
Expand Down Expand Up @@ -481,7 +503,13 @@ export default function SubscriptionPage() {
Phone <span className="text-red-500">*</span>
</>
}
{...register("phone")}
{...register("phone", {
onChange: (e) => {
if (!e.target.value.startsWith("+94")) {
e.target.value = "+94";
}
},
})}
placeholder="e.g. +94771234567"
/>
{errors.phone && (
Expand Down Expand Up @@ -530,17 +558,24 @@ export default function SubscriptionPage() {
</div>
{/* Country */}
<div>
<Input
type="text"
<Select
status={errors.country ? "error" : undefined}
header={
<>
Country <span className="text-red-500">*</span>
</>
}
{...register("country")}
placeholder="Enter your country"
/>
>
<option value="" disabled>
Select your country
</option>
{countryOptions.map((name) => (
<option key={name} value={name}>
{name}
</option>
))}
</Select>
{errors.country && (
<p className="pl-6 text-sm text-red-500">
{errors.country.message}
Expand Down