feat: implement user profile page and integrate profile fetching logic in AccountPopover and ApplicationLayout components

This commit is contained in:
2025-07-11 14:41:06 +05:00
parent d1fdcc873a
commit a20c939c91
4 changed files with 152 additions and 82 deletions

View File

@ -1,8 +1,9 @@
"use server"; "use server";
import { VerifyUserDetails } from "@/lib/person";
import { revalidatePath } from "next/cache"; import { getServerSession } from "next-auth";
import { redirect } from "next/navigation"; import { authOptions } from "@/app/auth";
import { CreateClient } from "./ninja/client"; import type { User, UserProfile } from "@/lib/types/user";
import { handleApiResponse } from "@/utils/tryCatch";
export async function VerifyUser(userId: string) { export async function VerifyUser(userId: string) {
// const user = await prisma.user.findUnique({ // const user = await prisma.user.findUnique({
@ -52,72 +53,37 @@ export async function VerifyUser(userId: string) {
// revalidatePath("/users"); // revalidatePath("/users");
} }
export async function Rejectuser({ export async function getProfile() {
userId, const session = await getServerSession(authOptions);
reason, const response = await fetch(
}: { userId: string; reason: string }) { `${process.env.SARLINK_API_BASE_URL}/api/auth/profile/`,
// const user = await prisma.user.findUnique({ {
// where: { method: "GET",
// id: userId,
// },
// });
// if (!user) {
// throw new Error("User not found");
// }
// await SendUserRejectionDetailSMS({
// details: reason,
// phoneNumber: user.phoneNumber,
// });
// await prisma.user.delete({
// where: {
// id: userId,
// },
// });
revalidatePath("/users");
redirect("/users");
}
export const SendUserRejectionDetailSMS = async ({
details,
phoneNumber,
}: {
details: string;
phoneNumber: string;
}) => {
try {
const respose = await fetch(`${process.env.SMS_API_BASE_URL}/api/sms`, {
method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${process.env.SMS_API_KEY}`, Authorization: `Token ${session?.apiToken}`,
}, },
body: JSON.stringify({ },
check_delivery: false, );
number: phoneNumber,
message: details,
}),
});
const data = await respose.json();
console.log(data);
return data;
} catch (error) {
console.error(error);
}
};
export async function AddDevice({ return handleApiResponse<User>(response, "getProfile");
name,
mac_address,
user_id,
}: { name: string; mac_address: string; user_id: string }) {
// const newDevice = await prisma.device.create({
// data: {
// name: name,
// mac: mac_address,
// userId: user_id,
// },
// });
revalidatePath("/devices");
// return newDevice;
} }
export async function getProfileById(userId: string) {
const session = await getServerSession(authOptions);
const response = await fetch(
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/${userId}/`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${session?.apiToken}`,
},
},
);
return handleApiResponse<UserProfile>(response, "getProfilebyId");
}

View File

@ -0,0 +1,95 @@
import { redirect } from "next/navigation";
import { getServerSession } from "next-auth";
import { getProfileById } from "@/actions/user-actions";
import { authOptions } from "@/app/auth";
import ClientErrorMessage from "@/components/client-error-message";
import { Badge } from "@/components/ui/badge";
import { FloatingLabelInput } from "@/components/ui/floating-label";
import { tryCatch } from "@/utils/tryCatch";
export default async function Profile() {
const session = await getServerSession(authOptions);
if (!session?.user) return redirect("/auth/signin?callbackUrl=/profile");
const [error, profile] = await tryCatch(getProfileById(session?.user.id));
if (error) {
if (error.message === "Invalid token.") redirect("/auth/signin");
return <ClientErrorMessage message={error.message} />;
}
return (
<div>
<div className="flex justify-between items-center font-bold border rounded-md border-dashed title-bg py-4 px-2 mb-4">
<h3 className="text-sarLinkOrange text-2xl">Profile</h3>
<div className="text-sarLinkOrange uppercase font-mono text-sm flex flex-col items-center rounded gap-2 py-2 px-4">
<span>Profile Status</span>
{verifiedStatus(profile?.verified ?? false)}
</div>
</div>
<fieldset>
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 max-w-4xl">
<FloatingLabelInput
id="floating-name"
label="Full Name"
value={`${profile?.first_name} ${profile?.last_name}`}
readOnly
/>
<FloatingLabelInput
id="floating-id-card"
label="ID Card"
value={`${profile?.id_card}`}
readOnly
/>
<FloatingLabelInput
id="floating-island"
label="Island"
value={`${profile?.atoll.name}. ${profile?.island.name}`}
readOnly
/>
<FloatingLabelInput
id="floating-dob"
label="Date of Birth"
value={`${new Date(
profile?.dob.toString() ?? "",
).toLocaleDateString("en-US", {
month: "short",
day: "2-digit",
year: "numeric",
})}`}
readOnly
/>
<FloatingLabelInput
id="floating-address"
label="Address"
value={`${profile?.address}`}
readOnly
/>
<FloatingLabelInput
id="floating-mobile"
label="Phone Number"
value={`${profile?.mobile}`}
readOnly
/>
<FloatingLabelInput
id="floating-account"
label="Account Number"
value={`${profile?.acc_no}`}
readOnly
/>
</div>
</fieldset>
{/* <Suspense key={query} fallback={"loading...."}>
<TopupsTable searchParams={searchParams} />
</Suspense> */}
</div>
);
}
function verifiedStatus(status: boolean) {
switch (status) {
case true:
return <Badge className="bg-green-500 text-white">Verified</Badge>;
case false:
return <Badge className="bg-red-500 text-white">Not Verified</Badge>;
default:
return <Badge className="bg-yellow-500 text-white">Unknown</Badge>;
}
}

View File

@ -1,13 +1,14 @@
"use client"; "use client";
import { Loader2, User as UserIcon } from "lucide-react";
import Link from "next/link";
import { signOut, useSession } from "next-auth/react";
import { useState } from "react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { import {
Popover, Popover,
PopoverContent, PopoverContent,
PopoverTrigger, PopoverTrigger,
} from "@/components/ui/popover"; } from "@/components/ui/popover";
import { Loader2, User as UserIcon } from "lucide-react";
import { signOut, useSession } from "next-auth/react";
import { useState } from "react";
export function AccountPopover() { export function AccountPopover() {
const session = useSession(); const session = useSession();
@ -36,16 +37,24 @@ export function AccountPopover() {
<p>{session.data?.user?.mobile}</p> <p>{session.data?.user?.mobile}</p>
</div> </div>
</div> </div>
<Button <div className="flex flex-col gap-2">
disabled={loading} <Button
onClick={async () => { disabled={loading}
setLoading(true); onClick={async () => {
await signOut(); setLoading(true);
setLoading(false); await signOut();
}} setLoading(false);
> }}
{loading ? <Loader2 className="animate-spin" /> : "Logout"} >
</Button> {loading ? <Loader2 className="animate-spin" /> : "Logout"}
</Button>
<Link href="/profile" className="text-muted-foreground">
<Button variant={"secondary"} className="w-full">
View Profile
</Button>
</Link>
</div>
</div> </div>
</PopoverContent> </PopoverContent>
</Popover> </Popover>

View File

@ -1,7 +1,7 @@
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { getServerSession } from "next-auth"; import { getServerSession } from "next-auth";
import { NuqsAdapter } from 'nuqs/adapters/next/app' import { NuqsAdapter } from 'nuqs/adapters/next/app'
import { getProfile } from "@/actions/payment"; import { getProfile } from "@/actions/user-actions";
import { authOptions } from "@/app/auth"; import { authOptions } from "@/app/auth";
import { DeviceCartDrawer } from "@/components/device-cart"; import { DeviceCartDrawer } from "@/components/device-cart";
import { ModeToggle } from "@/components/theme-toggle"; import { ModeToggle } from "@/components/theme-toggle";