import React, { useState, useEffect } from 'react'; import axios from 'axios'; function MyAccount() { const [wvList, setWvList] = useState([]); const [prList, setPrList] = useState([]); const [uploading, setUploading] = useState(false); const [username, setUsername] = useState(''); const [apiKey, setApiKey] = useState(''); const [password, setPassword] = useState(''); const [passwordError, setPasswordError] = useState(''); const [newApiKey, setNewApiKey] = useState(''); const [apiKeyError, setApiKeyError] = useState(''); // Fetch user info const fetchUserInfo = async () => { try { const response = await axios.post('/userinfo'); setWvList(response.data.Widevine_Devices || []); setPrList(response.data.Playready_Devices || []); setUsername(response.data.Styled_Username || ''); setApiKey(response.data.API_Key || ''); } catch (err) { console.error('Failed to fetch user info', err); } }; useEffect(() => { fetchUserInfo(); }, []); // Handle file upload const handleUpload = async (event, cdmType) => { const file = event.target.files[0]; if (!file) return; const extension = file.name.split('.').pop(); if ((cdmType === 'PR' && extension !== 'prd') || (cdmType === 'WV' && extension !== 'wvd')) { alert(`Please upload a .${cdmType === 'PR' ? 'prd' : 'wvd'} file.`); return; } const formData = new FormData(); formData.append('file', file); setUploading(true); try { await axios.post(`/upload/${cdmType}`, formData); await fetchUserInfo(); // Refresh list after upload } catch (err) { console.error('Upload failed', err); alert('Upload failed'); } finally { setUploading(false); } }; // Handle logout const handleLogout = async () => { try { await axios.post('/logout'); window.location.reload(); } catch (error) { console.error('Logout failed:', error); alert('Logout failed!'); } }; // Handle change password const handleChangePassword = async () => { if (passwordError || password === '') { alert('Please enter a valid password.'); return; } try { const response = await axios.post('/user/change_password', { new_password: password }); if (response.data.message === 'True') { alert('Password changed successfully.'); setPassword(''); } else { alert('Failed to change password.'); } } catch (error) { if (error.response && error.response.data?.message === 'Invalid password format') { alert('Password format is invalid. Please try again.'); } else { alert('Error occurred while changing password.'); } } }; // Handle change API key const handleChangeApiKey = async () => { if (apiKeyError || newApiKey === '') { alert('Please enter a valid API key.'); return; } try { const response = await axios.post('/user/change_api_key', { new_api_key: newApiKey, }); if (response.data.message === 'True') { alert('API key changed successfully.'); setApiKey(newApiKey); setNewApiKey(''); } else { alert('Failed to change API key.'); } } catch (error) { alert('Error occurred while changing API key.'); console.error(error); } }; return (

{username ? `${username}` : 'My Account'}

{/* API Key Section */}
{/* New API Key Section */} { const value = e.target.value; const isValid = /^[^\s]+$/.test(value); // No spaces if (!isValid) { setApiKeyError('API key must not contain spaces.'); } else { setApiKeyError(''); } setNewApiKey(value); }} placeholder="Enter new API key" className="w-full p-2 mb-1 rounded bg-gray-800 text-white border border-gray-600 text-center" /> {apiKeyError &&

{apiKeyError}

} {/* Change Password Section */} { const value = e.target.value; const isValid = /^[A-Za-z0-9!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]*$/.test(value); if (!isValid) { setPasswordError('Password must not contain spaces or invalid characters.'); } else { setPasswordError(''); } setPassword(value); }} placeholder="New Password" className="w-full p-2 mb-1 rounded bg-gray-800 text-white border border-gray-600 text-center" /> {passwordError &&

{passwordError}

}
{/* Widevine Section */}

Widevine CDMs

{wvList.length === 0 ? (
No Widevine CDMs uploaded.
) : ( wvList.map((filename, i) => (
{filename}
)) )}
{/* Playready Section */}

Playready CDMs

{prList.length === 0 ? (
No Playready CDMs uploaded.
) : ( prList.map((filename, i) => (
{filename}
)) )}
); } export default MyAccount;