feat: Implement agent-user connection request system with a new service and dynamic profile card buttons.

This commit is contained in:
pradeepkumar
2026-02-02 09:59:08 +05:30
parent edfbf4a51c
commit b5106945dc
10 changed files with 1018 additions and 151 deletions

View File

@@ -1,11 +1,14 @@
'use client';
import { useState } from 'react';
import Image from 'next/image';
import Link from 'next/link';
import { useSession } from 'next-auth/react';
import { useRouter, usePathname } from 'next/navigation';
import { Tag } from './Tag';
type ConnectionStatus = 'PENDING' | 'ACCEPTED' | 'REJECTED' | null;
interface ProfileCardProps {
firstName: string;
lastName: string;
@@ -19,6 +22,9 @@ interface ProfileCardProps {
editHref?: string;
messageHref?: string;
requestsHref?: string;
connectionStatus?: ConnectionStatus;
onConnectClick?: () => void; // Callback when Connect button is clicked (for opening modal)
onUnlinkClick?: () => void; // Callback when Unlink button is clicked
}
export function ProfileCard({
@@ -34,10 +40,25 @@ export function ProfileCard({
editHref = '/agent/edit',
messageHref,
requestsHref,
connectionStatus,
onConnectClick,
onUnlinkClick,
}: ProfileCardProps) {
const { data: session } = useSession();
const router = useRouter();
const pathname = usePathname();
const [isUnlinking, setIsUnlinking] = useState(false);
// Handle unlink click
const handleUnlinkClick = async () => {
if (isUnlinking) return;
setIsUnlinking(true);
try {
await onUnlinkClick?.();
} finally {
setIsUnlinking(false);
}
};
// Handle action click - require login if not authenticated
const handleActionClick = (e: React.MouseEvent, targetHref?: string) => {
@@ -146,7 +167,8 @@ export function ProfileCard({
Message
</button>
)}
{requestsHref ? (
{showEditButton && requestsHref ? (
// Agent's own profile - show Requests link
<Link
href={requestsHref}
onClick={(e) => handleActionClick(e, requestsHref)}
@@ -154,25 +176,63 @@ export function ProfileCard({
>
<Image
src="/assets/icons/requests-dark-icon.svg"
alt="Connect"
alt="Requests"
width={14}
height={13}
/>
{showEditButton ? 'Requests' : 'Connect'}
Requests
</Link>
) : (
<button
onClick={(e) => handleActionClick(e)}
className="flex items-center gap-2 px-4 py-1.5 bg-[#e58625] text-[#00293d] text-sm font-normal rounded-[15px] border border-[#00293d]/10 hover:bg-[#d47720] transition-colors font-serif cursor-pointer"
>
<Image
src="/assets/icons/requests-dark-icon.svg"
alt="Connect"
width={14}
height={13}
/>
{showEditButton ? 'Requests' : 'Connect'}
</button>
// Public view - show Connect/Pending/Unlink button based on connection status
connectionStatus === 'PENDING' ? (
<button
disabled
className="flex items-center gap-2 px-4 py-1.5 bg-[#fef3c7] text-[#92600f] text-sm font-normal rounded-[15px] border border-[#00293d]/10 cursor-not-allowed font-serif"
>
<Image
src="/assets/icons/requests-dark-icon.svg"
alt="Pending"
width={14}
height={13}
/>
Pending
</button>
) : connectionStatus === 'ACCEPTED' ? (
<button
onClick={handleUnlinkClick}
disabled={isUnlinking}
className="flex items-center gap-2 px-4 py-1.5 bg-red-500 text-white text-sm font-normal rounded-[15px] border border-[#00293d]/10 hover:bg-red-600 transition-colors font-serif cursor-pointer disabled:opacity-50"
>
<Image
src="/assets/icons/requests-dark-icon.svg"
alt="Unlink"
width={14}
height={13}
/>
{isUnlinking ? 'Unlinking...' : 'Unlink'}
</button>
) : (
<button
onClick={(e) => {
if (!session) {
e.preventDefault();
const returnUrl = encodeURIComponent(pathname);
router.push(`/login?callbackUrl=${returnUrl}`);
return;
}
onConnectClick?.();
}}
className="flex items-center gap-2 px-4 py-1.5 bg-[#e58625] text-[#00293d] text-sm font-normal rounded-[15px] border border-[#00293d]/10 hover:bg-[#d47720] transition-colors font-serif cursor-pointer"
>
<Image
src="/assets/icons/requests-dark-icon.svg"
alt="Connect"
width={14}
height={13}
/>
Connect
</button>
)
)}
</div>
</div>

View File

@@ -1,22 +1,32 @@
'use client';
import { useState } from 'react';
import { useSession } from 'next-auth/react';
import { useRouter, usePathname } from 'next/navigation';
type ConnectionStatus = 'PENDING' | 'ACCEPTED' | 'REJECTED' | null;
interface StatusButtonsProps {
isAvailable?: boolean;
connectionStatus?: ConnectionStatus;
onToggleAvailability?: () => void;
onConnectClick?: () => void; // Callback when Connect button is clicked (for opening modal)
onUnlinkClick?: () => void; // Callback when Unlink button is clicked
showToggle?: boolean; // If true, show as toggle (for agent's own dashboard)
}
export function StatusButtons({
isAvailable = true,
connectionStatus = null,
onToggleAvailability,
onConnectClick,
onUnlinkClick,
showToggle = false
}: StatusButtonsProps) {
const { data: session } = useSession();
const router = useRouter();
const pathname = usePathname();
const [isUnlinking, setIsUnlinking] = useState(false);
// Handle connect click - require login if not authenticated
const handleConnectClick = () => {
@@ -28,7 +38,63 @@ export function StatusButtons({
router.push(`/login?callbackUrl=${returnUrl}`);
return;
}
// TODO: Handle connect action when logged in
// Call the onConnectClick callback if provided
onConnectClick?.();
};
// Handle unlink click
const handleUnlinkClick = async () => {
if (isUnlinking) return;
setIsUnlinking(true);
try {
await onUnlinkClick?.();
} finally {
setIsUnlinking(false);
}
};
// Render the appropriate button based on connection status
const renderConnectionButton = () => {
// If PENDING - show yellow Pending button (disabled)
if (connectionStatus === 'PENDING') {
return (
<button
disabled
className="px-5 py-1.5 text-[#92600f] text-sm rounded-[15px] bg-[#fef3c7] cursor-not-allowed font-fractul"
>
Pending
</button>
);
}
// If ACCEPTED - show Unlink button
if (connectionStatus === 'ACCEPTED') {
return (
<button
onClick={handleUnlinkClick}
disabled={isUnlinking}
className="px-5 py-1.5 text-white text-sm rounded-[15px] bg-red-500 hover:bg-red-600 transition-colors font-fractul disabled:opacity-50"
>
{isUnlinking ? 'Unlinking...' : 'Unlink'}
</button>
);
}
// Default: No request or REJECTED - show Connect button
return (
<button
onClick={handleConnectClick}
disabled={!isAvailable}
className={`px-5 py-1.5 text-white text-sm rounded-[15px] transition-colors font-fractul ${
isAvailable
? 'bg-[#e58625] hover:bg-[#d47720] cursor-pointer'
: 'bg-[#00293d] cursor-not-allowed opacity-70'
}`}
>
Connect
</button>
);
};
// If showToggle is true, render both options as a toggle selector (for agent dashboard)
@@ -78,7 +144,7 @@ export function StatusButtons({
);
}
// Public view - show single status with Connect button
// Public view - show single status with Connect/Pending/Unlink button
return (
<div className="w-full max-w-[354px] lg:max-w-none">
<div className="flex items-center border border-[#00293d]/10 rounded-[15px] h-[50px] px-4">
@@ -90,17 +156,7 @@ export function StatusButtons({
{isAvailable ? 'Available.' : 'Unavailable.'}
</span>
</div>
<button
onClick={handleConnectClick}
disabled={!isAvailable}
className={`px-5 py-1.5 text-white text-sm rounded-[15px] transition-colors font-fractul ${
isAvailable
? 'bg-[#e58625] hover:bg-[#d47720] cursor-pointer'
: 'bg-[#00293d] cursor-not-allowed opacity-70'
}`}
>
Connect
</button>
{renderConnectionButton()}
</div>
</div>
);