refactor: streamline date and title handling in NewWeatherPage and NewWeeklyCheckInPage components for improved user experience
Some checks failed
Deploy with Docker Compose / deploy (push) Has been cancelled

This commit is contained in:
Julien Froidefond
2026-02-04 11:02:52 +01:00
parent ef0772f894
commit e8ffccd286
4 changed files with 68 additions and 57 deletions

View File

@@ -1,6 +1,6 @@
'use client';
import { useState, useEffect } from 'react';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import {
Card,
@@ -19,16 +19,15 @@ export default function NewWeatherPage() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [selectedDate, setSelectedDate] = useState(new Date().toISOString().split('T')[0]);
const [title, setTitle] = useState('');
const [title, setTitle] = useState(() => getWeekYearLabel(new Date(new Date().toISOString().split('T')[0])));
const [isTitleManuallyEdited, setIsTitleManuallyEdited] = useState(false);
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
setError(null);
setLoading(true);
const formData = new FormData(e.currentTarget);
const dateStr = formData.get('date') as string;
const date = dateStr ? new Date(dateStr) : undefined;
const date = selectedDate ? new Date(selectedDate) : undefined;
if (!title) {
setError('Veuillez remplir le titre');
@@ -47,13 +46,19 @@ export default function NewWeatherPage() {
router.push(`/weather/${result.data?.id}`);
}
// Default date to today
const today = new Date().toISOString().split('T')[0];
function handleDateChange(e: React.ChangeEvent<HTMLInputElement>) {
const newDate = e.target.value;
setSelectedDate(newDate);
// Only update title if user hasn't manually modified it
if (!isTitleManuallyEdited) {
setTitle(getWeekYearLabel(new Date(newDate)));
}
}
// Update title when date changes
useEffect(() => {
setTitle(getWeekYearLabel(new Date(selectedDate)));
}, [selectedDate]);
function handleTitleChange(e: React.ChangeEvent<HTMLInputElement>) {
setTitle(e.target.value);
setIsTitleManuallyEdited(true);
}
return (
<main className="mx-auto max-w-2xl px-4 py-8">
@@ -81,7 +86,7 @@ export default function NewWeatherPage() {
name="title"
placeholder="Ex: Météo S05 - 2026"
value={title}
onChange={(e) => setTitle(e.target.value)}
onChange={handleTitleChange}
required
/>
@@ -93,8 +98,8 @@ export default function NewWeatherPage() {
id="date"
name="date"
type="date"
defaultValue={today}
onChange={(e) => setSelectedDate(e.target.value)}
value={selectedDate}
onChange={handleDateChange}
required
className="w-full rounded-lg border border-border bg-input px-3 py-2 text-foreground outline-none focus:border-primary focus:ring-2 focus:ring-primary/20"
/>