Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ class User extends Authenticatable implements HasMedia
'remember_token',
];

public function avatar(): string
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['avatar'];

public function getAvatarAttribute(): string
{
if ($this->hasMedia('avatar')) {
return $this->getFirstMediaUrl('avatar', '80x80');
Expand Down
15 changes: 11 additions & 4 deletions resources/js/components/avatar-upload.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Button } from '@/components/ui/button';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';

import { useInitials } from '@/hooks/use-initials';
import { useForm } from '@inertiajs/react';
import { useRef, useState } from 'react';
import { toast } from 'sonner';
import { ImageCrop, ImageCropApply, ImageCropContent } from '../../../components/ui/kibo-ui/image-crop';

interface AvatarUploadProps {
user: {
avatar?: string | null | undefined;
avatar: string;
name: string;
};
}
Expand All @@ -30,12 +31,15 @@ export function AvatarUpload({ user }: AvatarUploadProps) {
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const [open, setOpen] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const getInitials = useInitials();
const {
setData,
post,
processing,
delete: destroy,
} = useForm<{
}

= useForm<{
avatar: File | null;
}>({
avatar: null,
Expand Down Expand Up @@ -76,7 +80,10 @@ export function AvatarUpload({ user }: AvatarUploadProps) {

return (
<div className="flex items-center gap-4">
<img src={user.avatar || ''} alt={user.name} className="h-16 w-16 rounded-full object-cover" />
<Avatar className="h-16 w-16">
<AvatarImage src={user.avatar} />
<AvatarFallback>{getInitials(user.name)}</AvatarFallback>
</Avatar>
<div className="grid gap-2">
<div className="flex gap-2">
<Dialog open={open} onOpenChange={setOpen}>
Expand Down
Loading