Skip to content

Commit 9815105

Browse files
committed
delete user
1 parent 9d3c2ca commit 9815105

File tree

13 files changed

+216
-5
lines changed

13 files changed

+216
-5
lines changed
Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,73 @@
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+
110
interface MemberCardProps {
211
firstName: string;
312
lastName: string;
413
email?: string;
14+
userId: string;
15+
userRelationships: CoachingRelationshipWithUserNames[];
16+
onRefresh: () => void;
517
}
618

7-
export function MemberCard({ firstName, lastName, email }: MemberCardProps) {
19+
export function MemberCard({
20+
firstName,
21+
lastName,
22+
email,
23+
userId,
24+
userRelationships,
25+
onRefresh,
26+
}: MemberCardProps) {
27+
const currentOrganizationId = useOrganizationStateStore(
28+
(state: OrganizationStateStore) => state.currentOrganizationId
29+
);
30+
const { userSession } = useAuthStore((state: AuthStore) => state);
31+
const { deleteNested: deleteUser } = useUserMutation(currentOrganizationId);
32+
33+
// Check if current user is a coach in any of this user's relationships
34+
// and make sure we can't delete ourselves
35+
const canDeleteUser = userRelationships.some(
36+
(rel) => rel.coach_id === userSession.id && userId !== userSession.id
37+
);
38+
39+
const handleDelete = async () => {
40+
if (!confirm("Are you sure you want to delete this member?")) {
41+
return;
42+
}
43+
44+
try {
45+
await deleteUser(currentOrganizationId, userId);
46+
onRefresh(); // Call refresh after successful deletion
47+
} catch (error) {
48+
console.error("Error deleting user:", error);
49+
// You might want to show an error toast here
50+
}
51+
};
52+
853
return (
9-
<div className="flex items-center p-4 hover:bg-accent/50 transition-colors cursor-pointer">
54+
<div className="flex items-center p-4 hover:bg-accent/50 transition-colors">
1055
<div className="flex-1">
1156
<h3 className="font-medium">
1257
{firstName} {lastName}
1358
</h3>
1459
{email && <p className="text-sm text-muted-foreground">{email}</p>}
1560
</div>
61+
{canDeleteUser && (
62+
<Button
63+
variant="ghost"
64+
size="icon"
65+
onClick={handleDelete}
66+
className="text-destructive hover:text-destructive"
67+
>
68+
<Trash2 className="h-4 w-4" />
69+
</Button>
70+
)}
1671
</div>
1772
);
1873
}

src/components/ui/members/member-container.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ export function MemberContainer({
6363
/>
6464
)}
6565
</div>
66-
<MemberList users={associatedUsers} />
66+
<MemberList
67+
users={associatedUsers}
68+
relationships={userRelationships}
69+
onRefresh={onRefresh}
70+
/>
6771
</div>
6872
);
6973
}

src/components/ui/members/member-list.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import { Card, CardContent } from "@/components/ui/card";
22
import { User } from "@/types/user";
33
import { MemberCard } from "./member-card";
4+
import { CoachingRelationshipWithUserNames } from "@/types/coaching_relationship_with_user_names";
45

56
interface MemberListProps {
67
users: User[];
8+
relationships: CoachingRelationshipWithUserNames[];
9+
onRefresh: () => void;
710
}
811

9-
export function MemberList({ users }: MemberListProps) {
12+
export function MemberList({
13+
users,
14+
relationships,
15+
onRefresh,
16+
}: MemberListProps) {
17+
// Create a mapping of user IDs to their associated relationships
18+
const userRelationshipsMap = users.reduce((acc, user) => {
19+
acc[user.id] = relationships.filter(
20+
(rel) => rel.coach_id === user.id || rel.coachee_id === user.id
21+
);
22+
return acc;
23+
}, {} as Record<string, CoachingRelationshipWithUserNames[]>);
24+
1025
return (
1126
<Card className="w-full">
1227
<CardContent className="p-6">
@@ -17,6 +32,9 @@ export function MemberList({ users }: MemberListProps) {
1732
firstName={user.first_name}
1833
lastName={user.last_name}
1934
email={user.email}
35+
userId={user.id}
36+
userRelationships={userRelationshipsMap[user.id]}
37+
onRefresh={onRefresh}
2038
/>
2139
))}
2240
</div>

src/lib/api/actions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ export const ActionApi = {
6565
*/
6666
delete: async (id: Id): Promise<Action> =>
6767
EntityApi.deleteFn<null, Action>(`${ACTIONS_BASEURL}/${id}`),
68+
69+
/**
70+
* Deletes an action nested under another entity (foreign key relationship).
71+
*
72+
* @param entityId The ID of the entity under which to delete the action
73+
* @param actionId The ID of the action to delete
74+
* @returns Promise resolving to the deleted Action object
75+
*/
76+
deleteNested: async (entityId: Id, actionId: Id): Promise<Action> => {
77+
throw new Error("Delete nested operation not implemented");
78+
},
6879
};
6980

7081
/**
@@ -174,5 +185,6 @@ export const useActionMutation = () => {
174185
createNested: ActionApi.createNested,
175186
update: ActionApi.update,
176187
delete: ActionApi.delete,
188+
deleteNested: ActionApi.deleteNested,
177189
});
178190
};

src/lib/api/agreements.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ export const AgreementApi = {
6969
*/
7070
delete: async (id: Id): Promise<Agreement> =>
7171
EntityApi.deleteFn<null, Agreement>(`${AGREEMENTS_BASEURL}/${id}`),
72+
73+
/**
74+
* Deletes an agreement nested under another entity (foreign key relationship).
75+
*
76+
* @param entityId The ID of the entity under which to delete the agreement
77+
* @param agreementId The ID of the agreement to delete
78+
* @returns Promise resolving to the deleted Agreement object
79+
*/
80+
deleteNested: async (entityId: Id, agreementId: Id): Promise<Agreement> => {
81+
throw new Error("Delete nested operation not implemented");
82+
},
7283
};
7384

7485
/**
@@ -175,5 +186,6 @@ export const useAgreementMutation = () => {
175186
createNested: AgreementApi.createNested,
176187
update: AgreementApi.update,
177188
delete: AgreementApi.delete,
189+
deleteNested: AgreementApi.deleteNested,
178190
});
179191
};

src/lib/api/coaching-relationships.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ export const CoachingRelationshipApi = {
9191
delete: async (_id: Id) => {
9292
throw new Error("Delete operation not implemented");
9393
},
94+
95+
/**
96+
* Deletes a coaching relationship nested under another entity (foreign key relationship).
97+
*
98+
* @param entityId The ID of the entity under which to delete the coaching relationship
99+
* @param relationshipId The ID of the coaching relationship to delete
100+
* @returns Promise resolving to the deleted CoachingRelationshipWithUserNames object
101+
*/
102+
deleteNested: async (
103+
organizationId: Id,
104+
relationshipId: Id
105+
): Promise<CoachingRelationshipWithUserNames> => {
106+
throw new Error("Delete nested operation not implemented");
107+
},
94108
};
95109

96110
/**
@@ -188,6 +202,7 @@ export const useCoachingRelationshipMutation = (organizationId: Id) => {
188202
createNested: CoachingRelationshipApi.createNested,
189203
update: CoachingRelationshipApi.update,
190204
delete: CoachingRelationshipApi.delete,
205+
deleteNested: CoachingRelationshipApi.deleteNested,
191206
}
192207
);
193208
};

src/lib/api/coaching-sessions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ export const CoachingSessionApi = {
9595
EntityApi.deleteFn<null, CoachingSession>(
9696
`${COACHING_SESSIONS_BASEURL}/${id}`
9797
),
98+
99+
/**
100+
* Deletes a coaching session nested under another entity (foreign key relationship).
101+
*
102+
* @param entityId The ID of the entity under which to delete the coaching session
103+
* @param coachingSessionId The ID of the coaching session to delete
104+
* @returns Promise resolving to the deleted CoachingSession object
105+
*/
106+
deleteNested: async (
107+
entityId: Id,
108+
coachingSessionId: Id
109+
): Promise<CoachingSession> => {
110+
throw new Error("Delete nested operation not implemented");
111+
},
98112
};
99113

100114
/**
@@ -195,6 +209,7 @@ export const useCoachingSessionMutation = () => {
195209
createNested: CoachingSessionApi.createNested,
196210
update: CoachingSessionApi.update,
197211
delete: CoachingSessionApi.delete,
212+
deleteNested: CoachingSessionApi.deleteNested,
198213
}
199214
);
200215
};

src/lib/api/entity-api.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export namespace EntityApi {
193193
url: string,
194194
config?: any
195195
): Promise<R> => {
196+
console.log("deleteFn", url, config);
196197
return mutationFn<T, R>("delete", url, config);
197198
};
198199

@@ -374,6 +375,7 @@ export namespace EntityApi {
374375
createNested: (id: Id, entity: T) => Promise<U>;
375376
update: (id: Id, entity: T) => Promise<U>;
376377
delete: (id: Id) => Promise<U>;
378+
deleteNested: (entityId: Id, nestedEntityId: Id) => Promise<U>;
377379
}
378380
) => {
379381
const [isLoading, setIsLoading] = useState(false);
@@ -444,6 +446,17 @@ export namespace EntityApi {
444446
* @returns Promise resolving to the deleted entity
445447
*/
446448
delete: (id: Id) => executeWithState(() => api.delete(id)),
449+
450+
/**
451+
* Deletes an entity nested under another entity (foreign key relationship).
452+
*
453+
* @param entityId The entity's id under which to delete entity
454+
* @param nestedEntityId The nested entity's id to delete
455+
* @returns Promise resolving to the deleted entity
456+
*/
457+
deleteNested: (entityId: Id, nestedEntityId: Id) =>
458+
executeWithState(() => api.deleteNested(entityId, nestedEntityId)),
459+
447460
/** Indicates if any operation is currently in progress */
448461
isLoading,
449462
/** Contains the error if the last operation failed, null otherwise */

src/lib/api/organizations.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ export const OrganizationApi = {
7171
*/
7272
delete: async (id: Id): Promise<Organization> =>
7373
EntityApi.deleteFn<null, Organization>(`${ORGANIZATIONS_BASEURL}/${id}`),
74+
75+
/**
76+
* Deletes an organization nested under another entity (foreign key relationship).
77+
*
78+
* @param entityId The ID of the entity under which to delete the organization
79+
* @param organizationId The ID of the organization to delete
80+
* @returns Promise resolving to the deleted Organization object
81+
*/
82+
deleteNested: async (
83+
entityId: Id,
84+
organizationId: Id
85+
): Promise<Organization> => {
86+
throw new Error("Delete nested operation not implemented");
87+
},
7488
};
7589

7690
/**
@@ -155,6 +169,7 @@ export const useOrganizationMutation = () => {
155169
createNested: OrganizationApi.createNested,
156170
update: OrganizationApi.update,
157171
delete: OrganizationApi.delete,
172+
deleteNested: OrganizationApi.deleteNested,
158173
}
159174
);
160175
};

src/lib/api/organizations/users.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ export const UserApi = {
5959
delete: async (id: Id): Promise<User> => {
6060
throw new Error("Delete operation not implemented");
6161
},
62+
63+
/**
64+
* Deletes a user nested in an organization.
65+
*/
66+
deleteNested: async (organizationId: Id, userId: Id): Promise<User> => {
67+
return EntityApi.deleteFn<null, User>(
68+
`${ORGANIZATIONS_USERS_BASEURL(organizationId)}/${userId}`
69+
);
70+
},
6271
};
6372

6473
/**
@@ -92,6 +101,7 @@ export const useUserMutation = (organizationId: Id) => {
92101
createNested: UserApi.createNested,
93102
update: UserApi.update,
94103
delete: UserApi.delete,
104+
deleteNested: UserApi.deleteNested,
95105
}
96106
);
97107
};

0 commit comments

Comments
 (0)