File size: 4,141 Bytes
e3365bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// DOM Elements
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('pdf-file');
const fileLabel = document.getElementById('file-label');
const fileNameDisplay = document.getElementById('file-name');
const roastBtn = document.getElementById('roast-btn');
const loader = document.getElementById('loader');
const loadingText = document.getElementById('loading-text');
const resultsArea = document.getElementById('results-area');

// Funny loading messages to keep user entertained while waiting
const loadingMessages = [
    "Scanning for typos...",
    "Judging your font choices...",
    "Laughing at your 'Skills' section...",
    "Consulting the Roast Master...",
    "Preparing emotional damage...",
    "Generating brutally honest feedback..."
];

// --- FILE UPLOAD HANDLING ---
dropZone.addEventListener('click', () => fileInput.click());

fileInput.addEventListener('change', handleFileSelect);

// Drag and drop effects
dropZone.addEventListener('dragover', (e) => {
    e.preventDefault();
    dropZone.style.borderColor = '#ff4b4b';
});

dropZone.addEventListener('dragleave', (e) => {
    e.preventDefault();
    dropZone.style.borderColor = '#30363d';
});

dropZone.addEventListener('drop', (e) => {
    e.preventDefault();
    dropZone.style.borderColor = '#30363d';
    fileInput.files = e.dataTransfer.files;
    handleFileSelect();
});

function handleFileSelect() {
    if (fileInput.files.length > 0) {
        const name = fileInput.files[0].name;
        fileNameDisplay.innerText = name;
        fileLabel.innerText = "File Selected:";
        roastBtn.disabled = false;
        // Hide previous results if they upload a new file
        resultsArea.classList.add('hidden');
    }
}

// --- ROAST ACTION ---
async function startRoast() {
    if (!fileInput.files[0]) {
        alert("Please upload a PDF first!");
        return;
    }

    // UI Updates
    roastBtn.disabled = true;
    roastBtn.innerText = "ROASTING...";
    loader.style.display = 'block';
    resultsArea.classList.add('hidden');

    // Cycle through funny text
    let msgIndex = 0;
    const msgInterval = setInterval(() => {
        loadingText.innerText = loadingMessages[msgIndex];
        msgIndex = (msgIndex + 1) % loadingMessages.length;
    }, 3000);

    // Prepare Data
    const formData = new FormData();
    formData.append('file', fileInput.files[0]); // This matches 'file' in app.py

    try {
        // Send to Backend
        const response = await fetch('/roast', {
            method: 'POST',
            body: formData
        });

        const data = await response.json();
        
        clearInterval(msgInterval); // Stop loading text

        if (data.error) {
            alert("Error: " + data.error);
        } else {
            displayResults(data);
        }

    } catch (error) {
        clearInterval(msgInterval);
        console.error("Error:", error);
        alert("Something went wrong. The Roast Master is sleeping.");
    } finally {
        // Reset UI state
        loader.style.display = 'none';
        roastBtn.innerText = "ROAST ME";
        roastBtn.disabled = false;
    }
}

function displayResults(data) {
    // 1. Set Rating Color based on score
    const scoreElement = document.getElementById('rating-score');
    scoreElement.innerText = `${data.rating}/10`;
    
    if (data.rating < 5) scoreElement.style.color = "#ff4b4b"; // Red
    else if (data.rating < 8) scoreElement.style.color = "#ffa500"; // Orange
    else scoreElement.style.color = "#2ea043"; // Green

    // 2. Set Roast Text
    document.getElementById('roast-content').innerText = `"${data.roast_critique}"`;

    // 3. Set Advice List
    const list = document.getElementById('advice-list');
    list.innerHTML = ""; // Clear old list
    
    data.professional_suggestions.forEach(tip => {
        const li = document.createElement('li');
        li.innerText = tip;
        list.appendChild(li);
    });

    // 4. Reveal Section
    resultsArea.classList.remove('hidden');
    
    // Auto scroll to results
    resultsArea.scrollIntoView({ behavior: 'smooth' });
}