forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReminderForm.js
More file actions
119 lines (104 loc) · 3.82 KB
/
Copy pathReminderForm.js
File metadata and controls
119 lines (104 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React, { useState } from 'react';
import './ReminderForm.css';
const TYPES = [
{ value: 'watering', label: '💧 Irrigazione' },
{ value: 'fertilizing', label: '🧪 Fertilizzazione' },
{ value: 'harvesting', label: '🌾 Raccolto' },
{ value: 'pruning', label: '✂️ Potatura' },
];
const FREQUENCIES = [
{ value: 'daily', label: 'Giornaliero' },
{ value: 'every_2_days', label: 'Ogni 2 giorni' },
{ value: 'every_3_days', label: 'Ogni 3 giorni' },
{ value: 'weekly', label: 'Settimanale' },
{ value: 'biweekly', label: 'Bisettimanale' },
{ value: 'monthly', label: 'Mensile' },
{ value: 'custom', label: 'Personalizzato' },
];
const CHANNELS = [
{ value: 'push', label: '🔔 Push' },
{ value: 'email', label: '📧 Email' },
{ value: 'telegram', label: '✈️ Telegram' },
];
const ReminderForm = ({ gardenId, ownerId, onCreated }) => {
const [form, setForm] = useState({
type: 'watering', frequency: 'weekly', customIntervalDays: '',
channel: 'push', notes: '',
});
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState('');
const handleChange = (e) => {
setForm(f => ({ ...f, [e.target.name]: e.target.value }));
};
const handleSubmit = async (e) => {
e.preventDefault();
setSubmitting(true);
setError('');
try {
const body = {
gardenId, ownerId,
type: form.type,
frequency: form.frequency,
customIntervalDays: form.frequency === 'custom' ? parseInt(form.customIntervalDays) || 7 : undefined,
channel: form.channel,
notes: form.notes,
};
const res = await fetch('/api/reminders', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const data = await res.json();
if (data.success) {
setForm({ type: 'watering', frequency: 'weekly', customIntervalDays: '', channel: 'push', notes: '' });
onCreated?.();
} else {
setError(data.message || 'Errore creazione');
}
} catch (err) {
setError('Errore di rete: ' + err.message);
} finally {
setSubmitting(false);
}
};
return (
<form className="reminder-form" onSubmit={handleSubmit}>
<h3>📅 Nuovo Promemoria</h3>
<div className="form-row">
<label>Tipo</label>
<select name="type" value={form.type} onChange={handleChange}>
{TYPES.map(t => <option key={t.value} value={t.value}>{t.label}</option>)}
</select>
</div>
<div className="form-row">
<label>Frequenza</label>
<select name="frequency" value={form.frequency} onChange={handleChange}>
{FREQUENCIES.map(f => <option key={f.value} value={f.value}>{f.label}</option>)}
</select>
</div>
{form.frequency === 'custom' && (
<div className="form-row">
<label>Intervallo (giorni)</label>
<input type="number" name="customIntervalDays" min="1" max="365"
value={form.customIntervalDays} onChange={handleChange} placeholder="es. 5" />
</div>
)}
<div className="form-row">
<label>Canale notifica</label>
<select name="channel" value={form.channel} onChange={handleChange}>
{CHANNELS.map(c => <option key={c.value} value={c.value}>{c.label}</option>)}
</select>
</div>
<div className="form-row">
<label>Note</label>
<textarea name="notes" value={form.notes} onChange={handleChange}
placeholder="es. Usa fertilizzante organico..." rows="2" maxLength="500" />
</div>
{error && <div className="form-error">{error}</div>}
<button type="submit" disabled={submitting} className="form-submit">
{submitting ? 'Creazione...' : '✨ Crea Promemoria'}
</button>
</form>
);
};
export default ReminderForm;