361 lines
12 KiB
HTML
361 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Project Thumbnail Test</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
background: #f5f5f5;
|
|
}
|
|
.container {
|
|
background: white;
|
|
padding: 20px;
|
|
border-radius: 8px;
|
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
}
|
|
h1 {
|
|
color: #333;
|
|
border-bottom: 2px solid #007bff;
|
|
padding-bottom: 10px;
|
|
}
|
|
.test-section {
|
|
margin: 20px 0;
|
|
padding: 15px;
|
|
background: #f8f9fa;
|
|
border-radius: 4px;
|
|
}
|
|
.test-section h2 {
|
|
color: #007bff;
|
|
margin-top: 0;
|
|
}
|
|
button {
|
|
background: #007bff;
|
|
color: white;
|
|
border: none;
|
|
padding: 10px 20px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
margin: 5px;
|
|
}
|
|
button:hover {
|
|
background: #0056b3;
|
|
}
|
|
button:disabled {
|
|
background: #ccc;
|
|
cursor: not-allowed;
|
|
}
|
|
.result {
|
|
margin-top: 10px;
|
|
padding: 10px;
|
|
background: white;
|
|
border-radius: 4px;
|
|
border: 1px solid #ddd;
|
|
}
|
|
.success {
|
|
color: #28a745;
|
|
font-weight: bold;
|
|
}
|
|
.error {
|
|
color: #dc3545;
|
|
font-weight: bold;
|
|
}
|
|
.project-card {
|
|
border: 1px solid #ddd;
|
|
border-radius: 8px;
|
|
padding: 15px;
|
|
margin: 10px 0;
|
|
background: white;
|
|
}
|
|
.thumbnail {
|
|
width: 100%;
|
|
max-width: 300px;
|
|
height: 200px;
|
|
object-fit: cover;
|
|
border-radius: 4px;
|
|
margin: 10px 0;
|
|
}
|
|
.placeholder {
|
|
width: 100%;
|
|
max-width: 300px;
|
|
height: 200px;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: white;
|
|
font-size: 48px;
|
|
font-weight: bold;
|
|
border-radius: 4px;
|
|
margin: 10px 0;
|
|
}
|
|
input[type="file"] {
|
|
margin: 10px 0;
|
|
}
|
|
pre {
|
|
background: #f4f4f4;
|
|
padding: 10px;
|
|
border-radius: 4px;
|
|
overflow-x: auto;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>🎬 Project Thumbnail Upload Test</h1>
|
|
|
|
<div class="test-section">
|
|
<h2>Test 1: Fetch Projects with Thumbnails</h2>
|
|
<button onclick="fetchProjects()">Fetch Projects</button>
|
|
<div id="projects-result" class="result"></div>
|
|
</div>
|
|
|
|
<div class="test-section">
|
|
<h2>Test 2: Upload Thumbnail</h2>
|
|
<p>Select a project and upload a thumbnail image:</p>
|
|
<select id="project-select">
|
|
<option value="">-- Select Project --</option>
|
|
</select>
|
|
<br>
|
|
<input type="file" id="thumbnail-file" accept=".jpg,.jpeg,.png,.gif,.webp">
|
|
<br>
|
|
<button onclick="uploadThumbnail()" id="upload-btn" disabled>Upload Thumbnail</button>
|
|
<div id="upload-result" class="result"></div>
|
|
</div>
|
|
|
|
<div class="test-section">
|
|
<h2>Test 3: Delete Thumbnail</h2>
|
|
<p>Select a project with a thumbnail to delete:</p>
|
|
<select id="delete-project-select">
|
|
<option value="">-- Select Project --</option>
|
|
</select>
|
|
<br>
|
|
<button onclick="deleteThumbnail()" id="delete-btn" disabled>Delete Thumbnail</button>
|
|
<div id="delete-result" class="result"></div>
|
|
</div>
|
|
|
|
<div class="test-section">
|
|
<h2>Test 4: Display Projects with Thumbnails</h2>
|
|
<button onclick="displayProjects()">Display All Projects</button>
|
|
<div id="display-result"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const API_BASE = 'http://localhost:8000';
|
|
let authToken = '';
|
|
let projects = [];
|
|
|
|
// Login first
|
|
async function login() {
|
|
try {
|
|
const response = await fetch(`${API_BASE}/auth/login`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: 'username=admin@example.com&password=admin123'
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Login failed');
|
|
}
|
|
|
|
const data = await response.json();
|
|
authToken = data.access_token;
|
|
console.log('✅ Logged in successfully');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ Login failed:', error);
|
|
alert('Please ensure the backend is running and you have an admin user');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function fetchProjects() {
|
|
const resultDiv = document.getElementById('projects-result');
|
|
resultDiv.innerHTML = 'Loading...';
|
|
|
|
if (!authToken && !await login()) {
|
|
resultDiv.innerHTML = '<span class="error">Login failed</span>';
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE}/projects/`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${authToken}`
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP ${response.status}`);
|
|
}
|
|
|
|
projects = await response.json();
|
|
|
|
// Populate select dropdowns
|
|
const projectSelect = document.getElementById('project-select');
|
|
const deleteSelect = document.getElementById('delete-project-select');
|
|
projectSelect.innerHTML = '<option value="">-- Select Project --</option>';
|
|
deleteSelect.innerHTML = '<option value="">-- Select Project --</option>';
|
|
|
|
projects.forEach(project => {
|
|
const option1 = document.createElement('option');
|
|
option1.value = project.id;
|
|
option1.textContent = `${project.name} ${project.thumbnail_url ? '(has thumbnail)' : ''}`;
|
|
projectSelect.appendChild(option1);
|
|
|
|
if (project.thumbnail_url) {
|
|
const option2 = document.createElement('option');
|
|
option2.value = project.id;
|
|
option2.textContent = project.name;
|
|
deleteSelect.appendChild(option2);
|
|
}
|
|
});
|
|
|
|
resultDiv.innerHTML = `
|
|
<span class="success">✅ Success!</span>
|
|
<p>Found ${projects.length} projects</p>
|
|
<p>Projects with thumbnails: ${projects.filter(p => p.thumbnail_url).length}</p>
|
|
<pre>${JSON.stringify(projects.map(p => ({
|
|
id: p.id,
|
|
name: p.name,
|
|
thumbnail_url: p.thumbnail_url
|
|
})), null, 2)}</pre>
|
|
`;
|
|
} catch (error) {
|
|
resultDiv.innerHTML = `<span class="error">❌ Error: ${error.message}</span>`;
|
|
}
|
|
}
|
|
|
|
document.getElementById('project-select').addEventListener('change', (e) => {
|
|
document.getElementById('upload-btn').disabled = !e.target.value;
|
|
});
|
|
|
|
document.getElementById('delete-project-select').addEventListener('change', (e) => {
|
|
document.getElementById('delete-btn').disabled = !e.target.value;
|
|
});
|
|
|
|
async function uploadThumbnail() {
|
|
const projectId = document.getElementById('project-select').value;
|
|
const fileInput = document.getElementById('thumbnail-file');
|
|
const resultDiv = document.getElementById('upload-result');
|
|
|
|
if (!projectId || !fileInput.files[0]) {
|
|
resultDiv.innerHTML = '<span class="error">Please select a project and file</span>';
|
|
return;
|
|
}
|
|
|
|
resultDiv.innerHTML = 'Uploading...';
|
|
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append('file', fileInput.files[0]);
|
|
|
|
const response = await fetch(`${API_BASE}/projects/${projectId}/thumbnail`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${authToken}`
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(error.detail || `HTTP ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
resultDiv.innerHTML = `
|
|
<span class="success">✅ Upload successful!</span>
|
|
<p>Thumbnail URL: ${data.thumbnail_url}</p>
|
|
<img src="${API_BASE}${data.thumbnail_url}" class="thumbnail" alt="Uploaded thumbnail">
|
|
`;
|
|
|
|
// Refresh projects
|
|
await fetchProjects();
|
|
} catch (error) {
|
|
resultDiv.innerHTML = `<span class="error">❌ Error: ${error.message}</span>`;
|
|
}
|
|
}
|
|
|
|
async function deleteThumbnail() {
|
|
const projectId = document.getElementById('delete-project-select').value;
|
|
const resultDiv = document.getElementById('delete-result');
|
|
|
|
if (!projectId) {
|
|
resultDiv.innerHTML = '<span class="error">Please select a project</span>';
|
|
return;
|
|
}
|
|
|
|
if (!confirm('Are you sure you want to delete this thumbnail?')) {
|
|
return;
|
|
}
|
|
|
|
resultDiv.innerHTML = 'Deleting...';
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE}/projects/${projectId}/thumbnail`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Authorization': `Bearer ${authToken}`
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(error.detail || `HTTP ${response.status}`);
|
|
}
|
|
|
|
resultDiv.innerHTML = '<span class="success">✅ Thumbnail deleted successfully!</span>';
|
|
|
|
// Refresh projects
|
|
await fetchProjects();
|
|
} catch (error) {
|
|
resultDiv.innerHTML = `<span class="error">❌ Error: ${error.message}</span>`;
|
|
}
|
|
}
|
|
|
|
async function displayProjects() {
|
|
const resultDiv = document.getElementById('display-result');
|
|
|
|
if (projects.length === 0) {
|
|
await fetchProjects();
|
|
}
|
|
|
|
resultDiv.innerHTML = projects.map(project => `
|
|
<div class="project-card">
|
|
<h3>${project.name}</h3>
|
|
${project.thumbnail_url
|
|
? `<img src="${API_BASE}${project.thumbnail_url}" class="thumbnail" alt="${project.name}">`
|
|
: `<div class="placeholder">${getInitials(project.name)}</div>`
|
|
}
|
|
<p><strong>Code:</strong> ${project.code_name}</p>
|
|
<p><strong>Client:</strong> ${project.client_name}</p>
|
|
<p><strong>Status:</strong> ${project.status}</p>
|
|
</div>
|
|
`).join('');
|
|
}
|
|
|
|
function getInitials(name) {
|
|
const words = name.split(' ');
|
|
if (words.length >= 2) {
|
|
return (words[0].charAt(0) + words[1].charAt(0)).toUpperCase();
|
|
}
|
|
return name.substring(0, 2).toUpperCase();
|
|
}
|
|
|
|
// Auto-login on page load
|
|
window.addEventListener('load', async () => {
|
|
await login();
|
|
await fetchProjects();
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|