diff --git a/app.py b/app.py index 9429bc9..d4c2f34 100644 --- a/app.py +++ b/app.py @@ -256,5 +256,35 @@ def calculate_work_duration(arrival_time, departure_time, total_break_duration): return work_duration, effective_break_duration +@app.route('/api/resume/', methods=['POST']) +def resume_entry(entry_id): + # Find the entry to resume + entry_to_resume = TimeEntry.query.get_or_404(entry_id) + + # Check if there's already an active entry + active_entry = TimeEntry.query.filter_by(departure_time=None).first() + if active_entry: + return jsonify({ + 'success': False, + 'message': 'Cannot resume this entry. Another session is already active.' + }), 400 + + # Clear the departure time to make this entry active again + entry_to_resume.departure_time = None + + # Reset pause state if it was paused + entry_to_resume.is_paused = False + entry_to_resume.pause_start_time = None + + db.session.commit() + + return jsonify({ + 'success': True, + 'message': 'Work resumed on existing entry', + 'id': entry_to_resume.id, + 'arrival_time': entry_to_resume.arrival_time.strftime('%Y-%m-%d %H:%M:%S'), + 'total_break_duration': entry_to_resume.total_break_duration + }) + if __name__ == '__main__': app.run(debug=True) \ No newline at end of file diff --git a/static/js/script.js b/static/js/script.js index 218e2c4..1fb14b9 100644 --- a/static/js/script.js +++ b/static/js/script.js @@ -133,3 +133,52 @@ document.addEventListener('DOMContentLoaded', function() { }); } }); + +// Add event listener for resume work buttons +document.addEventListener('click', function(e) { + if (e.target && e.target.classList.contains('resume-work-btn')) { + const entryId = e.target.getAttribute('data-id'); + resumeWork(entryId); + } +}); + +// Function to resume work +function resumeWork(entryId) { + fetch(`/api/resume/${entryId}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + } + }) + .then(response => { + if (!response.ok) { + return response.json().then(data => { + throw new Error(data.message || 'Failed to resume work'); + }); + } + return response.json(); + }) + .then(data => { + if (data.success) { + // Show a notification + const notification = document.createElement('div'); + notification.className = 'notification'; + notification.textContent = data.message; + document.body.appendChild(notification); + + // Remove notification after 3 seconds + setTimeout(() => { + notification.remove(); + }, 3000); + + // Reload the page to show the active session + window.location.reload(); + } else { + alert(data.message); + } + }) + .catch(error => { + console.error('Error:', error); + alert(error.message || 'An error occurred while trying to resume work.'); + }); +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index bf84df1..098ba94 100644 --- a/templates/index.html +++ b/templates/index.html @@ -65,6 +65,9 @@ {{ '%d:%02d:%02d'|format(entry.duration//3600, (entry.duration%3600)//60, entry.duration%60) if entry.duration is not none else 'In progress' }} {{ '%d:%02d:%02d'|format(entry.total_break_duration//3600, (entry.total_break_duration%3600)//60, entry.total_break_duration%60) if entry.total_break_duration is not none else '00:00:00' }} + {% if entry.departure_time and not active_entry %} + + {% endif %}