CoolFace
Apppublic

Henry84003/Workout-Recommender

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
script.js61 linesDownload Raw Back to static
1document.addEventListener('DOMContentLoaded', () => {2 3    // Form Submission Logic4    const form = document.getElementById('prediction-form');5    const submitBtn = document.getElementById('submit-btn');6    const btnText = submitBtn.querySelector('.btn-text');7    const loader = submitBtn.querySelector('.loader');8    const resultContainer = document.getElementById('result-container');9    const recommendationText = document.getElementById('recommendation-text');10 11    form.addEventListener('submit', async (e) => {12        e.preventDefault();13 14        // Get values15        const weight = document.getElementById('weight').value;16        const height = document.getElementById('height').value;17        const bfp = document.getElementById('bfp').value;18        const age = document.getElementById('age').value;19        const gender = document.querySelector('input[name="gender"]:checked').value;20 21        // UI Loading state22        btnText.classList.add('hidden');23        loader.classList.remove('hidden');24        submitBtn.disabled = true;25        resultContainer.classList.add('hidden');26 27        try {28            const response = await fetch('/predict', {29                method: 'POST',30                headers: {31                    'Content-Type': 'application/json'32                },33                body: JSON.stringify({34                    weight, height, bfp, age, gender35                })36            });37 38            const data = await response.json();39 40            // Display Result41            recommendationText.textContent = data.recommendation;42            document.getElementById('description-text').textContent = data.description;43            resultContainer.classList.remove('hidden');44 45            // Scroll to result46            resultContainer.scrollIntoView({ behavior: 'smooth', block: 'end' });47 48        } catch (error) {49            console.error('Error:', error);50            alert('An error occurred while generating the recommendation.');51        } finally {52            // Restore UI state53            btnText.classList.remove('hidden');54            loader.classList.add('hidden');55            submitBtn.disabled = false;56        }57    });58 59 60});61