Separating users from tasks + fixing weird ota bug
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import OTAUpdate from "./lib/OTAUpdate.svelte";
|
||||
import Sidebar from "./lib/Sidebar.svelte";
|
||||
import TaskManager from "./lib/TaskManager.svelte";
|
||||
import UserManager from "./lib/UserManager.svelte";
|
||||
|
||||
/** @type {'loading' | 'ok' | 'error' | 'rebooting'} */
|
||||
let status = $state("loading");
|
||||
@@ -10,7 +11,7 @@
|
||||
let showRebootConfirm = $state(false);
|
||||
let isRecovering = $state(false);
|
||||
|
||||
/** @type {'dashboard' | 'tasks'} */
|
||||
/** @type {'dashboard' | 'tasks' | 'users'} */
|
||||
let currentView = $state("dashboard");
|
||||
|
||||
let systemInfo = $state({
|
||||
@@ -144,7 +145,7 @@
|
||||
<div class="w-full max-w-6xl mx-auto space-y-8">
|
||||
<!-- Header -->
|
||||
<div class="text-center">
|
||||
<h1 class="text-2xl font-bold text-accent">Calendink Provider 🚀👑🥸</h1>
|
||||
<h1 class="text-2xl font-bold text-accent">Calendink Provider 📅📅🚀🚀</h1>
|
||||
<p class="text-text-secondary text-sm">ESP32-S3 System Dashboard v{__APP_VERSION__}</p>
|
||||
|
||||
<!-- Status Badge -->
|
||||
@@ -323,6 +324,11 @@
|
||||
<div class="bg-bg-card border border-border rounded-xl p-6 shadow-xl">
|
||||
<TaskManager />
|
||||
</div>
|
||||
{:else if currentView === 'users'}
|
||||
<!-- User Management View -->
|
||||
<div class="bg-bg-card border border-border rounded-xl p-8 shadow-xl">
|
||||
<UserManager mode="manager" />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<!-- Reboot Confirmation Modal -->
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
const navItems = [
|
||||
{ id: 'dashboard', label: 'Dashboard', icon: '🏠' },
|
||||
{ id: 'tasks', label: 'Tasks', icon: '📋' },
|
||||
{ id: 'users', label: 'Users', icon: '👥' },
|
||||
];
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
<script>
|
||||
import { getUsers, addUser, removeUser } from './api.js';
|
||||
import { getUsers, addUser, removeUser, updateUser } from './api.js';
|
||||
|
||||
let { selectedUserId = $bindable(null), onUsersChanged = () => {} } = $props();
|
||||
let {
|
||||
selectedUserId = $bindable(null),
|
||||
onUsersChanged = () => {},
|
||||
mode = 'selector' // 'selector' | 'manager'
|
||||
} = $props();
|
||||
|
||||
let users = $state([]);
|
||||
let newUserName = $state('');
|
||||
@@ -9,10 +13,14 @@
|
||||
let error = $state('');
|
||||
let confirmDeleteId = $state(null);
|
||||
|
||||
// Edit state
|
||||
let editingUserId = $state(null);
|
||||
let editName = $state('');
|
||||
|
||||
async function fetchUsers() {
|
||||
try {
|
||||
users = await getUsers();
|
||||
if (users.length > 0 && !selectedUserId) {
|
||||
if (users.length > 0 && !selectedUserId && mode === 'selector') {
|
||||
selectedUserId = users[0].id;
|
||||
}
|
||||
// If selected user was deleted, select first available
|
||||
@@ -32,7 +40,7 @@
|
||||
newUserName = '';
|
||||
showAddForm = false;
|
||||
await fetchUsers();
|
||||
selectedUserId = user.id;
|
||||
if (mode === 'selector') selectedUserId = user.id;
|
||||
onUsersChanged();
|
||||
} catch (e) {
|
||||
error = e.message;
|
||||
@@ -50,12 +58,30 @@
|
||||
}
|
||||
}
|
||||
|
||||
function startEditing(user) {
|
||||
editingUserId = user.id;
|
||||
editName = user.name;
|
||||
}
|
||||
|
||||
async function handleUpdateUser() {
|
||||
if (!editName.trim()) return;
|
||||
try {
|
||||
await updateUser(editingUserId, editName.trim());
|
||||
editingUserId = null;
|
||||
await fetchUsers();
|
||||
onUsersChanged();
|
||||
} catch (e) {
|
||||
error = e.message;
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
fetchUsers();
|
||||
});
|
||||
</script>
|
||||
</script>
|
||||
|
||||
<div class="user-manager">
|
||||
<div class="user-manager mode-{mode}">
|
||||
{#if mode === 'selector'}
|
||||
<div class="user-bar">
|
||||
<div class="user-chips">
|
||||
{#each users as user}
|
||||
@@ -68,52 +94,84 @@
|
||||
onkeydown={(e) => { if (e.key === 'Enter') selectedUserId = user.id; }}
|
||||
>
|
||||
<span class="user-name">{user.name}</span>
|
||||
{#if confirmDeleteId === user.id}
|
||||
<span class="confirm-delete">
|
||||
<button class="confirm-yes" onclick={(e) => { e.stopPropagation(); handleRemoveUser(user.id); }} title="Confirm delete">✓</button>
|
||||
<button class="confirm-no" onclick={(e) => { e.stopPropagation(); confirmDeleteId = null; }} title="Cancel">✕</button>
|
||||
</span>
|
||||
{:else}
|
||||
<button
|
||||
class="remove-btn"
|
||||
onclick={(e) => { e.stopPropagation(); confirmDeleteId = user.id; }}
|
||||
title="Remove user"
|
||||
>✕</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<!-- Manager Mode -->
|
||||
<header class="manager-header">
|
||||
<h1 class="text-xl font-bold text-text-primary">User Management</h1>
|
||||
<button class="add-user-btn-large" onclick={() => showAddForm = true}>
|
||||
+ New User
|
||||
</button>
|
||||
</header>
|
||||
|
||||
{#if showAddForm}
|
||||
<form class="add-user-form" onsubmit={(e) => { e.preventDefault(); handleAddUser(); }}>
|
||||
<div class="modal-overlay">
|
||||
<form class="add-user-modal" onsubmit={(e) => { e.preventDefault(); handleAddUser(); }}>
|
||||
<h3 class="text-lg font-semibold">Add User</h3>
|
||||
<!-- svelte-ignore a11y_autofocus -->
|
||||
<input
|
||||
type="text"
|
||||
bind:value={newUserName}
|
||||
placeholder="Name..."
|
||||
class="add-user-input"
|
||||
class="modal-input"
|
||||
autofocus
|
||||
/>
|
||||
<button type="submit" class="add-user-submit" disabled={!newUserName.trim()}>+</button>
|
||||
<button type="button" class="add-user-cancel" onclick={() => { showAddForm = false; newUserName = ''; }}>✕</button>
|
||||
<div class="modal-actions">
|
||||
<button type="button" class="btn-secondary" onclick={() => { showAddForm = false; newUserName = ''; }}>Cancel</button>
|
||||
<button type="submit" class="btn-primary" disabled={!newUserName.trim()}>Add User</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="user-list">
|
||||
{#each users as user}
|
||||
<div class="user-item">
|
||||
{#if editingUserId === user.id}
|
||||
<form class="edit-row" onsubmit={(e) => { e.preventDefault(); handleUpdateUser(); }}>
|
||||
<!-- svelte-ignore a11y_autofocus -->
|
||||
<input type="text" bind:value={editName} class="edit-input" autofocus />
|
||||
<div class="edit-actions">
|
||||
<button type="submit" class="save-btn" title="Save">✓</button>
|
||||
<button type="button" class="cancel-btn" onclick={() => editingUserId = null} title="Cancel">✕</button>
|
||||
</div>
|
||||
</form>
|
||||
{:else}
|
||||
<button class="add-user-btn" onclick={() => showAddForm = true}>
|
||||
+ User
|
||||
</button>
|
||||
<div class="user-info">
|
||||
<span class="user-name-large">{user.name}</span>
|
||||
<span class="user-id">ID: {user.id}</span>
|
||||
</div>
|
||||
<div class="user-actions">
|
||||
<button class="action-btn" onclick={() => startEditing(user)} title="Rename">✏️</button>
|
||||
{#if confirmDeleteId === user.id}
|
||||
<button class="action-btn danger" onclick={() => handleRemoveUser(user.id)}>Delete!</button>
|
||||
<button class="action-btn" onclick={() => confirmDeleteId = null}>Cancel</button>
|
||||
{:else}
|
||||
<button class="action-btn" onclick={() => confirmDeleteId = user.id} title="Delete">🗑️</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="empty-state">No users found. Create one to get started!</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if error}
|
||||
<div class="user-error">{error}</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
<style>
|
||||
.user-manager {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
/* Selector Mode Styles */
|
||||
.user-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -131,144 +189,248 @@
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 6px 12px;
|
||||
padding: 6px 16px;
|
||||
border-radius: 20px;
|
||||
border: 1px solid var(--color-border);
|
||||
background: var(--color-bg-card);
|
||||
color: var(--color-text-secondary);
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
transition: all 0.15s;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
}
|
||||
|
||||
.user-chip:hover {
|
||||
border-color: var(--color-accent);
|
||||
color: var(--color-text-primary);
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.user-chip.selected {
|
||||
background: color-mix(in srgb, var(--color-accent) 15%, transparent);
|
||||
background: color-mix(in srgb, var(--color-accent) 20%, transparent);
|
||||
border-color: var(--color-accent);
|
||||
color: var(--color-accent);
|
||||
box-shadow: 0 0 0 1px var(--color-accent);
|
||||
}
|
||||
|
||||
.user-name {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.remove-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--color-text-secondary);
|
||||
cursor: pointer;
|
||||
font-size: 10px;
|
||||
padding: 0 2px;
|
||||
opacity: 0.5;
|
||||
transition: opacity 0.15s, color 0.15s;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.remove-btn:hover {
|
||||
opacity: 1;
|
||||
color: var(--color-danger);
|
||||
}
|
||||
|
||||
.confirm-delete {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.confirm-yes, .confirm-no {
|
||||
background: none;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
font-size: 11px;
|
||||
padding: 0 2px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.confirm-yes {
|
||||
color: var(--color-danger);
|
||||
}
|
||||
|
||||
.confirm-no {
|
||||
color: var(--color-text-secondary);
|
||||
}
|
||||
|
||||
.add-user-btn {
|
||||
padding: 6px 12px;
|
||||
border-radius: 20px;
|
||||
border: 1px dashed var(--color-border);
|
||||
background: transparent;
|
||||
color: var(--color-text-secondary);
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.add-user-btn:hover {
|
||||
border-color: var(--color-accent);
|
||||
color: var(--color-accent);
|
||||
}
|
||||
|
||||
.add-user-form {
|
||||
/* Manager Mode Styles */
|
||||
.manager-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.add-user-input {
|
||||
padding: 5px 10px;
|
||||
border-radius: 16px;
|
||||
border: 1px solid var(--color-border);
|
||||
background: var(--color-bg-primary);
|
||||
color: var(--color-text-primary);
|
||||
font-size: 12px;
|
||||
width: 100px;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.add-user-input:focus {
|
||||
border-color: var(--color-accent);
|
||||
}
|
||||
|
||||
.add-user-submit {
|
||||
.add-user-btn-large {
|
||||
padding: 10px 20px;
|
||||
border-radius: 12px;
|
||||
background: var(--color-accent);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
box-shadow: 0 4px 14px color-mix(in srgb, var(--color-accent) 40%, transparent);
|
||||
}
|
||||
|
||||
.add-user-btn-large:hover {
|
||||
filter: brightness(1.1);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.user-list {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.user-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 16px 20px;
|
||||
border-radius: 16px;
|
||||
background: var(--color-bg-card);
|
||||
border: 1px solid var(--color-border);
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.user-item:hover {
|
||||
border-color: var(--color-accent);
|
||||
background: var(--color-bg-card-hover);
|
||||
}
|
||||
|
||||
.user-info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.user-name-large {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: var(--color-text-primary);
|
||||
}
|
||||
|
||||
.user-id {
|
||||
font-size: 11px;
|
||||
color: var(--color-text-secondary);
|
||||
font-family: var(--font-mono);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
}
|
||||
|
||||
.user-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.action-btn {
|
||||
background: var(--color-bg-primary);
|
||||
border: 1px solid var(--color-border);
|
||||
color: var(--color-text-secondary);
|
||||
padding: 8px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.action-btn:hover {
|
||||
border-color: var(--color-accent);
|
||||
color: var(--color-accent);
|
||||
background: var(--color-bg-card);
|
||||
}
|
||||
|
||||
.action-btn.danger:hover {
|
||||
border-color: var(--color-danger);
|
||||
color: var(--color-danger);
|
||||
background: color-mix(in srgb, var(--color-danger) 10%, transparent);
|
||||
}
|
||||
|
||||
/* Modal Styles */
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
backdrop-filter: blur(4px);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
line-height: 1;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.add-user-submit:disabled {
|
||||
opacity: 0.4;
|
||||
.add-user-modal {
|
||||
background: var(--color-bg-card);
|
||||
padding: 24px;
|
||||
border-radius: 20px;
|
||||
border: 1px solid var(--color-border);
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 20px;
|
||||
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.modal-input {
|
||||
padding: 12px 16px;
|
||||
border-radius: 12px;
|
||||
border: 1px solid var(--color-border);
|
||||
background: var(--color-bg-primary);
|
||||
color: var(--color-text-primary);
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.modal-actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
/* Edit Inline Row */
|
||||
.edit-row {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.edit-input {
|
||||
flex: 1;
|
||||
padding: 8px 12px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--color-accent);
|
||||
background: var(--color-bg-primary);
|
||||
color: var(--color-text-primary);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.edit-actions {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.save-btn, .cancel-btn {
|
||||
padding: 4px 12px;
|
||||
border-radius: 6px;
|
||||
border: 1px solid var(--color-border);
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.save-btn {
|
||||
background: var(--color-success);
|
||||
color: white;
|
||||
border-color: var(--color-success);
|
||||
}
|
||||
|
||||
.cancel-btn {
|
||||
background: var(--color-bg-primary);
|
||||
color: var(--color-text-secondary);
|
||||
}
|
||||
|
||||
/* Common UI items */
|
||||
.btn-primary {
|
||||
padding: 8px 16px;
|
||||
border-radius: 8px;
|
||||
background: var(--color-accent);
|
||||
color: white;
|
||||
border: none;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
padding: 8px 16px;
|
||||
border-radius: 8px;
|
||||
background: var(--color-bg-primary);
|
||||
color: var(--color-text-secondary);
|
||||
border: 1px solid var(--color-border);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.add-user-cancel {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--color-text-secondary);
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.user-error {
|
||||
margin-top: 8px;
|
||||
padding: 6px 10px;
|
||||
border-radius: 6px;
|
||||
margin-top: 16px;
|
||||
padding: 12px;
|
||||
border-radius: 12px;
|
||||
background: color-mix(in srgb, var(--color-danger) 10%, transparent);
|
||||
border: 1px solid color-mix(in srgb, var(--color-danger) 20%, transparent);
|
||||
color: var(--color-danger);
|
||||
font-size: 11px;
|
||||
font-size: 13px;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 48px;
|
||||
background: var(--color-bg-card);
|
||||
border: 1px dashed var(--color-border);
|
||||
border-radius: 16px;
|
||||
color: var(--color-text-secondary);
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -163,6 +163,25 @@ export async function removeUser(id) {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user's name.
|
||||
* @param {number} id
|
||||
* @param {string} name
|
||||
* @returns {Promise<{status: string}>}
|
||||
*/
|
||||
export async function updateUser(id, name) {
|
||||
const res = await fetch(`${API_BASE}/api/users/update`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ id, name })
|
||||
});
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
throw new Error(`Failed (${res.status}): ${errorText || res.statusText}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
// ─── Task Management ─────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"major": 0,
|
||||
"minor": 1,
|
||||
"revision": 4
|
||||
"revision": 13
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "api/users/store.hpp"
|
||||
|
||||
// Find a task by ID, returns nullptr if not found
|
||||
internal task_t *find_task(uint16 id)
|
||||
task_t *find_task(uint16 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
@@ -19,7 +19,7 @@ internal task_t *find_task(uint16 id)
|
||||
}
|
||||
|
||||
// Add a task, returns pointer to new task or nullptr if full
|
||||
internal task_t *add_task(uint8 user_id, const char *title, int64 due_date)
|
||||
task_t *add_task(uint8 user_id, const char *title, int64 due_date)
|
||||
{
|
||||
// Verify user exists
|
||||
if (find_user(user_id) == nullptr)
|
||||
@@ -44,7 +44,7 @@ internal task_t *add_task(uint8 user_id, const char *title, int64 due_date)
|
||||
}
|
||||
|
||||
// Remove a task by ID, returns true if found and removed
|
||||
internal bool remove_task(uint16 id)
|
||||
bool remove_task(uint16 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
@@ -59,7 +59,7 @@ internal bool remove_task(uint16 id)
|
||||
}
|
||||
|
||||
// Remove all tasks belonging to a user
|
||||
internal void remove_tasks_for_user(uint8 user_id)
|
||||
void remove_tasks_for_user(uint8 user_id)
|
||||
{
|
||||
for (int i = 0; i < MAX_TASKS; i++)
|
||||
{
|
||||
@@ -73,7 +73,7 @@ internal void remove_tasks_for_user(uint8 user_id)
|
||||
|
||||
// Simple insertion sort for small arrays — sort task pointers by due_date
|
||||
// ascending
|
||||
internal void sort_tasks_by_due_date(task_t **arr, int count)
|
||||
void sort_tasks_by_due_date(task_t **arr, int count)
|
||||
{
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
@@ -90,7 +90,7 @@ internal void sort_tasks_by_due_date(task_t **arr, int count)
|
||||
|
||||
// Populate dummy tasks on boot for development iteration.
|
||||
// Uses relative offsets from current time so due dates always make sense.
|
||||
internal void seed_tasks()
|
||||
void seed_tasks()
|
||||
{
|
||||
int64 now = (int64)(esp_timer_get_time() / 1000000);
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
#include "types.hpp"
|
||||
|
||||
// Data store operations for tasks
|
||||
internal task_t *find_task(uint16 id);
|
||||
internal task_t *add_task(uint8 user_id, const char *title, int64 due_date);
|
||||
internal bool remove_task(uint16 id);
|
||||
internal void remove_tasks_for_user(uint8 user_id);
|
||||
internal void sort_tasks_by_due_date(task_t **arr, int count);
|
||||
internal void seed_tasks();
|
||||
task_t *find_task(uint16 id);
|
||||
task_t *add_task(uint8 user_id, const char *title, int64 due_date);
|
||||
bool remove_task(uint16 id);
|
||||
void remove_tasks_for_user(uint8 user_id);
|
||||
void sort_tasks_by_due_date(task_t **arr, int count);
|
||||
void seed_tasks();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "api/users/store.hpp"
|
||||
|
||||
// Find a user by ID, returns nullptr if not found
|
||||
internal user_t *find_user(uint8 id)
|
||||
user_t *find_user(uint8 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
@@ -16,7 +16,7 @@ internal user_t *find_user(uint8 id)
|
||||
}
|
||||
|
||||
// Add a user, returns pointer to new user or nullptr if full
|
||||
internal user_t *add_user(const char *name)
|
||||
user_t *add_user(const char *name)
|
||||
{
|
||||
for (int i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
@@ -32,7 +32,7 @@ internal user_t *add_user(const char *name)
|
||||
}
|
||||
|
||||
// Remove a user by ID, returns true if found and removed
|
||||
internal bool remove_user(uint8 id)
|
||||
bool remove_user(uint8 id)
|
||||
{
|
||||
for (int i = 0; i < MAX_USERS; i++)
|
||||
{
|
||||
@@ -47,8 +47,19 @@ internal bool remove_user(uint8 id)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update a user's name, returns pointer to user or nullptr if not found
|
||||
user_t *update_user(uint8 id, const char *name)
|
||||
{
|
||||
user_t *user = find_user(id);
|
||||
if (user)
|
||||
{
|
||||
strlcpy(user->name, name, sizeof(user->name));
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
// Populate dummy users on boot for development iteration
|
||||
internal void seed_users()
|
||||
void seed_users()
|
||||
{
|
||||
add_user("Alice");
|
||||
add_user("Bob");
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
#include "types.hpp"
|
||||
#include "user.hpp"
|
||||
|
||||
|
||||
// Data store operations for users
|
||||
internal user_t *find_user(uint8 id);
|
||||
internal user_t *add_user(const char *name);
|
||||
internal bool remove_user(uint8 id);
|
||||
internal void seed_users();
|
||||
user_t *find_user(uint8 id);
|
||||
user_t *add_user(const char *name);
|
||||
bool remove_user(uint8 id);
|
||||
user_t *update_user(uint8 id, const char *name);
|
||||
void seed_users();
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
#include "api/users/list.cpp"
|
||||
#include "api/users/add.cpp"
|
||||
#include "api/users/remove.cpp"
|
||||
#include "api/users/update.cpp"
|
||||
// clang-format on
|
||||
|
||||
68
Provider/main/api/users/update.cpp
Normal file
68
Provider/main/api/users/update.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
// POST /api/users/update — Update an existing user's name
|
||||
// Body: {"id": 1, "name": "Bob"}
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
#include "api/users/store.hpp"
|
||||
#include "types.hpp"
|
||||
#include "user.hpp"
|
||||
|
||||
internal esp_err_t api_users_update_handler(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
|
||||
char buf[128];
|
||||
int received = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
||||
if (received <= 0)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Empty body");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf[received] = '\0';
|
||||
|
||||
cJSON *body = cJSON_Parse(buf);
|
||||
if (!body)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *id_item = cJSON_GetObjectItem(body, "id");
|
||||
cJSON *name_item = cJSON_GetObjectItem(body, "name");
|
||||
|
||||
if (!cJSON_IsNumber(id_item) || !cJSON_IsString(name_item) ||
|
||||
strlen(name_item->valuestring) == 0)
|
||||
{
|
||||
cJSON_Delete(body);
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing 'id' or 'name'");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
user_t *user = update_user((uint8)id_item->valueint, name_item->valuestring);
|
||||
cJSON_Delete(body);
|
||||
|
||||
if (!user)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "User not found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *resp = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(resp, "status", "ok");
|
||||
|
||||
const char *json = cJSON_PrintUnformatted(resp);
|
||||
httpd_resp_sendstr(req, json);
|
||||
|
||||
free((void *)json);
|
||||
cJSON_Delete(resp);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
internal const httpd_uri_t api_users_update_uri = {.uri = "/api/users/update",
|
||||
.method = HTTP_POST,
|
||||
.handler =
|
||||
api_users_update_handler,
|
||||
.user_ctx = NULL};
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
// Shared Application State (Unity Build)
|
||||
internal bool g_Ethernet_Initialized = false;
|
||||
internal bool g_Wifi_Initialized = false;
|
||||
internal uint8_t g_Active_WWW_Partition = 0;
|
||||
// Shared Application State
|
||||
extern bool g_Ethernet_Initialized;
|
||||
extern bool g_Wifi_Initialized;
|
||||
extern uint8_t g_Active_WWW_Partition;
|
||||
|
||||
@@ -186,6 +186,7 @@ internal httpd_handle_t start_webserver(void)
|
||||
esp_vfs_littlefs_conf_t conf = {};
|
||||
conf.base_path = "/www";
|
||||
conf.partition_label = g_Active_WWW_Partition == 0 ? "www_0" : "www_1";
|
||||
ESP_LOGI(TAG, "Mounting LittleFS partition: %s", conf.partition_label);
|
||||
conf.format_if_mount_failed = false;
|
||||
conf.dont_mount = false;
|
||||
esp_err_t ret = esp_vfs_littlefs_register(&conf);
|
||||
@@ -244,6 +245,7 @@ internal httpd_handle_t start_webserver(void)
|
||||
// Register todo list API routes
|
||||
httpd_register_uri_handler(server, &api_users_get_uri);
|
||||
httpd_register_uri_handler(server, &api_users_post_uri);
|
||||
httpd_register_uri_handler(server, &api_users_update_uri);
|
||||
httpd_register_uri_handler(server, &api_users_delete_uri);
|
||||
httpd_register_uri_handler(server, &api_tasks_upcoming_uri);
|
||||
httpd_register_uri_handler(server, &api_tasks_get_uri);
|
||||
@@ -272,14 +274,13 @@ internal httpd_handle_t start_webserver(void)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
internal void stop_webserver(httpd_handle_t server)
|
||||
internal void stop_webserver(httpd_handle_t server, uint8_t partition_index)
|
||||
{
|
||||
if (server)
|
||||
{
|
||||
httpd_stop(server);
|
||||
#ifdef CONFIG_CALENDINK_DEPLOY_WEB_PAGES
|
||||
esp_vfs_littlefs_unregister(g_Active_WWW_Partition == 0 ? "www_0"
|
||||
: "www_1");
|
||||
esp_vfs_littlefs_unregister(partition_index == 0 ? "www_0" : "www_1");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// STD Lib
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// SDK
|
||||
#include "esp_log.h"
|
||||
@@ -23,7 +24,12 @@
|
||||
#include "http_server.cpp"
|
||||
// clang-format on
|
||||
|
||||
internal constexpr bool kBlockUntilEthernetEstablished = false;
|
||||
// Global Application State Definitions
|
||||
bool g_Ethernet_Initialized = false;
|
||||
bool g_Wifi_Initialized = false;
|
||||
uint8_t g_Active_WWW_Partition = 0;
|
||||
|
||||
constexpr bool kBlockUntilEthernetEstablished = false;
|
||||
|
||||
extern "C" void app_main()
|
||||
{
|
||||
@@ -46,6 +52,10 @@ extern "C" void app_main()
|
||||
{
|
||||
// Read active www partition from NVS
|
||||
err = nvs_get_u8(my_handle, "www_part", &g_Active_WWW_Partition);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
printf("NVS: Found active www partition: %d\n", g_Active_WWW_Partition);
|
||||
}
|
||||
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND)
|
||||
{
|
||||
@@ -74,6 +84,76 @@ extern "C" void app_main()
|
||||
printf("Error opening NVS handle!\n");
|
||||
}
|
||||
|
||||
// Detect if this is the first boot after a new flash (Firmware or Frontend)
|
||||
bool is_new_flash = false;
|
||||
if (nvs_open("storage", NVS_READWRITE, &my_handle) == ESP_OK)
|
||||
{
|
||||
// 1. Check Firmware Compile Time
|
||||
char last_time[64] = {0};
|
||||
size_t time_size = sizeof(last_time);
|
||||
const char *current_time = __DATE__ " " __TIME__;
|
||||
if (nvs_get_str(my_handle, "last_fw_time", last_time, &time_size) !=
|
||||
ESP_OK ||
|
||||
strcmp(last_time, current_time) != 0)
|
||||
{
|
||||
printf("New firmware detected! (Last: %s, Current: %s)\n",
|
||||
last_time[0] ? last_time : "None", current_time);
|
||||
is_new_flash = true;
|
||||
nvs_set_str(my_handle, "last_fw_time", current_time);
|
||||
}
|
||||
|
||||
// 2. Check Frontend Partition Fingerprint (www_0)
|
||||
const esp_partition_t *www0_p = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_LITTLEFS, "www_0");
|
||||
if (www0_p)
|
||||
{
|
||||
uint8_t current_sha[32];
|
||||
if (esp_partition_get_sha256(www0_p, current_sha) == ESP_OK)
|
||||
{
|
||||
uint8_t last_sha[32] = {0};
|
||||
size_t sha_size = sizeof(last_sha);
|
||||
if (nvs_get_blob(my_handle, "www0_sha", last_sha, &sha_size) !=
|
||||
ESP_OK ||
|
||||
memcmp(last_sha, current_sha, 32) != 0)
|
||||
{
|
||||
printf("New frontend partition detected via SHA256!\n");
|
||||
is_new_flash = true;
|
||||
nvs_set_blob(my_handle, "www0_sha", current_sha, 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_new_flash)
|
||||
{
|
||||
nvs_commit(my_handle);
|
||||
}
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
|
||||
// If we are running from FACTORY and a new flash was detected, override to
|
||||
// www_0
|
||||
{
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
if (running != NULL &&
|
||||
running->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY)
|
||||
{
|
||||
if (is_new_flash && g_Active_WWW_Partition != 0)
|
||||
{
|
||||
printf("FACTORY APP + NEW FLASH: Overriding www_part to 0 (was %d)\n",
|
||||
g_Active_WWW_Partition);
|
||||
g_Active_WWW_Partition = 0;
|
||||
|
||||
// Persist the override
|
||||
if (nvs_open("storage", NVS_READWRITE, &my_handle) == ESP_OK)
|
||||
{
|
||||
nvs_set_u8(my_handle, "www_part", 0);
|
||||
nvs_commit(my_handle);
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
setup_led();
|
||||
@@ -168,6 +248,9 @@ extern "C" void app_main()
|
||||
|
||||
printf("Connected!\n");
|
||||
|
||||
// Start the webserver
|
||||
web_server = start_webserver();
|
||||
|
||||
// Mark the current app as valid to cancel rollback, only if it's an OTA app
|
||||
{
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
@@ -179,9 +262,6 @@ extern "C" void app_main()
|
||||
}
|
||||
}
|
||||
|
||||
// Start the webserver
|
||||
web_server = start_webserver();
|
||||
|
||||
// Keep the main task alive indefinitely
|
||||
while (true)
|
||||
{
|
||||
@@ -193,7 +273,7 @@ shutdown:
|
||||
|
||||
if (web_server)
|
||||
{
|
||||
stop_webserver(web_server);
|
||||
stop_webserver(web_server, g_Active_WWW_Partition);
|
||||
web_server = NULL;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user