- Rename application from "Worker Presence Tracker" to "Worker Presence Manager" in UI texts and metadata.

- Add settings management feature to dynamically update total points and sprint days.
- Refactor forms and modals for better user feedback, validation, and usability.
This commit is contained in:
Mathieu 2025-11-19 13:34:08 +01:00
parent 279292a984
commit 4ffc736fea
5 changed files with 289 additions and 59 deletions

View file

@ -1,4 +1,4 @@
# Worker Presence Tracker
# Worker Presence Manager
A Next.js application for tracking worker presence with a point system.

View file

@ -2,7 +2,7 @@ import type { Metadata } from "next";
import "./globals.css";
export const metadata: Metadata = {
title: "Worker Presence Tracker",
title: "Worker Presence Manager",
description: "Track worker presence with point system",
};

View file

@ -1,22 +1,30 @@
'use client';
import { useState, useEffect } from 'react';
import { workersAPI, Worker } from '@/lib/api';
import { workersAPI, Worker, settingsAPI, Settings } from '@/lib/api';
export default function Home() {
const [workers, setWorkers] = useState<Worker[]>([]);
const [newWorkerName, setNewWorkerName] = useState('');
const [newPresenceDays, setNewPresenceDays] = useState('');
const [nameError, setNameError] = useState(false);
const [daysError, setDaysError] = useState(false);
const [editingId, setEditingId] = useState<string | null>(null);
const [editingDays, setEditingDays] = useState('');
const [loading, setLoading] = useState(false);
const [settings, setSettings] = useState<Settings>({ id: 1, totalPoints: 13, totalDays: 10 });
const [showSettings, setShowSettings] = useState(false);
const [showResetConfirm, setShowResetConfirm] = useState(false);
const [editTotalPoints, setEditTotalPoints] = useState('');
const [editTotalDays, setEditTotalDays] = useState('');
const TOTAL_POINTS = 13;
const TOTAL_DAYS = 10;
const TOTAL_POINTS = settings.totalPoints;
const TOTAL_DAYS = settings.totalDays;
const POINTS_PER_DAY = TOTAL_POINTS / TOTAL_DAYS;
useEffect(() => {
loadWorkers();
loadSettings();
}, []);
const loadWorkers = async () => {
@ -29,12 +37,68 @@ export default function Home() {
}
};
const loadSettings = async () => {
try {
const data = await settingsAPI.get();
setSettings(data);
} catch (error) {
console.error('Failed to load settings:', error);
}
};
const openSettings = () => {
setEditTotalPoints(settings.totalPoints.toString());
setEditTotalDays(settings.totalDays.toString());
setShowSettings(true);
};
const closeSettings = () => {
setShowSettings(false);
setEditTotalPoints('');
setEditTotalDays('');
};
const saveSettings = async () => {
const points = parseInt(editTotalPoints);
const days = parseInt(editTotalDays);
if (isNaN(points) || points <= 0) {
alert('Please enter a valid number of points greater than 0');
return;
}
if (isNaN(days) || days <= 0) {
alert('Please enter a valid number of days greater than 0');
return;
}
try {
setLoading(true);
const updatedSettings = await settingsAPI.update({ totalPoints: points, totalDays: days });
setSettings(updatedSettings);
closeSettings();
} catch (error) {
console.error('Failed to save settings:', error);
alert('Failed to save settings');
} finally {
setLoading(false);
}
};
const addWorker = async () => {
if (!newWorkerName.trim() || !newPresenceDays.trim()) return;
// Validate required fields
const isNameEmpty = !newWorkerName.trim();
const isDaysEmpty = !newPresenceDays.trim();
setNameError(isNameEmpty);
setDaysError(isDaysEmpty);
if (isNameEmpty || isDaysEmpty) return;
const days = parseInt(newPresenceDays);
if (isNaN(days) || days < 0 || days > TOTAL_DAYS) {
alert(`Please enter a valid number of days between 0 and ${TOTAL_DAYS}`);
setDaysError(true);
return;
}
@ -47,6 +111,8 @@ export default function Home() {
await loadWorkers();
setNewWorkerName('');
setNewPresenceDays('');
setNameError(false);
setDaysError(false);
} catch (error) {
console.error('Failed to add worker:', error);
alert('Failed to add worker');
@ -99,13 +165,10 @@ export default function Home() {
}
};
const resetAllPresences = async () => {
if (!confirm('Are you sure you want to reset all worker presences to 0?')) {
return;
}
const confirmResetAllPresences = async () => {
try {
setLoading(true);
setShowResetConfirm(false);
await workersAPI.resetAllPresences();
await loadWorkers();
} catch (error) {
@ -129,20 +192,18 @@ export default function Home() {
<div className="max-w-4xl mx-auto">
<div className="flex justify-between items-start mb-8">
<div>
<h1 className="text-4xl font-bold text-gray-800 mb-2">Worker Presence Tracker</h1>
<h1 className="text-4xl font-bold text-gray-800 mb-2">Worker Presence Manager</h1>
<p className="text-gray-600">
Sprint duration: 2 weeks (10 working days) | Total points per worker: {TOTAL_POINTS}
Sprint duration: {TOTAL_DAYS} working days | Total points per worker: {TOTAL_POINTS}
</p>
</div>
{workers.length > 0 && (
<button
onClick={resetAllPresences}
disabled={loading}
className="px-4 py-2 bg-orange-600 text-white rounded-lg hover:bg-orange-700 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
>
Reset All Presences
</button>
)}
<button
onClick={openSettings}
disabled={loading}
className="px-4 py-2 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
>
Settings
</button>
</div>
{/* Add Worker Form */}
@ -153,18 +214,32 @@ export default function Home() {
type="text"
placeholder="Worker name"
value={newWorkerName}
onChange={(e) => setNewWorkerName(e.target.value)}
className="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
onChange={(e) => {
setNewWorkerName(e.target.value);
if (nameError) setNameError(false);
}}
className={`flex-1 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 ${
nameError
? 'border-red-500 focus:ring-red-500'
: 'border-gray-300 focus:ring-blue-500'
}`}
onKeyPress={(e) => e.key === 'Enter' && addWorker()}
/>
<input
type="number"
placeholder="Presence days (0-10)"
placeholder={`Presence days (0-${TOTAL_DAYS})`}
value={newPresenceDays}
onChange={(e) => setNewPresenceDays(e.target.value)}
onChange={(e) => {
setNewPresenceDays(e.target.value);
if (daysError) setDaysError(false);
}}
min="0"
max="10"
className="w-full sm:w-48 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
max={TOTAL_DAYS}
className={`w-full sm:w-48 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 ${
daysError
? 'border-red-500 focus:ring-red-500'
: 'border-gray-300 focus:ring-blue-500'
}`}
onKeyPress={(e) => e.key === 'Enter' && addWorker()}
/>
<button
@ -223,7 +298,7 @@ export default function Home() {
value={editingDays}
onChange={(e) => setEditingDays(e.target.value)}
min="0"
max="10"
max={TOTAL_DAYS}
className="w-20 px-2 py-1 border border-blue-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500"
onKeyPress={(e) => {
if (e.key === 'Enter') saveEditing(worker.id);
@ -264,15 +339,21 @@ export default function Home() {
<div className="flex gap-2 justify-end">
<button
onClick={() => startEditing(worker)}
className="text-blue-600 hover:text-blue-900 transition-colors"
className="p-2 text-blue-600 hover:text-blue-900 hover:bg-blue-50 rounded transition-colors"
title="Edit"
>
Edit
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-5 h-5">
<path strokeLinecap="round" strokeLinejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
</button>
<button
onClick={() => removeWorker(worker.id)}
className="text-red-600 hover:text-red-900 transition-colors"
className="p-2 text-red-600 hover:text-red-900 hover:bg-red-50 rounded transition-colors"
title="Remove"
>
Remove
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-5 h-5">
<path strokeLinecap="round" strokeLinejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
</button>
</div>
)}
@ -286,31 +367,43 @@ export default function Home() {
{/* Summary */}
<div className="bg-gray-50 px-6 py-4 border-t border-gray-200">
<div className="flex flex-wrap gap-6 text-sm">
<div>
<span className="text-gray-600">Total Workers: </span>
<span className="font-semibold text-gray-900">{workers.length}</span>
</div>
<div>
<span className="text-gray-600">Avg Presence Days: </span>
<span className="font-semibold text-gray-900">
{workers.length > 0
? (workers.reduce((sum, w) => sum + w.presenceDays, 0) / workers.length).toFixed(1)
: '0'}
</span>
</div>
<div>
<span className="text-gray-600">Total Presence Days: </span>
<span className="font-semibold text-gray-900">
{workers.reduce((sum, w) => sum + w.presenceDays, 0)}
</span>
</div>
<div>
<span className="text-gray-600">Total Remaining Points: </span>
<span className="font-semibold text-blue-600">
{workers.reduce((sum, w) => sum + calculatePoints(w.presenceDays), 0).toFixed(2)}
</span>
<div className="flex flex-wrap items-center justify-between gap-4">
<div className="flex flex-wrap gap-6 text-sm">
<div>
<span className="text-gray-600">Total Workers: </span>
<span className="font-semibold text-gray-900">{workers.length}</span>
</div>
<div>
<span className="text-gray-600">Avg Presence Days: </span>
<span className="font-semibold text-gray-900">
{workers.length > 0
? (workers.reduce((sum, w) => sum + w.presenceDays, 0) / workers.length).toFixed(1)
: '0'}
</span>
</div>
<div>
<span className="text-gray-600">Total Presence Days: </span>
<span className="font-semibold text-gray-900">
{workers.reduce((sum, w) => sum + w.presenceDays, 0)}
</span>
</div>
</div>
<div>
<span className="text-gray-600">Total Points in the sprint: </span>
<span className="font-semibold text-blue-600">
{workers.reduce((sum, w) => sum + calculatePoints(w.presenceDays), 0).toFixed(2)}
</span>
</div>
<button
onClick={() => setShowResetConfirm(true)}
disabled={loading}
className="p-2 bg-orange-600 text-white rounded-lg hover:bg-orange-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
title="Reset All Presences"
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-5 h-5">
<path strokeLinecap="round" strokeLinejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99" />
</svg>
</button>
</div>
</div>
</div>
@ -324,13 +417,121 @@ export default function Home() {
<div className="mt-8 bg-blue-50 border border-blue-200 rounded-lg p-4">
<h3 className="font-semibold text-blue-900 mb-2">How it works:</h3>
<ul className="text-sm text-blue-800 space-y-1">
<li> Each worker has {TOTAL_POINTS} points for a 2-week sprint ({TOTAL_DAYS} working days)</li>
<li> Each worker has {TOTAL_POINTS} points for a sprint ({TOTAL_DAYS} working days)</li>
<li> Each presence day equals {POINTS_PER_DAY.toFixed(2)} points</li>
<li> Points Earned = Presence Days × {POINTS_PER_DAY.toFixed(2)}</li>
<li> Points Used = {TOTAL_POINTS} - Points Earned (days not present)</li>
<li> Remaining Points = Points Earned</li>
</ul>
</div>
{/* Settings Modal */}
{showSettings && (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4 z-50">
<div className="bg-white rounded-lg shadow-xl max-w-md w-full p-6">
<h2 className="text-2xl font-bold text-gray-800 mb-4">Settings</h2>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Points per Worker per Sprint
</label>
<input
type="number"
value={editTotalPoints}
onChange={(e) => setEditTotalPoints(e.target.value)}
min="1"
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="e.g., 13"
/>
<p className="mt-1 text-sm text-gray-500">
Total points allocated to each worker for the sprint
</p>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Number of Days in Sprint
</label>
<input
type="number"
value={editTotalDays}
onChange={(e) => setEditTotalDays(e.target.value)}
min="1"
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="e.g., 10"
/>
<p className="mt-1 text-sm text-gray-500">
Total working days in the sprint
</p>
</div>
<div className="bg-gray-50 rounded p-3 text-sm text-gray-600">
<p>
<strong>Points per day:</strong>{' '}
{editTotalPoints && editTotalDays && parseInt(editTotalPoints) > 0 && parseInt(editTotalDays) > 0
? (parseInt(editTotalPoints) / parseInt(editTotalDays)).toFixed(2)
: 'N/A'}
</p>
</div>
</div>
<div className="flex gap-3 mt-6">
<button
onClick={saveSettings}
disabled={loading}
className="flex-1 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
>
Save
</button>
<button
onClick={closeSettings}
disabled={loading}
className="flex-1 px-4 py-2 bg-gray-200 text-gray-800 rounded-lg hover:bg-gray-300 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
>
Cancel
</button>
</div>
</div>
</div>
)}
{/* Reset Confirmation Modal */}
{showResetConfirm && (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4 z-50">
<div className="bg-white rounded-lg shadow-xl max-w-md w-full p-6">
<div className="flex items-center gap-3 mb-4">
<div className="p-3 bg-orange-100 rounded-full">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6 text-orange-600">
<path strokeLinecap="round" strokeLinejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" />
</svg>
</div>
<h2 className="text-xl font-bold text-gray-800">Reset All Presences</h2>
</div>
<p className="text-gray-600 mb-6">
Are you sure you want to reset all worker presences to 0? This action cannot be undone.
</p>
<div className="flex gap-3">
<button
onClick={() => setShowResetConfirm(false)}
disabled={loading}
className="flex-1 px-4 py-2 bg-gray-200 text-gray-800 rounded-lg hover:bg-gray-300 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
>
Cancel
</button>
<button
onClick={confirmResetAllPresences}
disabled={loading}
className="flex-1 px-4 py-2 bg-orange-600 text-white rounded-lg hover:bg-orange-700 transition-colors font-medium disabled:opacity-50 disabled:cursor-not-allowed"
>
Reset All
</button>
</div>
</div>
</div>
)}
</div>
</main>
);

View file

@ -5,5 +5,10 @@
"presenceDays": 0,
"id": 1
}
]
],
"settings": {
"id": 1,
"totalPoints": 13,
"totalDays": 10
}
}

View file

@ -8,6 +8,12 @@ export interface Worker {
presenceDays: number;
}
export interface Settings {
id: number;
totalPoints: number;
totalDays: number;
}
export const workersAPI = {
async getAll(): Promise<Worker[]> {
const response = await fetch(`${API_URL}/workers`);
@ -51,3 +57,21 @@ export const workersAPI = {
);
},
};
export const settingsAPI = {
async get(): Promise<Settings> {
const response = await fetch(`${API_URL}/settings`);
if (!response.ok) throw new Error('Failed to fetch settings');
return response.json();
},
async update(settings: Partial<Settings>): Promise<Settings> {
const response = await fetch(`${API_URL}/settings`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(settings),
});
if (!response.ok) throw new Error('Failed to update settings');
return response.json();
},
};