Skip to content

Commit bf98264

Browse files
Merge pull request #1 from designbycode/feature/avatar-upload-fix
Fix: Update avatar upload component with fallback
2 parents 5a5b2b6 + 7b7424b commit bf98264

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

app/Models/User.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ class User extends Authenticatable implements HasMedia
3737
'remember_token',
3838
];
3939

40-
public function avatar(): string
40+
/**
41+
* The accessors to append to the model's array form.
42+
*
43+
* @var array
44+
*/
45+
protected $appends = ['avatar'];
46+
47+
public function getAvatarAttribute(): string
4148
{
4249
if ($this->hasMedia('avatar')) {
4350
return $this->getFirstMediaUrl('avatar', '80x80');

resources/js/components/avatar-upload.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
12
import { Button } from '@/components/ui/button';
23
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
3-
4+
import { useInitials } from '@/hooks/use-initials';
45
import { useForm } from '@inertiajs/react';
56
import { useRef, useState } from 'react';
67
import { toast } from 'sonner';
78
import { ImageCrop, ImageCropApply, ImageCropContent } from '../../../components/ui/kibo-ui/image-crop';
89

910
interface AvatarUploadProps {
1011
user: {
11-
avatar?: string | null | undefined;
12+
avatar: string;
1213
name: string;
1314
};
1415
}
@@ -30,12 +31,15 @@ export function AvatarUpload({ user }: AvatarUploadProps) {
3031
const [selectedFile, setSelectedFile] = useState<File | null>(null);
3132
const [open, setOpen] = useState(false);
3233
const fileInputRef = useRef<HTMLInputElement>(null);
34+
const getInitials = useInitials();
3335
const {
3436
setData,
3537
post,
3638
processing,
3739
delete: destroy,
38-
} = useForm<{
40+
}
41+
42+
= useForm<{
3943
avatar: File | null;
4044
}>({
4145
avatar: null,
@@ -76,7 +80,10 @@ export function AvatarUpload({ user }: AvatarUploadProps) {
7680

7781
return (
7882
<div className="flex items-center gap-4">
79-
<img src={user.avatar || ''} alt={user.name} className="h-16 w-16 rounded-full object-cover" />
83+
<Avatar className="h-16 w-16">
84+
<AvatarImage src={user.avatar} />
85+
<AvatarFallback>{getInitials(user.name)}</AvatarFallback>
86+
</Avatar>
8087
<div className="grid gap-2">
8188
<div className="flex gap-2">
8289
<Dialog open={open} onOpenChange={setOpen}>

0 commit comments

Comments
 (0)