import { useEffect, useState } from 'react'; import { httpDelete, httpGet, httpPost } from '../lib/http'; import { pageProgressMessage } from '../stores/page'; import { isLoggedIn } from '../lib/jwt'; import { showLoginPopup } from '../lib/popup'; import { getUrlParams } from '../lib/browser'; import { CheckIcon } from './ReactIcons/CheckIcon'; import { DeleteUserIcon } from './ReactIcons/DeleteUserIcon'; import { useToast } from '../hooks/use-toast'; import { useAuth } from '../hooks/use-auth'; import { AddedUserIcon } from './ReactIcons/AddedUserIcon'; import { StopIcon } from './ReactIcons/StopIcon'; import { ErrorIcon } from './ReactIcons/ErrorIcon'; export type FriendshipStatus = | 'none' | 'sent' | 'received' | 'accepted' | 'rejected' | 'got_rejected'; type UserResponse = { id: string; links: Record; avatar: string; name: string; status: FriendshipStatus; }; export function Befriend() { const { u: inviteId } = getUrlParams(); const toast = useToast(); const currentUser = useAuth(); const [isConfirming, setIsConfirming] = useState(false); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(''); const [user, setUser] = useState(); const isAuthenticated = isLoggedIn(); async function loadUser(userId: string) { const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-friend/${userId}` ); if (error || !response) { setError(error?.message || 'Something went wrong'); return; } if (response.status === 'accepted') { window.location.href = '/account/friends?c=fa'; return; } setUser(response); } useEffect(() => { if (inviteId) { loadUser(inviteId).finally(() => { pageProgressMessage.set(''); setIsLoading(false); }); } else { setIsLoading(false); setError('Missing invite ID in URL'); pageProgressMessage.set(''); } }, [inviteId]); async function addFriend(userId: string, successMessage: string) { pageProgressMessage.set('Please wait...'); setError(''); const { response, error } = await httpPost( `${import.meta.env.PUBLIC_API_URL}/v1-add-friend/${userId}`, {} ); if (error || !response) { setError(error?.message || 'Something went wrong'); return; } if (response.status === 'accepted') { window.location.href = '/account/friends?c=fa'; return; } setUser(response); } async function deleteFriend(userId: string, successMessage: string) { pageProgressMessage.set('Please wait...'); setError(''); const { response, error } = await httpDelete( `${import.meta.env.PUBLIC_API_URL}/v1-delete-friend/${userId}`, {} ); if (error || !response) { setError(error?.message || 'Something went wrong'); return; } setUser(response); toast.success(successMessage); } if (isLoading) { return null; } if (!user) { return (

Error

{error || 'There was a problem, please try again.'}

); } const userAvatar = user.avatar ? `${import.meta.env.PUBLIC_AVATAR_BASE_URL}/${user.avatar}` : '/images/default-avatar.png'; const isMe = currentUser?.id === user.id; return (
{'join

{user.name}

After you add {user.name} as a friend, you will be able to view each other's skills and progress.

{user.status === 'none' && ( )} {user.status === 'sent' && ( <> Request Sent {!isConfirming && ( )} {isConfirming && ( Are you sure?{' '} {' '} )} )} {user.status === 'accepted' && ( <> You are friends {!isConfirming && ( )} {isConfirming && ( Are you sure?{' '} {' '} )} )} {user.status === 'rejected' && ( <> Request Rejected Changed your mind?{' '} )} {user.status === 'got_rejected' && ( <> Request Rejected )} {user.status === 'received' && ( <> {!isConfirming && ( )} {isConfirming && ( Are you sure?{' '} {' '} )} )}
{error && (

{error}

)}
); }