|
| 1 | +import { useOrganizationStateStore } from "@/lib/providers/organization-state-store-provider"; |
| 2 | +import { useAuthStore } from "@/lib/providers/auth-store-provider"; |
| 3 | +import { useUserMutation } from "@/lib/api/organizations/users"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { Trash2 } from "lucide-react"; |
| 6 | +import { CoachingRelationshipWithUserNames } from "@/types/coaching_relationship_with_user_names"; |
| 7 | +import { OrganizationStateStore } from "@/lib/stores/organization-state-store"; |
| 8 | +import { AuthStore } from "@/lib/stores/auth-store"; |
| 9 | +import { Id } from "@/types/general"; |
| 10 | + |
1 | 11 | interface MemberCardProps { |
2 | 12 | firstName: string; |
3 | 13 | lastName: string; |
4 | | - email?: string; |
| 14 | + email: string; |
| 15 | + userId: Id; |
| 16 | + userRelationships: CoachingRelationshipWithUserNames[]; |
| 17 | + onRefresh: () => void; |
5 | 18 | } |
6 | 19 |
|
7 | | -export function MemberCard({ firstName, lastName, email }: MemberCardProps) { |
| 20 | +export function MemberCard({ |
| 21 | + firstName, |
| 22 | + lastName, |
| 23 | + email, |
| 24 | + userId, |
| 25 | + userRelationships, |
| 26 | + onRefresh, |
| 27 | +}: MemberCardProps) { |
| 28 | + const currentOrganizationId = useOrganizationStateStore( |
| 29 | + (state: OrganizationStateStore) => state.currentOrganizationId |
| 30 | + ); |
| 31 | + const { userSession } = useAuthStore((state: AuthStore) => state); |
| 32 | + const { deleteNested: deleteUser } = useUserMutation(currentOrganizationId); |
| 33 | + |
| 34 | + // Check if current user is a coach in any of this user's relationships |
| 35 | + // and make sure we can't delete ourselves |
| 36 | + const canDeleteUser = userRelationships.some( |
| 37 | + (rel) => rel.coach_id === userSession.id && userId !== userSession.id |
| 38 | + ); |
| 39 | + |
| 40 | + const handleDelete = async () => { |
| 41 | + if (!confirm("Are you sure you want to delete this member?")) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + await deleteUser(currentOrganizationId, userId); |
| 47 | + onRefresh(); |
| 48 | + } catch (error) { |
| 49 | + console.error("Error deleting user:", error); |
| 50 | + // TODO: Show an error toast here once we start using toasts for showing operation results. |
| 51 | + } |
| 52 | + }; |
| 53 | + |
8 | 54 | return ( |
9 | | - <div className="flex items-center p-4 hover:bg-accent/50 transition-colors cursor-pointer"> |
| 55 | + <div className="flex items-center p-4 hover:bg-accent/50 transition-colors"> |
10 | 56 | <div className="flex-1"> |
11 | 57 | <h3 className="font-medium"> |
12 | 58 | {firstName} {lastName} |
13 | 59 | </h3> |
14 | 60 | {email && <p className="text-sm text-muted-foreground">{email}</p>} |
15 | 61 | </div> |
| 62 | + {canDeleteUser && ( |
| 63 | + <Button |
| 64 | + variant="ghost" |
| 65 | + size="icon" |
| 66 | + onClick={handleDelete} |
| 67 | + className="text-destructive hover:text-destructive" |
| 68 | + > |
| 69 | + <Trash2 className="h-4 w-4" /> |
| 70 | + </Button> |
| 71 | + )} |
16 | 72 | </div> |
17 | 73 | ); |
18 | 74 | } |
0 commit comments