|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { User } from '../types/User'; |
| 3 | +import { Navigate } from 'react-router'; |
| 4 | + |
| 5 | +function Profile({ user }: { user: User | null }) { |
| 6 | + const [apiKey, setApiKey] = useState<string | null>(null); |
| 7 | + const [isGenerating, setIsGenerating] = useState(false); |
| 8 | + const [error, setError] = useState<string | null>(null); |
| 9 | + |
| 10 | + if (!user) { |
| 11 | + return <Navigate to="/login" />; |
| 12 | + } |
| 13 | + |
| 14 | + const generateApiKey = async () => { |
| 15 | + setIsGenerating(true); |
| 16 | + setError(null); |
| 17 | + |
| 18 | + try { |
| 19 | + // TODO: Replace with actual backend endpoint when implemented |
| 20 | + // This is a placeholder that simulates API key generation |
| 21 | + setTimeout(() => { |
| 22 | + // Mock API key (in production this would come from the backend) |
| 23 | + const mockApiKey = `key_${Math.random().toString(36).substring(2, 15)}`; |
| 24 | + setApiKey(mockApiKey); |
| 25 | + setIsGenerating(false); |
| 26 | + }, 1000); |
| 27 | + |
| 28 | + // Actual API call would look like: |
| 29 | + // const response = await fetch('http://localhost:8080/api/users/generate-api-key', { |
| 30 | + // method: 'POST', |
| 31 | + // credentials: 'include', |
| 32 | + // headers: { |
| 33 | + // 'Content-Type': 'application/json', |
| 34 | + // } |
| 35 | + // }); |
| 36 | + // const data = await response.json(); |
| 37 | + // setApiKey(data.apiKey); |
| 38 | + } catch (err) { |
| 39 | + setError('Failed to generate API key. Please try again later.'); |
| 40 | + setIsGenerating(false); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="max-w-5xl container mx-auto px-4 py-8"> |
| 46 | + <h1 className="text-3xl font-bold mb-6">User Profile</h1> |
| 47 | + |
| 48 | + <div className="bg-white shadow-md rounded-lg p-6 mb-6"> |
| 49 | + <h2 className="text-xl font-semibold mb-4">Personal Information</h2> |
| 50 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> |
| 51 | + <div> |
| 52 | + <p className="text-sm text-gray-500">First Name</p> |
| 53 | + <p className="font-medium">{user.firstName}</p> |
| 54 | + </div> |
| 55 | + <div> |
| 56 | + <p className="text-sm text-gray-500">Last Name</p> |
| 57 | + <p className="font-medium">{user.lastName}</p> |
| 58 | + </div> |
| 59 | + <div> |
| 60 | + <p className="text-sm text-gray-500">Role</p> |
| 61 | + <p className="font-medium">{user.role}</p> |
| 62 | + </div> |
| 63 | + <div> |
| 64 | + <p className="text-sm text-gray-500">Email</p> |
| 65 | + <p className="font-medium">{user.emailAddress}</p> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + |
| 70 | + <div className="bg-white shadow-md rounded-lg p-6"> |
| 71 | + <h2 className="text-xl font-semibold mb-4">API Access</h2> |
| 72 | + <p className="text-gray-600 mb-4"> |
| 73 | + Generate an API key to access the CourseHub APIs programmatically. Keep this key secure! |
| 74 | + </p> |
| 75 | + |
| 76 | + {apiKey ? ( |
| 77 | + <div className="mb-4"> |
| 78 | + <p className="text-sm text-gray-500 mb-1">Your API Key</p> |
| 79 | + <div className="flex items-center"> |
| 80 | + <input |
| 81 | + type="text" |
| 82 | + value={apiKey} |
| 83 | + readOnly |
| 84 | + className="flex-1 border rounded-md py-2 px-3 bg-gray-50" |
| 85 | + /> |
| 86 | + <button |
| 87 | + className="ml-2 bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-md" |
| 88 | + onClick={() => { |
| 89 | + navigator.clipboard.writeText(apiKey); |
| 90 | + }} |
| 91 | + > |
| 92 | + Copy |
| 93 | + </button> |
| 94 | + </div> |
| 95 | + <p className="text-sm text-red-500 mt-2"> |
| 96 | + Make sure to save this key now! You won't be able to see it again. |
| 97 | + </p> |
| 98 | + </div> |
| 99 | + ) : ( |
| 100 | + <> |
| 101 | + {error && <p className="text-red-500 mb-4">{error}</p>} |
| 102 | + <button |
| 103 | + onClick={generateApiKey} |
| 104 | + disabled={isGenerating} |
| 105 | + className="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 transition disabled:bg-blue-400" |
| 106 | + > |
| 107 | + {isGenerating ? 'Generating...' : 'Generate API Key'} |
| 108 | + </button> |
| 109 | + </> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +export default Profile; |
0 commit comments