All files / app/(dashboard)/profile page.tsx

97.24% Statements 247/254
42.85% Branches 18/42
100% Functions 3/3
97.24% Lines 247/254

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 3141x                                 31x 31x 31x 31x 31x 31x   31x 31x 31x 31x 31x 31x 31x 31x   31x 21x 21x 21x 21x 21x 21x   21x 2x 2x 2x   18x 18x 18x 18x 18x   21x 2x 2x   16x 16x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 2x 2x 2x 2x 2x 2x 21x 20x 20x 21x   21x 31x   31x 2x   2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 1x 1x   1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x   31x 9x 9x 9x 9x   9x   22x 22x 22x 22x 22x 22x   22x   22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x     31x   31x 31x 31x 31x 31x 31x 31x 31x 31x 31x   31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x     31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x     31x 31x   31x 31x 31x 31x 31x 31x 31x 31x 31x 31x     31x 31x 31x 31x     31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x     31x   31x 31x 31x 31x 31x 31x 31x 31x 31x     31x   31x 31x 31x 31x 31x 31x 31x 31x 31x     31x   31x 31x 31x 31x 31x 31x 31x 31x 31x     31x 31x 31x 31x     31x 31x 31x 2x 2x   2x   20x 20x   20x   31x 31x 31x 31x   31x  
'use client';
 
import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { Loader2, Save } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { createClient } from '@/lib/supabase/client';
import { updateProfileSchema, type UpdateProfileInput } from '@/lib/validations/profile';
import { useToast } from '@/hooks/use-toast';
import type { Profile } from '@/types/database';
 
export default function ProfilePage() {
  const [isLoading, setIsLoading] = useState(true);
  const [isSaving, setIsSaving] = useState(false);
  const [profile, setProfile] = useState<Profile | null>(null);
  const router = useRouter();
  const { toast } = useToast();
 
  const {
    register,
    handleSubmit,
    reset,
    formState: { errors, isDirty },
  } = useForm<UpdateProfileInput>({
    resolver: zodResolver(updateProfileSchema),
  });
 
  useEffect(() => {
    const fetchProfile = async () => {
      try {
        const supabase = createClient();
        const {
          data: { user },
        } = await supabase.auth.getUser();
 
        if (!user) {
          router.push('/login');
          return;
        }
 
        const { data: profileData, error } = await supabase
          .from('profiles')
          .select('*')
          .eq('user_id', user.id)
          .single();
 
        if (error) {
          throw error;
        }
 
        setProfile(profileData);
        reset({
          display_name: profileData.display_name || '',
          bio: profileData.bio || '',
          role: profileData.role || '',
          whatsapp: profileData.whatsapp || '',
          email_public: profileData.email_public || '',
          linkedin_url: profileData.linkedin_url || '',
          website_url: profileData.website_url || '',
          twitter_url: profileData.twitter_url || '',
          github_url: profileData.github_url || '',
        });
      } catch (error) {
        console.error('Error fetching profile:', error);
        toast({
          variant: 'destructive',
          title: 'Error',
          description: 'Failed to load profile data.',
        });
      } finally {
        setIsLoading(false);
      }
    };
 
    fetchProfile();
  }, [reset, router, toast]);
 
  const onSubmit = async (data: UpdateProfileInput) => {
    if (!profile) return;
 
    setIsSaving(true);
    try {
      const supabase = createClient();
 
      const { error } = await supabase
        .from('profiles')
        .update({
          display_name: data.display_name || null,
          bio: data.bio || null,
          role: data.role || null,
          whatsapp: data.whatsapp || null,
          email_public: data.email_public || null,
          linkedin_url: data.linkedin_url || null,
          website_url: data.website_url || null,
          twitter_url: data.twitter_url || null,
          github_url: data.github_url || null,
          updated_at: new Date().toISOString(),
        })
        .eq('id', profile.id);
 
      if (error) {
        throw error;
      }
 
      toast({
        title: 'Profile Updated',
        description: 'Your profile has been saved successfully.',
      });
 
      // Reset form state to mark it as not dirty
      reset(data);
    } catch (error) {
      console.error('Error updating profile:', error);
      toast({
        variant: 'destructive',
        title: 'Error',
        description: 'Failed to update profile. Please try again.',
      });
    } finally {
      setIsSaving(false);
    }
  };
 
  if (isLoading) {
    return (
      <div className="flex min-h-[400px] items-center justify-center">
        <Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
      </div>
    );
  }
 
  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-3xl font-bold">Edit Profile</h1>
        <p className="text-muted-foreground">Update your profile information visible to visitors</p>
      </div>
 
      <form onSubmit={handleSubmit(onSubmit)} className="space-y-6">
        {/* Basic Information */}
        <Card>
          <CardHeader>
            <CardTitle>Basic Information</CardTitle>
            <CardDescription>Your public profile details</CardDescription>
          </CardHeader>
          <CardContent className="space-y-4">
            <div className="space-y-2">
              <Label htmlFor="display_name">Display Name</Label>
              <Input
                id="display_name"
                {...register('display_name')}
                placeholder="John Doe"
                disabled={isSaving}
              />
              {errors.display_name && (
                <p className="text-sm text-destructive">{errors.display_name.message}</p>
              )}
            </div>
 
            <div className="space-y-2">
              <Label htmlFor="role">Role / Title</Label>
              <Input
                id="role"
                {...register('role')}
                placeholder="Full-Stack Developer"
                disabled={isSaving}
              />
              {errors.role && <p className="text-sm text-destructive">{errors.role.message}</p>}
            </div>
 
            <div className="space-y-2">
              <Label htmlFor="bio">Bio</Label>
              <Textarea
                id="bio"
                {...register('bio')}
                placeholder="Tell visitors about yourself, your experience, and what makes you unique..."
                rows={4}
                disabled={isSaving}
              />
              {errors.bio && <p className="text-sm text-destructive">{errors.bio.message}</p>}
              <p className="text-xs text-muted-foreground">Maximum 500 characters</p>
            </div>
          </CardContent>
        </Card>
 
        {/* Contact Information */}
        <Card>
          <CardHeader>
            <CardTitle>Contact Information</CardTitle>
            <CardDescription>How clients can reach you</CardDescription>
          </CardHeader>
          <CardContent className="space-y-4">
            <div className="grid gap-4 sm:grid-cols-2">
              <div className="space-y-2">
                <Label htmlFor="whatsapp">WhatsApp Number</Label>
                <Input
                  id="whatsapp"
                  {...register('whatsapp')}
                  placeholder="+919876543210"
                  disabled={isSaving}
                />
                {errors.whatsapp && (
                  <p className="text-sm text-destructive">{errors.whatsapp.message}</p>
                )}
                <p className="text-xs text-muted-foreground">Include country code</p>
              </div>
 
              <div className="space-y-2">
                <Label htmlFor="email_public">Public Email</Label>
                <Input
                  id="email_public"
                  type="email"
                  {...register('email_public')}
                  placeholder="hello@example.com"
                  disabled={isSaving}
                />
                {errors.email_public && (
                  <p className="text-sm text-destructive">{errors.email_public.message}</p>
                )}
              </div>
            </div>
          </CardContent>
        </Card>
 
        {/* Social Links */}
        <Card>
          <CardHeader>
            <CardTitle>Social Links</CardTitle>
            <CardDescription>Connect your social profiles</CardDescription>
          </CardHeader>
          <CardContent className="space-y-4">
            <div className="grid gap-4 sm:grid-cols-2">
              <div className="space-y-2">
                <Label htmlFor="linkedin_url">LinkedIn</Label>
                <Input
                  id="linkedin_url"
                  {...register('linkedin_url')}
                  placeholder="https://linkedin.com/in/username"
                  disabled={isSaving}
                />
                {errors.linkedin_url && (
                  <p className="text-sm text-destructive">{errors.linkedin_url.message}</p>
                )}
              </div>
 
              <div className="space-y-2">
                <Label htmlFor="website_url">Website</Label>
                <Input
                  id="website_url"
                  {...register('website_url')}
                  placeholder="https://yourwebsite.com"
                  disabled={isSaving}
                />
                {errors.website_url && (
                  <p className="text-sm text-destructive">{errors.website_url.message}</p>
                )}
              </div>
 
              <div className="space-y-2">
                <Label htmlFor="twitter_url">Twitter / X</Label>
                <Input
                  id="twitter_url"
                  {...register('twitter_url')}
                  placeholder="https://twitter.com/username"
                  disabled={isSaving}
                />
                {errors.twitter_url && (
                  <p className="text-sm text-destructive">{errors.twitter_url.message}</p>
                )}
              </div>
 
              <div className="space-y-2">
                <Label htmlFor="github_url">GitHub</Label>
                <Input
                  id="github_url"
                  {...register('github_url')}
                  placeholder="https://github.com/username"
                  disabled={isSaving}
                />
                {errors.github_url && (
                  <p className="text-sm text-destructive">{errors.github_url.message}</p>
                )}
              </div>
            </div>
          </CardContent>
        </Card>
 
        {/* Save Button */}
        <div className="flex justify-end">
          <Button type="submit" disabled={isSaving || !isDirty}>
            {isSaving ? (
              <>
                <Loader2 className="mr-2 h-4 w-4 animate-spin" />
                Saving...
              </>
            ) : (
              <>
                <Save className="mr-2 h-4 w-4" />
                Save Changes
              </>
            )}
          </Button>
        </div>
      </form>
    </div>
  );
}