Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | import { useState } from 'react';
import { useNavigate, Link } from 'react-router-dom';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Building2, Users, Loader2, Plus, X, Trash2, Edit2, Check, Crown, ShieldOff, Mail, UserPlus, AlertTriangle, Info } from 'lucide-react';
import api from '../../services/api';
import toast from 'react-hot-toast';
import { useOrganizationStore } from '../../store/organizationStore';
import { useAuth } from '../../contexts/AuthContext';
export default function OrganizationSettings() {
const { currentOrganization } = useOrganizationStore();
const { user } = useAuth();
const organizationId = currentOrganization?.id;
const queryClient = useQueryClient();
const [editingName, setEditingName] = useState(false);
const [newMemberEmail, setNewMemberEmail] = useState('');
const [showAddMember, setShowAddMember] = useState(false);
const { data: organization, isLoading: orgLoading } = useQuery({
queryKey: ['organization', organizationId],
queryFn: async () => {
const response = await api.get(`/organizations/${organizationId}`);
return response.data.data?.organization;
},
enabled: !!organizationId
});
const { data: members, isLoading: membersLoading } = useQuery({
queryKey: ['organization-members', organizationId],
queryFn: async () => {
const response = await api.get(`/organizations/${organizationId}/members`);
return response.data.data?.members || [];
},
enabled: !!organizationId
});
const { data: invitations, isLoading: invitationsLoading } = useQuery({
queryKey: ['organization-invitations', organizationId],
queryFn: async () => {
const response = await api.get(`/organizations/${organizationId}/invitations`);
return response.data.data?.invitations || [];
},
enabled: !!organizationId
});
const updateOrgMutation = useMutation({
mutationFn: async (data) => {
const response = await api.put(`/organizations/${organizationId}`, data);
return response.data;
},
onSuccess: () => {
queryClient.invalidateQueries(['organization', organizationId]);
toast.success('Organisation mise à jour');
setEditingName(false);
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de la mise à jour');
}
});
const addInvitationMutation = useMutation({
mutationFn: async (email) => {
const response = await api.post(`/organizations/${organizationId}/addInvitation`, { email });
return response.data;
},
onSuccess: () => {
queryClient.invalidateQueries(['organization-invitations', organizationId]);
setNewMemberEmail('');
setShowAddMember(false);
toast.success('Invitation envoyée');
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de l\'envoi de l\'invitation');
}
});
const removeInvitationMutation = useMutation({
mutationFn: async (email) => {
const response = await api.post(`/organizations/${organizationId}/removeInvitation`, { email });
return response.data;
},
onSuccess: () => {
queryClient.invalidateQueries(['organization-invitations', organizationId]);
toast.success('Invitation annulée');
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de l\'annulation');
}
});
const removeMemberMutation = useMutation({
mutationFn: async (userId) => {
const response = await api.delete(`/organizations/${organizationId}/members/${userId}`);
return response.data;
},
onSuccess: () => {
queryClient.invalidateQueries(['organization-members', organizationId]);
toast.success('Membre retiré');
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de la suppression du membre');
}
});
const updateMemberRoleMutation = useMutation({
mutationFn: async ({ userId, role }) => {
const response = await api.patch(`/organizations/${organizationId}/members/${userId}/role`, { role });
return response.data;
},
onSuccess: () => {
queryClient.invalidateQueries(['organization-members', organizationId]);
toast.success('Rôle mis à jour');
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de la mise à jour du rôle');
}
});
const { fetchOrganizations } = useOrganizationStore();
const deleteOrgMutation = useMutation({
mutationFn: async () => {
const response = await api.delete(`/organizations/${organizationId}`);
return response.data;
},
onSuccess: () => {
toast.success('Organisation supprimée');
fetchOrganizations();
navigate('/');
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de la suppression');
}
});
const handleDeleteOrg = () => {
if (window.confirm(`Êtes-vous sûr de vouloir supprimer l'organisation "${organization.name}" ? Cette action est irréversible.`)) {
deleteOrgMutation.mutate();
}
};
const handleNameSubmit = (e) => {
e.preventDefault();
const formData = new FormData(e.target);
updateOrgMutation.mutate({ name: formData.get('name') });
};
const isOwner = organization?.userRole === 'owner';
if (orgLoading) {
return (
<div className="flex items-center justify-center h-64">
<Loader2 className="w-8 h-8 animate-spin text-indigo-600" />
</div>
);
}
if (!organization) {
return (
<div className="text-center py-12">
<p className="text-gray-500">Organisation non trouvée</p>
</div>
);
}
return (
<div className="space-y-6">
<div>
<h2 className="text-2xl font-bold text-gray-900">Paramètres de l'Organisation</h2>
<p className="text-gray-600">Gérez les informations et les membres de votre organisation</p>
</div>
{/* Organization Info */}
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center mb-4">
<div className="p-2 bg-indigo-100 rounded-lg mr-3">
<Building2 className="w-5 h-5 text-indigo-600" />
</div>
<h3 className="text-lg font-medium text-gray-900">Informations</h3>
</div>
<form onSubmit={handleNameSubmit} className="flex items-center space-x-4">
<div className="flex-1">
{editingName ? (
<input
type="text"
name="name"
defaultValue={organization.name}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent"
autoFocus
/>
) : (
<p className="text-xl font-semibold text-gray-900">{organization.name}</p>
)}
</div>
{isOwner && (
editingName ? (
<div className="flex space-x-2">
<button
type="submit"
disabled={updateOrgMutation.isLoading}
className="p-2 bg-green-100 text-green-600 rounded-lg hover:bg-green-200 disabled:opacity-50"
>
<Check className="w-5 h-5" />
</button>
<button
type="button"
onClick={() => setEditingName(false)}
className="p-2 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200"
>
<X className="w-5 h-5" />
</button>
</div>
) : (
<button
type="button"
onClick={() => setEditingName(true)}
className="p-2 bg-gray-100 text-gray-600 rounded-lg hover:bg-gray-200"
>
<Edit2 className="w-5 h-5" />
</button>
)
)}
</form>
</div>
{/* Pending Invitations - only for owner */}
{isOwner && (
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center">
<div className="p-2 bg-yellow-100 rounded-lg mr-3">
<Mail className="w-5 h-5 text-yellow-600" />
</div>
<h3 className="text-lg font-medium text-gray-900">Invitations en attente</h3>
</div>
<button
onClick={() => setShowAddMember(!showAddMember)}
className="flex items-center px-3 py-2 bg-yellow-600 text-white rounded-lg hover:bg-yellow-700"
>
<UserPlus className="w-4 h-4 mr-2" />
Inviter
</button>
</div>
{showAddMember && (
<div className="mb-4 p-4 bg-gray-50 rounded-lg">
<form
onSubmit={(e) => {
e.preventDefault();
if (newMemberEmail) {
addInvitationMutation.mutate(newMemberEmail);
}
}}
className="flex space-x-4"
>
<input
type="email"
value={newMemberEmail}
onChange={(e) => setNewMemberEmail(e.target.value)}
placeholder="Email à inviter"
className="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-yellow-500 focus:border-transparent"
required
/>
<button
type="submit"
disabled={addInvitationMutation.isLoading}
className="px-4 py-2 bg-yellow-600 text-white rounded-lg hover:bg-yellow-700 disabled:opacity-50"
>
{addInvitationMutation.isLoading ? (
<Loader2 className="w-5 h-5 animate-spin" />
) : (
'Inviter'
)}
</button>
<button
type="button"
onClick={() => {
setShowAddMember(false);
setNewMemberEmail('');
}}
className="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300"
>
Annuler
</button>
</form>
</div>
)}
{invitationsLoading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="w-6 h-6 animate-spin text-yellow-600" />
</div>
) : invitations && invitations.length > 0 ? (
<div className="space-y-3">
<div className="flex items-start gap-3 bg-blue-50 border border-blue-200 rounded-lg px-4 py-3 text-sm text-blue-800">
<Info className="w-5 h-5 text-blue-500 flex-shrink-0 mt-0.5" />
<p>L'invité doit accepter l'invitation depuis son <Link to="/profile" className="underline font-medium hover:text-blue-900">profil</Link>.</p>
</div>
{invitations.map((invitation) => (
<div
key={invitation.membershipId}
className="flex items-center justify-between p-3 bg-yellow-50 rounded-lg"
>
<div className="flex items-center">
<div className="w-10 h-10 rounded-full bg-yellow-100 flex items-center justify-center mr-3">
<Mail className="w-5 h-5 text-yellow-600" />
</div>
<div>
<p className="font-medium text-gray-900">{invitation.email}</p>
<p className="text-sm text-gray-500">
Invité le {new Date(invitation.invitedAt).toLocaleDateString()}
</p>
</div>
</div>
<button
onClick={() => removeInvitationMutation.mutate(invitation.email)}
className="p-2 text-red-600 hover:bg-red-50 rounded-lg"
title="Annuler l'invitation"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
))}
</div>
) : (
<p className="text-gray-500 text-center py-4">Aucune invitation en attente</p>
)}
</div>
)}
{/* Members */}
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center mb-4">
<div className="p-2 bg-indigo-100 rounded-lg mr-3">
<Users className="w-5 h-5 text-indigo-600" />
</div>
<h3 className="text-lg font-medium text-gray-900">Membres</h3>
</div>
{membersLoading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="w-6 h-6 animate-spin text-indigo-600" />
</div>
) : members && members.length > 0 ? (
<div className="space-y-3">
{members.map((member) => (
<div
key={member.userId}
className="flex items-center justify-between p-3 bg-gray-50 rounded-lg"
>
<div className="flex items-center">
<div className="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center mr-3">
<span className="text-indigo-600 font-medium">
{member.email?.charAt(0).toUpperCase() || '?'}
</span>
</div>
<div>
<p className="font-medium text-gray-900">{member.name || member.email}</p>
<p className="text-sm text-gray-500">{member.email}</p>
</div>
</div>
<div className="flex items-center space-x-3">
<span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${
member.role === 'owner'
? 'bg-yellow-100 text-yellow-800'
: 'bg-gray-100 text-gray-800'
}`}>
{member.role === 'owner' && <Crown className="w-3 h-3 mr-1" />}
{member.role}
</span>
{isOwner && member.role === 'member' && (
<button
onClick={() => updateMemberRoleMutation.mutate({ userId: member.userId, role: 'owner' })}
disabled={updateMemberRoleMutation.isLoading}
className="p-2 text-yellow-600 hover:bg-yellow-50 rounded-lg"
title="Définir comme owner"
>
<Crown className="w-4 h-4" />
</button>
)}
{isOwner && member.role === 'owner' && member.userId !== user?.id && (
<button
onClick={() => updateMemberRoleMutation.mutate({ userId: member.userId, role: 'member' })}
disabled={updateMemberRoleMutation.isLoading}
className="p-2 text-amber-600 hover:bg-amber-50 rounded-lg"
title="Retirer le rôle owner"
>
<ShieldOff className="w-4 h-4" />
</button>
)}
{member.role !== 'owner' && isOwner && (
<button
onClick={() => removeMemberMutation.mutate(member.userId)}
className="p-2 text-red-600 hover:bg-red-50 rounded-lg"
>
<Trash2 className="w-4 h-4" />
</button>
)}
</div>
</div>
))}
</div>
) : (
<p className="text-gray-500 text-center py-4">Aucun membre</p>
)}
</div>
{/* Danger Zone - only for owner */}
{isOwner && (
<div className="bg-white rounded-lg shadow p-6 border-2 border-red-200">
<div className="flex items-center mb-4">
<div className="p-2 bg-red-100 rounded-lg mr-3">
<AlertTriangle className="w-5 h-5 text-red-600" />
</div>
<h3 className="text-lg font-medium text-red-900">Zone dangereuse</h3>
</div>
<p className="text-gray-600 mb-4">
La suppression d'une organisation est irréversible. Toutes les données associées seront perdues.
</p>
<button
onClick={handleDeleteOrg}
disabled={deleteOrgMutation.isLoading}
className="flex items-center px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 disabled:opacity-50"
>
{deleteOrgMutation.isLoading ? (
<Loader2 className="w-5 h-5 animate-spin mr-2" />
) : (
<Trash2 className="w-5 h-5 mr-2" />
)}
Supprimer l'organisation
</button>
</div>
)}
</div>
);
}
|