Files
clawd/dashboard/habits.html
2026-02-10 16:45:11 +00:00

1234 lines
43 KiB
HTML

<!DOCTYPE html>
<html lang="ro">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="/echo/favicon.svg">
<title>Echo · Habits</title>
<link rel="stylesheet" href="/echo/common.css">
<script src="https://unpkg.com/lucide@latest/dist/umd/lucide.min.js"></script>
<script src="/echo/swipe-nav.js"></script>
<style>
.main {
max-width: 1400px;
margin: 0 auto;
padding: var(--space-5);
}
.page-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: var(--space-6);
flex-wrap: wrap;
gap: var(--space-4);
}
.page-title {
font-size: var(--text-xl);
font-weight: 600;
color: var(--text-primary);
}
/* Habits grid */
.habits-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
gap: var(--space-4);
}
@media (max-width: 768px) {
.habits-grid {
grid-template-columns: 1fr;
}
}
@media (min-width: 769px) and (max-width: 1200px) {
.habits-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1201px) {
.habits-grid {
grid-template-columns: repeat(3, 1fr);
}
}
/* Empty state */
.empty-state {
text-align: center;
padding: var(--space-10);
color: var(--text-muted);
}
.empty-state svg {
width: 64px;
height: 64px;
margin-bottom: var(--space-4);
opacity: 0.5;
}
.empty-state p {
font-size: var(--text-lg);
margin-bottom: var(--space-2);
}
.empty-state .hint {
font-size: var(--text-sm);
opacity: 0.7;
}
/* Habit card */
.habit-card {
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
border-left: 4px solid var(--accent);
padding: var(--space-4);
transition: all var(--transition-base);
display: flex;
flex-direction: column;
gap: var(--space-3);
}
.habit-card:hover {
border-color: var(--accent);
transform: translateY(-2px);
box-shadow: var(--shadow-md);
}
.habit-card-header {
display: flex;
align-items: center;
gap: var(--space-2);
}
.habit-card-icon {
width: 20px;
height: 20px;
color: var(--text-primary);
flex-shrink: 0;
}
.habit-card-name {
flex: 1;
font-size: var(--text-base);
font-weight: 600;
color: var(--text-primary);
}
.habit-card-actions {
display: flex;
gap: var(--space-2);
}
.habit-card-action-btn {
background: none;
border: none;
color: var(--text-muted);
cursor: pointer;
padding: var(--space-1);
display: flex;
align-items: center;
justify-content: center;
border-radius: var(--radius-md);
transition: all var(--transition-base);
}
.habit-card-action-btn:hover {
background: var(--bg-hover);
color: var(--text-primary);
}
.habit-card-action-btn svg {
width: 16px;
height: 16px;
}
.habit-card-streaks {
display: flex;
gap: var(--space-4);
font-size: var(--text-sm);
color: var(--text-muted);
}
.habit-card-streak {
display: flex;
align-items: center;
gap: var(--space-1);
}
.habit-card-check-btn {
width: 100%;
padding: var(--space-3);
border: 2px solid var(--accent);
background: var(--accent);
color: white;
border-radius: var(--radius-md);
font-size: var(--text-base);
font-weight: 600;
cursor: pointer;
transition: all var(--transition-base);
display: flex;
align-items: center;
justify-content: center;
gap: var(--space-2);
}
.habit-card-check-btn:hover:not(:disabled) {
background: var(--accent-hover);
transform: scale(1.02);
}
.habit-card-check-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
background: var(--bg-muted);
border-color: var(--border);
color: var(--text-muted);
}
.habit-card-last-check {
font-size: var(--text-sm);
color: var(--text-muted);
text-align: center;
}
.habit-card-lives {
display: flex;
justify-content: center;
gap: var(--space-1);
font-size: var(--text-lg);
}
.habit-card-completion {
font-size: var(--text-sm);
color: var(--text-muted);
text-align: center;
}
.habit-card-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding-top: var(--space-2);
border-top: 1px solid var(--border);
}
.habit-card-category {
font-size: var(--text-xs);
padding: var(--space-1) var(--space-2);
border-radius: var(--radius-sm);
background: var(--bg-muted);
color: var(--text-muted);
}
.habit-card-priority {
font-size: var(--text-xs);
color: var(--text-muted);
display: flex;
align-items: center;
gap: var(--space-1);
}
.priority-indicator {
width: 8px;
height: 8px;
border-radius: 50%;
}
.priority-high {
background: var(--error);
}
.priority-medium {
background: var(--warning);
}
.priority-low {
background: var(--success);
}
/* Modal overlay */
.modal-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.7);
z-index: 1000;
align-items: center;
justify-content: center;
padding: var(--space-4);
}
.modal-overlay.active {
display: flex;
}
.modal {
background: var(--bg-surface);
border-radius: var(--radius-lg);
max-width: 600px;
width: 100%;
max-height: 90vh;
overflow-y: auto;
box-shadow: var(--shadow-lg);
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: var(--space-4);
border-bottom: 1px solid var(--border);
}
.modal-title {
font-size: var(--text-lg);
font-weight: 600;
color: var(--text-primary);
}
.modal-close {
background: none;
border: none;
color: var(--text-muted);
cursor: pointer;
padding: var(--space-1);
display: flex;
align-items: center;
justify-content: center;
border-radius: var(--radius-md);
transition: all var(--transition-base);
}
.modal-close:hover {
background: var(--bg-hover);
color: var(--text-primary);
}
.modal-close svg {
width: 20px;
height: 20px;
}
.modal-body {
padding: var(--space-4);
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: var(--space-2);
padding: var(--space-4);
border-top: 1px solid var(--border);
}
/* Form fields */
.form-field {
margin-bottom: var(--space-4);
}
.form-label {
display: block;
font-size: var(--text-sm);
font-weight: 500;
color: var(--text-primary);
margin-bottom: var(--space-1);
}
.form-label.required::after {
content: '*';
color: var(--error);
margin-left: var(--space-1);
}
.form-input,
.form-select,
.form-textarea {
width: 100%;
padding: var(--space-2);
border: 1px solid var(--border);
border-radius: var(--radius-md);
background: var(--bg-base);
color: var(--text-primary);
font-size: var(--text-sm);
transition: all var(--transition-base);
}
.form-input:focus,
.form-select:focus,
.form-textarea:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-muted);
}
.form-textarea {
min-height: 80px;
resize: vertical;
font-family: inherit;
}
/* Color picker */
.color-picker-swatches {
display: grid;
grid-template-columns: repeat(8, 1fr);
gap: var(--space-2);
margin-bottom: var(--space-2);
}
.color-swatch {
width: 100%;
aspect-ratio: 1;
border: 2px solid transparent;
border-radius: var(--radius-md);
cursor: pointer;
transition: all var(--transition-base);
}
.color-swatch:hover {
transform: scale(1.1);
}
.color-swatch.selected {
border-color: var(--text-primary);
box-shadow: 0 0 0 2px var(--bg-surface), 0 0 0 4px var(--accent);
}
/* Icon picker */
.icon-picker-grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: var(--space-2);
max-height: 250px;
overflow-y: auto;
padding: var(--space-2);
border: 1px solid var(--border);
border-radius: var(--radius-md);
background: var(--bg-base);
}
.icon-option {
aspect-ratio: 1;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid transparent;
border-radius: var(--radius-md);
cursor: pointer;
transition: all var(--transition-base);
background: var(--bg-surface);
}
.icon-option:hover {
background: var(--bg-hover);
transform: scale(1.1);
}
.icon-option.selected {
border-color: var(--accent);
background: var(--accent-muted);
}
.icon-option svg {
width: 20px;
height: 20px;
color: var(--text-primary);
}
/* Frequency params */
.frequency-params {
margin-top: var(--space-2);
}
.day-checkboxes {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: var(--space-2);
}
.day-checkbox-label {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--space-1);
padding: var(--space-2);
border: 1px solid var(--border);
border-radius: var(--radius-md);
cursor: pointer;
transition: all var(--transition-base);
font-size: var(--text-xs);
}
.day-checkbox-label:has(input:checked) {
background: var(--accent-muted);
border-color: var(--accent);
}
.day-checkbox-label input {
margin: 0;
}
/* Toast notification */
.toast {
position: fixed;
bottom: var(--space-4);
right: var(--space-4);
padding: var(--space-3) var(--space-4);
border-radius: var(--radius-md);
background: var(--bg-surface);
border: 1px solid var(--border);
box-shadow: var(--shadow-lg);
z-index: 2000;
display: flex;
align-items: center;
gap: var(--space-2);
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.toast.success {
border-color: var(--success);
}
.toast.error {
border-color: var(--error);
}
.toast svg {
width: 20px;
height: 20px;
}
.toast.success svg {
color: var(--success);
}
.toast.error svg {
color: var(--error);
}
</style>
</head>
<body>
<header class="header">
<a href="/echo/index.html" class="logo">
<i data-lucide="circle-dot"></i>
Echo
</a>
<nav class="nav">
<a href="/echo/index.html" class="nav-item">
<i data-lucide="layout-dashboard"></i>
<span>Dashboard</span>
</a>
<a href="/echo/workspace.html" class="nav-item">
<i data-lucide="code"></i>
<span>Workspace</span>
</a>
<a href="/echo/notes.html" class="nav-item">
<i data-lucide="file-text"></i>
<span>KB</span>
</a>
<a href="/echo/habits.html" class="nav-item active">
<i data-lucide="dumbbell"></i>
<span>Habits</span>
</a>
<a href="/echo/files.html" class="nav-item">
<i data-lucide="folder"></i>
<span>Files</span>
</a>
<button class="theme-toggle" onclick="toggleTheme()" title="Schimbă tema">
<i data-lucide="sun" id="themeIcon"></i>
</button>
</nav>
</header>
<main class="main">
<div class="page-header">
<h1 class="page-title">Habits</h1>
<button class="btn btn-primary" onclick="showAddHabitModal()">
<i data-lucide="plus"></i>
Add Habit
</button>
</div>
<div id="habitsContainer">
<div class="empty-state">
<i data-lucide="loader"></i>
<p>Loading habits...</p>
</div>
</div>
</main>
<!-- Add/Edit Habit Modal -->
<div id="habitModal" class="modal-overlay">
<div class="modal">
<div class="modal-header">
<h2 class="modal-title">Add Habit</h2>
<button class="modal-close" onclick="closeHabitModal()">
<i data-lucide="x"></i>
</button>
</div>
<div class="modal-body">
<form id="habitForm">
<!-- Name -->
<div class="form-field">
<label class="form-label required" for="habitName">Name</label>
<input type="text" id="habitName" class="form-input" maxlength="100" required>
</div>
<!-- Category -->
<div class="form-field">
<label class="form-label" for="habitCategory">Category</label>
<select id="habitCategory" class="form-select">
<option value="work">Work</option>
<option value="health">Health</option>
<option value="growth">Growth</option>
<option value="personal">Personal</option>
</select>
</div>
<!-- Color -->
<div class="form-field">
<label class="form-label">Color</label>
<div class="color-picker-swatches" id="colorSwatches"></div>
<input type="text" id="customColor" class="form-input" placeholder="#RRGGBB" pattern="^#[0-9A-Fa-f]{6}$">
</div>
<!-- Icon -->
<div class="form-field">
<label class="form-label">Icon</label>
<div class="icon-picker-grid" id="iconPicker"></div>
</div>
<!-- Priority -->
<div class="form-field">
<label class="form-label" for="habitPriority">Priority (1-100)</label>
<input type="number" id="habitPriority" class="form-input" min="1" max="100" value="50">
</div>
<!-- Notes -->
<div class="form-field">
<label class="form-label" for="habitNotes">Notes</label>
<textarea id="habitNotes" class="form-textarea"></textarea>
</div>
<!-- Frequency Type -->
<div class="form-field">
<label class="form-label" for="frequencyType">Frequency</label>
<select id="frequencyType" class="form-select" onchange="updateFrequencyParams()">
<option value="daily">Daily</option>
<option value="specific_days">Specific Days</option>
<option value="x_per_week">X times per week</option>
<option value="weekly">Weekly</option>
<option value="monthly">Monthly</option>
<option value="custom">Custom</option>
</select>
</div>
<!-- Frequency Params (conditional) -->
<div id="frequencyParams" class="frequency-params"></div>
<!-- Reminder Time -->
<div class="form-field">
<label class="form-label" for="reminderTime">Reminder Time (optional)</label>
<input type="time" id="reminderTime" class="form-input">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn" onclick="closeHabitModal()">Cancel</button>
<button type="submit" class="btn btn-primary" id="submitHabitBtn" onclick="submitHabitForm(event)">
<span id="submitBtnText">Create Habit</span>
</button>
</div>
</div>
</div>
<script>
// Theme management
function initTheme() {
const saved = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', saved);
updateThemeIcon(saved);
}
function toggleTheme() {
const current = document.documentElement.getAttribute('data-theme') || 'dark';
const next = current === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', next);
localStorage.setItem('theme', next);
updateThemeIcon(next);
}
function updateThemeIcon(theme) {
const icon = document.getElementById('themeIcon');
if (icon) {
icon.setAttribute('data-lucide', theme === 'dark' ? 'sun' : 'moon');
lucide.createIcons();
}
}
initTheme();
// Habits state
let habits = [];
// Load habits from API
async function loadHabits() {
try {
const response = await fetch('/echo/api/habits');
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
habits = await response.json();
renderHabits();
} catch (error) {
console.error('Failed to load habits:', error);
showError('Failed to load habits: ' + error.message);
}
}
// Render habits grid
function renderHabits() {
const container = document.getElementById('habitsContainer');
if (habits.length === 0) {
container.innerHTML = `
<div class="empty-state">
<i data-lucide="dumbbell"></i>
<p>No habits yet. Create your first habit!</p>
<p class="hint">Click "Add Habit" to get started</p>
</div>
`;
lucide.createIcons();
return;
}
const habitsHtml = habits.map(habit => renderHabitCard(habit)).join('');
container.innerHTML = `<div class="habits-grid">${habitsHtml}</div>`;
lucide.createIcons();
}
// Render single habit card
function renderHabitCard(habit) {
const isDoneToday = isCheckedToday(habit);
const lastCheckInfo = getLastCheckInfo(habit);
const livesHtml = renderLives(habit.lives || 3);
const completionRate = habit.completion_rate_30d || 0;
return `
<div class="habit-card" style="border-left-color: ${habit.color}">
<div class="habit-card-header">
<i data-lucide="${habit.icon || 'circle'}" class="habit-card-icon"></i>
<span class="habit-card-name">${escapeHtml(habit.name)}</span>
<div class="habit-card-actions">
<button class="habit-card-action-btn" onclick="showEditHabitModal('${habit.id}')" title="Edit">
<i data-lucide="settings"></i>
</button>
<button class="habit-card-action-btn" onclick="deleteHabit('${habit.id}')" title="Delete">
<i data-lucide="trash-2"></i>
</button>
</div>
</div>
<div class="habit-card-streaks">
<div class="habit-card-streak">
🔥 ${habit.streak?.current || 0}
</div>
<div class="habit-card-streak">
🏆 ${habit.streak?.best || 0}
</div>
</div>
<button
class="habit-card-check-btn"
onclick="checkInHabit('${habit.id}')"
${isDoneToday ? 'disabled' : ''}
>
${isDoneToday ? '✓ Done today' : 'Check In'}
</button>
<div class="habit-card-last-check">${lastCheckInfo}</div>
<div class="habit-card-lives">${livesHtml}</div>
<div class="habit-card-completion">${completionRate}% (30d)</div>
<div class="habit-card-footer">
<span class="habit-card-category">${escapeHtml(habit.category || 'General')}</span>
<span class="habit-card-priority">
<span class="priority-indicator priority-${getPriorityLevel(habit.priority || 3)}"></span>
P${habit.priority || 3}
</span>
</div>
</div>
`;
}
// Check if habit was checked today
function isCheckedToday(habit) {
if (!habit.completions || habit.completions.length === 0) {
return false;
}
const today = new Date().toISOString().split('T')[0];
return habit.completions.some(c => c.date === today);
}
// Get last check-in info text
function getLastCheckInfo(habit) {
if (!habit.completions || habit.completions.length === 0) {
return 'Last: Never';
}
const lastCompletion = habit.completions[habit.completions.length - 1];
const lastDate = new Date(lastCompletion.date);
const today = new Date();
today.setHours(0, 0, 0, 0);
lastDate.setHours(0, 0, 0, 0);
const diffDays = Math.floor((today - lastDate) / (1000 * 60 * 60 * 24));
if (diffDays === 0) {
return 'Last: Today';
} else if (diffDays === 1) {
return 'Last: Yesterday';
} else {
return `Last: ${diffDays} days ago`;
}
}
// Render lives as hearts
function renderLives(lives) {
const totalLives = 3;
let html = '';
for (let i = 0; i < totalLives; i++) {
html += i < lives ? '❤️' : '🖤';
}
return html;
}
// Get priority level string
function getPriorityLevel(priority) {
if (priority === 1) return 'high';
if (priority === 2) return 'medium';
return 'low';
}
// Modal state
let selectedColor = '#3B82F6';
let selectedIcon = 'dumbbell';
// Preset colors
const presetColors = [
'#EF4444', '#F97316', '#F59E0B', '#10B981',
'#3B82F6', '#8B5CF6', '#EC4899', '#6B7280'
];
// Common icons
const commonIcons = [
'dumbbell', 'moon', 'book', 'brain', 'heart', 'flame',
'star', 'target', 'trophy', 'coffee', 'music', 'camera',
'zap', 'sun', 'droplet', 'leaf', 'feather', 'pencil',
'smile', 'watch', 'footprints', 'activity', 'battery', 'headphones',
'utensils', 'apple', 'pizza', 'glass-water', 'pill', 'stethoscope',
'briefcase', 'laptop', 'smartphone', 'mail', 'calendar', 'clock'
];
// Show add habit modal
function showAddHabitModal() {
const modal = document.getElementById('habitModal');
const form = document.getElementById('habitForm');
const modalTitle = document.querySelector('.modal-title');
const submitBtnText = document.getElementById('submitBtnText');
// Reset editing state
editingHabitId = null;
// Reset modal title and button text to create mode
modalTitle.textContent = 'Add Habit';
submitBtnText.textContent = 'Create Habit';
// Reset form
form.reset();
selectedColor = '#3B82F6';
selectedIcon = 'dumbbell';
// Initialize color picker
initColorPicker();
// Initialize icon picker
initIconPicker();
// Update frequency params for initial selection
updateFrequencyParams();
// Show modal
modal.classList.add('active');
lucide.createIcons();
}
// Close habit modal
function closeHabitModal() {
const modal = document.getElementById('habitModal');
modal.classList.remove('active');
// Reset editing state
editingHabitId = null;
}
// Close modal when clicking outside
document.addEventListener('click', (e) => {
const modal = document.getElementById('habitModal');
if (e.target === modal) {
closeHabitModal();
}
});
// Initialize color picker
function initColorPicker() {
const swatchesContainer = document.getElementById('colorSwatches');
swatchesContainer.innerHTML = presetColors.map(color =>
`<div class="color-swatch ${color === selectedColor ? 'selected' : ''}"
style="background-color: ${color}"
onclick="selectColor('${color}')"></div>`
).join('');
// Handle custom color input
const customColorInput = document.getElementById('customColor');
customColorInput.addEventListener('input', (e) => {
const value = e.target.value;
if (/^#[0-9A-Fa-f]{6}$/.test(value)) {
selectColor(value);
}
});
}
// Select color
function selectColor(color) {
selectedColor = color;
// Update swatches
const swatches = document.querySelectorAll('.color-swatch');
swatches.forEach(swatch => {
if (swatch.style.backgroundColor === color || rgbToHex(swatch.style.backgroundColor) === color) {
swatch.classList.add('selected');
} else {
swatch.classList.remove('selected');
}
});
// Update custom color input if not a preset
if (!presetColors.includes(color)) {
document.getElementById('customColor').value = color;
}
}
// Convert RGB to Hex
function rgbToHex(rgb) {
const match = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
if (!match) return rgb;
return '#' + [1, 2, 3].map(i => {
const hex = parseInt(match[i]).toString(16);
return hex.length === 1 ? '0' + hex : hex;
}).join('').toUpperCase();
}
// Initialize icon picker
function initIconPicker() {
const iconPickerContainer = document.getElementById('iconPicker');
iconPickerContainer.innerHTML = commonIcons.map(icon =>
`<div class="icon-option ${icon === selectedIcon ? 'selected' : ''}"
onclick="selectIcon('${icon}')">
<i data-lucide="${icon}"></i>
</div>`
).join('');
lucide.createIcons();
}
// Select icon
function selectIcon(icon) {
selectedIcon = icon;
// Update icon options
const iconOptions = document.querySelectorAll('.icon-option');
iconOptions.forEach((option, index) => {
if (commonIcons[index] === icon) {
option.classList.add('selected');
} else {
option.classList.remove('selected');
}
});
}
// Update frequency params based on selected type
function updateFrequencyParams() {
const frequencyType = document.getElementById('frequencyType').value;
const paramsContainer = document.getElementById('frequencyParams');
let html = '';
if (frequencyType === 'specific_days') {
html = `
<label class="form-label">Select Days</label>
<div class="day-checkboxes">
${['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'].map((day, index) => `
<label class="day-checkbox-label">
<input type="checkbox" name="day" value="${index}" checked>
<span>${day}</span>
</label>
`).join('')}
</div>
`;
} else if (frequencyType === 'x_per_week') {
html = `
<label class="form-label" for="xPerWeek">Times per week</label>
<input type="number" id="xPerWeek" class="form-input" min="1" max="7" value="3">
`;
} else if (frequencyType === 'custom') {
html = `
<label class="form-label" for="customInterval">Interval (days)</label>
<input type="number" id="customInterval" class="form-input" min="1" value="7">
`;
}
paramsContainer.innerHTML = html;
}
// Submit habit form
async function submitHabitForm(event) {
event.preventDefault();
const submitBtn = document.getElementById('submitHabitBtn');
const submitBtnText = document.getElementById('submitBtnText');
// Determine if we're creating or editing
const isEditing = editingHabitId !== null;
// Get form values
const name = document.getElementById('habitName').value.trim();
const category = document.getElementById('habitCategory').value;
const priority = parseInt(document.getElementById('habitPriority').value);
const notes = document.getElementById('habitNotes').value.trim();
const frequencyType = document.getElementById('frequencyType').value;
const reminderTime = document.getElementById('reminderTime').value;
// Validate name
if (!name) {
showToast('Please enter a habit name', 'error');
return;
}
if (name.length > 100) {
showToast('Habit name must be 100 characters or less', 'error');
return;
}
// Validate color
if (!/^#[0-9A-Fa-f]{6}$/.test(selectedColor)) {
showToast('Invalid color format', 'error');
return;
}
// Build frequency params
let frequencyParams = {};
if (frequencyType === 'specific_days') {
const checkedDays = Array.from(document.querySelectorAll('input[name="day"]:checked'))
.map(cb => parseInt(cb.value));
frequencyParams = { days: checkedDays };
} else if (frequencyType === 'x_per_week') {
const xPerWeek = parseInt(document.getElementById('xPerWeek').value);
frequencyParams = { count: xPerWeek };
} else if (frequencyType === 'custom') {
const interval = parseInt(document.getElementById('customInterval').value);
frequencyParams = { interval };
}
// Build habit object
const habitData = {
name,
category,
color: selectedColor,
icon: selectedIcon,
priority,
notes: notes || undefined,
frequency: {
type: frequencyType,
...frequencyParams
},
reminderTime: reminderTime || undefined
};
// Show loading state
submitBtn.disabled = true;
submitBtnText.textContent = isEditing ? 'Saving...' : 'Creating...';
try {
const url = isEditing ? `/echo/api/habits/${editingHabitId}` : '/echo/api/habits';
const method = isEditing ? 'PUT' : 'POST';
const response = await fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(habitData)
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || `HTTP ${response.status}`);
}
// Success
showToast(isEditing ? 'Habit updated!' : 'Habit created successfully!', 'success');
closeHabitModal();
await loadHabits();
} catch (error) {
console.error(`Failed to ${isEditing ? 'update' : 'create'} habit:`, error);
showToast(`Failed to ${isEditing ? 'update' : 'create'} habit: ` + error.message, 'error');
} finally {
submitBtn.disabled = false;
submitBtnText.textContent = isEditing ? 'Save Changes' : 'Create Habit';
}
}
// Show toast notification
function showToast(message, type = 'success') {
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.innerHTML = `
<i data-lucide="${type === 'success' ? 'check-circle' : 'alert-circle'}"></i>
<span>${escapeHtml(message)}</span>
`;
document.body.appendChild(toast);
lucide.createIcons();
setTimeout(() => {
toast.remove();
}, 3000);
}
// Show edit habit modal
let editingHabitId = null; // Track which habit we're editing
function showEditHabitModal(habitId) {
const habit = habits.find(h => h.id === habitId);
if (!habit) {
showToast('Habit not found', 'error');
return;
}
editingHabitId = habitId;
const modal = document.getElementById('habitModal');
const form = document.getElementById('habitForm');
const modalTitle = document.querySelector('.modal-title');
const submitBtn = document.getElementById('submitHabitBtn');
const submitBtnText = document.getElementById('submitBtnText');
// Change modal title and button text
modalTitle.textContent = 'Edit Habit';
submitBtnText.textContent = 'Save Changes';
// Pre-populate form fields
document.getElementById('habitName').value = habit.name;
document.getElementById('habitCategory').value = habit.category || 'health';
document.getElementById('habitPriority').value = habit.priority || 3;
document.getElementById('habitNotes').value = habit.notes || '';
document.getElementById('frequencyType').value = habit.frequency?.type || 'daily';
document.getElementById('reminderTime').value = habit.reminderTime || '';
// Set selected color and icon
selectedColor = habit.color || '#3B82F6';
selectedIcon = habit.icon || 'dumbbell';
// Initialize color picker with current selection
initColorPicker();
// Initialize icon picker with current selection
initIconPicker();
// Update frequency params and pre-populate
updateFrequencyParams();
// Pre-populate frequency params based on type
const frequencyType = habit.frequency?.type;
if (frequencyType === 'specific_days' && habit.frequency.days) {
const dayCheckboxes = document.querySelectorAll('input[name="day"]');
dayCheckboxes.forEach(cb => {
cb.checked = habit.frequency.days.includes(parseInt(cb.value));
});
} else if (frequencyType === 'x_per_week' && habit.frequency.count) {
const xPerWeekInput = document.getElementById('xPerWeek');
if (xPerWeekInput) {
xPerWeekInput.value = habit.frequency.count;
}
} else if (frequencyType === 'custom' && habit.frequency.interval) {
const customIntervalInput = document.getElementById('customInterval');
if (customIntervalInput) {
customIntervalInput.value = habit.frequency.interval;
}
}
// Show modal
modal.classList.add('active');
lucide.createIcons();
}
// Delete habit (placeholder)
async function deleteHabit(habitId) {
if (!confirm('Are you sure you want to delete this habit?')) {
return;
}
alert('Delete functionality - coming in next story!');
}
// Check in habit (placeholder)
async function checkInHabit(habitId) {
alert('Check-in functionality - coming in next story!');
}
// Show error message
function showError(message) {
const container = document.getElementById('habitsContainer');
container.innerHTML = `
<div class="empty-state">
<i data-lucide="alert-circle"></i>
<p style="color: var(--error)">${escapeHtml(message)}</p>
</div>
`;
lucide.createIcons();
}
// Escape HTML to prevent XSS
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Initialize page
lucide.createIcons();
loadHabits();
</script>
</body>
</html>