-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManageInstitutionSection.tsx
More file actions
291 lines (281 loc) · 11.5 KB
/
ManageInstitutionSection.tsx
File metadata and controls
291 lines (281 loc) · 11.5 KB
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
import { useState } from 'react';
import { Card, CardContent, CardHeader, CardTitle } from '../../ui/Card/Card';
import Button from '../../ui/Button/Button';
import {
Users,
UserMinus,
AlertCircle,
Crown,
Inbox,
Check,
X,
} from 'lucide-react';
import { Avatar, AvatarImage, AvatarFallback } from '../../ui/Avatar/Avatar';
import type { Institution } from '../../../types/entities';
import { useRemoveProfessor } from '../../../hooks/useInstitutionMutations';
import {
useGetPendingRequests,
useProcessJoinRequest,
} from '../../../hooks/useJoinRequests';
import { Professor } from '../../../models/Professor';
interface ManageInstitutionSectionProps {
institution: Institution;
}
export default function ManageInstitutionSection({
institution,
}: ManageInstitutionSectionProps) {
const { mutate: removeProfessor, isPending: isRemoving } =
useRemoveProfessor();
const { data: pendingRequests = [], isLoading: isLoadingRequests } =
useGetPendingRequests(institution.institutionId || institution.id || '');
const { mutate: processRequest, isPending: isProcessingRequest } =
useProcessJoinRequest();
const [processingId, setProcessingId] = useState<string | null>(null);
const handleProcessRequest = (
requestId: string,
action: 'accept' | 'reject'
) => {
setProcessingId(requestId);
processRequest(
{ requestId, action },
{
onSuccess: () => {
// Silencioso en caso de éxito
},
onError: (error) => {
alert(`Error al procesar la solicitud: ${error.message}`);
},
onSettled: () => {
setProcessingId(null);
},
}
);
};
const handleRemoveProfessor = (
professorId: string,
professorName: string
) => {
if (
confirm(
`¿Estás seguro de que quieres remover a ${professorName} de la institución?`
)
) {
removeProfessor(
{ institutionId: institution.institutionId || institution.id || '', professorId },
{
onSuccess: () => {
// Silencioso en caso de éxito
},
onError: (error) => {
alert(`Error al remover profesor: ${error.message}`);
},
}
);
}
};
const allProfessors = institution.professors || [];
const otherProfessors = allProfessors.filter(
(prof) => prof && prof.id !== institution.manager?.id
);
return (
<Card className="border-t-4 border-purple-400">
<CardHeader>
<CardTitle className="flex items-center gap-2 text-slate-800">
<Users className="w-5 h-5 text-purple-600" />
Panel de Gestión
</CardTitle>
</CardHeader>
<CardContent className="space-y-8">
<div className="bg-purple-50 border border-purple-200 rounded-lg p-4">
<div className="flex gap-3">
<AlertCircle className="w-5 h-5 text-purple-600 flex-shrink-0 mt-0.5" />
<div className="text-sm text-purple-800">
<p className="font-medium mb-1">Como encargado puedes:</p>
<ul className="list-disc list-inside space-y-1 text-purple-700">
<li>Gestionar las solicitudes para unirse a tu institución.</li>
<li>Remover miembros existentes (excepto a ti mismo).</li>
</ul>
</div>
</div>
</div>
<div className="text-center p-4 bg-green-50 rounded-lg border border-green-100 max-w-xs mx-auto">
<p className="text-3xl font-bold text-green-600">
{allProfessors.length}
</p>
<p className="text-sm text-slate-600 mt-1">
Miembros en la Institución
</p>
</div>
<div className="space-y-3 pt-6 border-t">
<div className="flex items-center justify-between">
<h4 className="font-semibold text-slate-800 flex items-center gap-2">
<Inbox className="w-5 h-5 text-slate-500" />
Solicitudes Pendientes
</h4>
<span className="text-sm font-medium bg-blue-100 text-blue-700 rounded-full px-2 py-0.5">
{pendingRequests.length}
</span>
</div>
<div className="space-y-2 max-h-60 overflow-y-auto pr-2">
{isLoadingRequests ? (
<p className="text-sm text-slate-500 text-center py-4">
Cargando solicitudes...
</p>
) : pendingRequests.length === 0 ? (
<div className="text-center py-8 border-2 border-dashed rounded-lg">
<Inbox className="w-12 h-12 text-slate-300 mx-auto mb-3" />
<p className="text-slate-500 text-sm">
No hay solicitudes pendientes.
</p>
</div>
) : (
pendingRequests.map((req) => {
const profInstance = new Professor(req.professor);
return (
<div key={req.id} className="p-3 border rounded-lg bg-slate-50">
<div className="flex items-center justify-between gap-3">
<div className="flex items-center gap-3 flex-1 min-w-0">
<Avatar className="h-10 w-10 flex-shrink-0">
<AvatarImage
src={profInstance.profilePicture}
alt={profInstance.fullName}
/>
<AvatarFallback className="bg-slate-400 text-white font-semibold">
{profInstance.initials}
</AvatarFallback>
</Avatar>
<div className="flex-1 min-w-0">
<p className="font-medium text-slate-800 truncate">
{profInstance.fullName}
</p>
<p className="text-sm text-slate-600 truncate">
{profInstance.email ||
'Email no disponible'}
</p>
</div>
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<Button
size="sm"
variant="outline"
className="border-red-200 text-red-600 hover:bg-red-50"
onClick={() => handleProcessRequest(req.id, 'reject')}
isLoading={
isProcessingRequest && processingId === req.id
}
disabled={isProcessingRequest}
>
<X className="w-4 h-4" />
</Button>
<Button
size="sm"
className="bg-green-500 hover:bg-green-600"
onClick={() => handleProcessRequest(req.id, 'accept')}
isLoading={
isProcessingRequest && processingId === req.id
}
disabled={isProcessingRequest}
>
<Check className="w-4 h-4" />
</Button>
</div>
</div>
</div>
)}
))}
</div>
</div>
<div className="space-y-3 pt-6 border-t">
<h4 className="font-semibold text-slate-800">
Miembros de la Institución
</h4>
<div className="space-y-2 max-h-80 overflow-y-auto pr-2">
{institution.manager?.user && (
<div className="border-2 border-purple-200 rounded-lg p-3 bg-purple-50/50">
<div className="flex items-center gap-3">
<Avatar className="h-10 w-10 flex-shrink-0 hidden sm:flex">
<AvatarImage
src={institution.manager.user.profile_picture}
alt={institution.manager.user.name}
/>
<AvatarFallback className="bg-gradient-to-br from-purple-500 to-blue-500 text-white font-semibold">
{institution.manager.user.name?.charAt(0)}
{institution.manager.user.surname?.charAt(0)}
</AvatarFallback>
</Avatar>
<div className="min-w-0 flex-1">
<p className="font-semibold text-slate-800 flex items-center gap-2 flex-wrap">
{institution.manager.user.name}{' '}
{institution.manager.user.surname}
<span className="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-purple-100 text-purple-800">
<Crown className="w-3 h-3 mr-1" /> Encargado
</span>
</p>
<p className="text-sm text-slate-600 truncate">
{institution.manager.user.mail}
</p>
</div>
</div>
</div>
)}
{otherProfessors.length > 0 ? (
otherProfessors.map((professor) => {
const otherProfessorsInstance = new Professor(professor);
return (
<div
key={professor.id}
className="border rounded-lg p-3 hover:bg-slate-50"
>
<div className="flex items-center justify-between">
<div className="flex items-center gap-3 flex-1 min-w-0">
<Avatar className="h-10 w-10 flex-shrink-0 hidden sm:flex">
<AvatarImage
src={otherProfessorsInstance.profilePicture}
alt={otherProfessorsInstance.fullName}
/>
<AvatarFallback className="bg-gradient-to-br from-blue-400 to-green-400 text-white font-semibold">
{otherProfessorsInstance.initials}
</AvatarFallback>
</Avatar>
<div className="flex-1 min-w-0">
<p className="font-medium text-slate-800 truncate">
{otherProfessorsInstance.fullName}
</p>
<p className="text-sm text-slate-600 truncate">
{otherProfessorsInstance.email ||
'Email no disponible'}
</p>
</div>
</div>
<Button
type="button"
variant="ghost"
size="sm"
className="text-red-600 hover:bg-red-50 flex-shrink-0"
onClick={() =>
handleRemoveProfessor(
professor.id,
otherProfessorsInstance.fullName
)
}
disabled={isRemoving}
>
<UserMinus className="w-4 h-4" />
</Button>
</div>
</div>
)}
)) : (
<div className="text-center py-8 border-2 border-dashed rounded-lg">
<Users className="w-12 h-12 text-slate-300 mx-auto mb-3" />
<p className="text-slate-500 text-sm">
Aún no hay otros miembros.
</p>
</div>
)}
</div>
</div>
</CardContent>
</Card>
);
}