All files / apps/web/src/frontoffice/pages Profile.jsx

0% Statements 0/40
0% Branches 0/20
0% Functions 0/14
0% Lines 0/40

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '../../contexts/AuthContext';
import { useOrganizationStore } from '../../store/organizationStore';
import { User, Building2, Trash2, Loader2, Check, X, Mail } from 'lucide-react';
import toast from 'react-hot-toast';
 
export default function Profile() {
  const navigate = useNavigate();
  const { 
    user, 
    organizations, 
    pendingInvitations, 
    updateDefaultOrganization, 
    deleteAccount,
    acceptInvitation,
    declineInvitation,
    logout
  } = useAuth();
  const { setCurrentOrganization } = useOrganizationStore();
  const [deletingAccount, setDeletingAccount] = useState(false);
  const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
 
  const handleAcceptInvitation = async (organizationId) => {
    const result = await acceptInvitation(organizationId);
    if (result.success) {
      toast.success('Invitation acceptée');
    } else {
      toast.error(result.error);
    }
  };
 
  const handleDeclineInvitation = async (organizationId) => {
    const result = await declineInvitation(organizationId);
    if (result.success) {
      toast.success('Invitation déclinée');
    } else {
      toast.error(result.error);
    }
  };
 
  const handleDeleteAccount = async () => {
    setDeletingAccount(true);
    const result = await deleteAccount();
    setDeletingAccount(false);
    if (result.success) {
      toast.success('Compte supprimé');
      navigate('/');
    } else {
      toast.error(result.error);
    }
    setShowDeleteConfirm(false);
  };
 
  const handleLogout = () => {
    logout();
    navigate('/');
  };
 
  if (!user) {
    return (
      <div className="flex items-center justify-center h-64">
        <Loader2 className="w-8 h-8 animate-spin text-indigo-600" />
      </div>
    );
  }
 
  return (
    <div className="max-w-2xl mx-auto py-8 px-4">
      <h1 className="text-2xl font-bold text-gray-900 mb-8">Mon Compte</h1>
 
      {/* User Info */}
      <div className="bg-white rounded-lg shadow p-6 mb-6">
        <div className="flex items-center mb-4">
          <div className="p-2 bg-indigo-100 rounded-lg mr-3">
            <User className="w-5 h-5 text-indigo-600" />
          </div>
          <h2 className="text-lg font-medium text-gray-900">Informations</h2>
        </div>
        
        <div className="space-y-3">
          <div>
            <label className="text-sm text-gray-500">Nom</label>
            <p className="text-gray-900">{user.name}</p>
          </div>
          <div>
            <label className="text-sm text-gray-500">Email</label>
            <p className="text-gray-900">{user.email}</p>
          </div>
          <div>
            <label className="text-sm text-gray-500">Rôle global</label>
            <p className="text-gray-900 capitalize">{user.role}</p>
          </div>
        </div>
      </div>
 
      {/* Pending Invitations */}
      {pendingInvitations.length > 0 && (
        <div className="bg-white rounded-lg shadow p-6 mb-6">
          <div className="flex items-center mb-4">
            <div className="p-2 bg-yellow-100 rounded-lg mr-3">
              <Mail className="w-5 h-5 text-yellow-600" />
            </div>
            <h2 className="text-lg font-medium text-gray-900">Invitations en attente</h2>
          </div>
          
          <div className="space-y-3">
            {pendingInvitations.map(invitation => (
              <div key={invitation.organizationId} className="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                <div>
                  <p className="font-medium text-gray-900">{invitation.organizationName}</p>
                  <p className="text-sm text-gray-500">Invitation en attente</p>
                </div>
                <div className="flex space-x-2">
                  <button
                    onClick={() => handleAcceptInvitation(invitation.organizationId)}
                    className="p-2 bg-green-100 text-green-600 rounded-lg hover:bg-green-200"
                    title="Accepter"
                  >
                    <Check className="w-4 h-4" />
                  </button>
                  <button
                    onClick={() => handleDeclineInvitation(invitation.organizationId)}
                    className="p-2 bg-red-100 text-red-600 rounded-lg hover:bg-red-200"
                    title="Décliner"
                  >
                    <X className="w-4 h-4" />
                  </button>
                </div>
              </div>
            ))}
          </div>
        </div>
      )}
 
      {/* Organizations */}
      {organizations.length > 0 && (
        <div className="bg-white rounded-lg shadow p-6 mb-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>
            <h2 className="text-lg font-medium text-gray-900">Mes organisations</h2>
          </div>
          
          <div className="space-y-2">
            {organizations.map(org => (
              <div key={org.id} className="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                <div>
                  <p className="font-medium text-gray-900">{org.name}</p>
                  <p className="text-sm text-gray-500 capitalize">{org.role}</p>
                </div>
                <button
                  onClick={() => {
                    setCurrentOrganization(org);
                    navigate('/');
                  }}
                  className="text-indigo-600 hover:text-indigo-800 text-sm"
                >
                  Ouvrir →
                </button>
              </div>
            ))}
          </div>
        </div>
      )}
 
      {/* Default Organization */}
      <div className="bg-white rounded-lg shadow p-6 mb-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>
          <h2 className="text-lg font-medium text-gray-900">Organisation par défaut</h2>
        </div>
        
        <select
          value={user.defaultOrganizationId || ''}
          onChange={(e) => updateDefaultOrganization(e.target.value || null)}
          className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-transparent"
        >
          <option value="">Aucune organisation par défaut</option>
          {organizations.map(org => (
            <option key={org.id} value={org.id}>
              {org.name} ({org.role})
            </option>
          ))}
        </select>
        <p className="text-sm text-gray-500 mt-2">
          L'organisation sélectionnée sera chargée automatiquement à chaque connexion.
        </p>
      </div>
 
      {/* Danger Zone */}
      <div className="bg-white rounded-lg shadow p-6 border border-red-200">
        <div className="flex items-center mb-4">
          <div className="p-2 bg-red-100 rounded-lg mr-3">
            <Trash2 className="w-5 h-5 text-red-600" />
          </div>
          <h2 className="text-lg font-medium text-gray-900">Zone dangereuse</h2>
        </div>
        
        {!showDeleteConfirm ? (
          <button
            onClick={() => setShowDeleteConfirm(true)}
            className="px-4 py-2 bg-red-100 text-red-600 rounded-lg hover:bg-red-200"
          >
            Supprimer mon compte
          </button>
        ) : (
          <div className="space-y-3">
            <p className="text-red-600 font-medium">Êtes-vous sûr ? Cette action est irréversible.</p>
            <div className="flex space-x-3">
              <button
                onClick={handleDeleteAccount}
                disabled={deletingAccount}
                className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 disabled:opacity-50"
              >
                {deletingAccount ? <Loader2 className="w-4 h-4 animate-spin" /> : 'Confirmer la suppression'}
              </button>
              <button
                onClick={() => setShowDeleteConfirm(false)}
                className="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300"
              >
                Annuler
              </button>
            </div>
          </div>
        )}
      </div>
 
      {/* Logout */}
      <div className="mt-6 text-center">
        <button
          onClick={handleLogout}
          className="text-gray-500 hover:text-gray-700"
        >
          Se déconnecter
        </button>
      </div>
    </div>
  );
}