forked from mongodb/docs-sample-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchEditMovieForm.tsx
More file actions
363 lines (331 loc) · 11.4 KB
/
BatchEditMovieForm.tsx
File metadata and controls
363 lines (331 loc) · 11.4 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
'use client';
/**
* Batch Edit Movie Form Component
*
* Form for editing multiple movies at once with validation
*/
import { useState } from 'react';
import { Movie } from '../../types/movie';
import styles from '../EditMovieForm/EditMovieForm.module.css';
interface BatchEditMovieFormProps {
selectedCount: number;
onSave: (updateData: Partial<Movie>) => void;
onCancel: () => void;
isLoading?: boolean;
}
export default function BatchEditMovieForm({
selectedCount,
onSave,
onCancel,
isLoading = false
}: BatchEditMovieFormProps) {
const [formData, setFormData] = useState({
title: '',
year: '',
plot: '',
runtime: '',
rated: '',
genres: '',
directors: '',
writers: '',
cast: '',
countries: '',
languages: '',
poster: '',
});
const [errors, setErrors] = useState<Record<string, string>>({});
const validateForm = () => {
const newErrors: Record<string, string> = {};
// For batch updates, we only validate if a field has a value and if
// that value must meet certain criteria.
// Empty fields will be ignored in the update
if (formData.year && (parseInt(formData.year) < 1800 || parseInt(formData.year) > new Date().getFullYear() + 5)) {
newErrors.year = 'Please enter a valid year';
}
if (formData.runtime && (parseInt(formData.runtime) < 1 || parseInt(formData.runtime) > 1000)) {
newErrors.runtime = 'Please enter a valid runtime in minutes';
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (!validateForm()) {
return;
}
// Only include fields that have values
const updateData: Partial<Movie> = {};
if (formData.title.trim()) {
updateData.title = formData.title.trim();
}
if (formData.year) {
updateData.year = parseInt(formData.year);
}
if (formData.plot.trim()) {
updateData.plot = formData.plot.trim();
}
if (formData.runtime) {
updateData.runtime = parseInt(formData.runtime);
}
if (formData.rated.trim()) {
updateData.rated = formData.rated.trim();
}
if (formData.genres.trim()) {
updateData.genres = formData.genres.split(',').map(g => g.trim()).filter(g => g);
}
if (formData.directors.trim()) {
updateData.directors = formData.directors.split(',').map(d => d.trim()).filter(d => d);
}
if (formData.writers.trim()) {
updateData.writers = formData.writers.split(',').map(w => w.trim()).filter(w => w);
}
if (formData.cast.trim()) {
updateData.cast = formData.cast.split(',').map(c => c.trim()).filter(c => c);
}
if (formData.countries.trim()) {
updateData.countries = formData.countries.split(',').map(c => c.trim()).filter(c => c);
}
if (formData.languages.trim()) {
updateData.languages = formData.languages.split(',').map(l => l.trim()).filter(l => l);
}
if (formData.poster.trim()) {
updateData.poster = formData.poster.trim();
}
// Check if user entered anything for the batch update
if (Object.keys(updateData).length === 0) {
setErrors({ general: 'Please fill in at least one field to update' });
return;
}
onSave(updateData);
};
const handleInputChange = (field: string, value: string) => {
setFormData(prev => ({ ...prev, [field]: value }));
// Clear error when user starts typing
if (errors[field]) {
setErrors(prev => ({ ...prev, [field]: '' }));
}
// Clear general error
if (errors.general) {
setErrors(prev => ({ ...prev, general: '' }));
}
};
return (
<div className={styles.formContainer}>
<h2 className={styles.formTitle}>Batch Edit {selectedCount} Movies</h2>
<p className={styles.batchDescription}>
Only fill in the fields you want to update. Empty fields will be left unchanged on all selected movies.
</p>
{errors.general && (
<div className={styles.generalError}>
{errors.general}
</div>
)}
<form onSubmit={handleSubmit} className={styles.form}>
<div className={styles.formGrid}>
{/* Title */}
<div className={styles.formGroup}>
<label htmlFor="title" className={styles.label}>
Title
</label>
<input
type="text"
id="title"
value={formData.title}
onChange={(e) => handleInputChange('title', e.target.value)}
className={`${styles.input} ${errors.title ? styles.inputError : ''}`}
disabled={isLoading}
placeholder="Leave empty to keep existing titles"
/>
{errors.title && <span className={styles.error}>{errors.title}</span>}
</div>
{/* Year */}
<div className={styles.formGroup}>
<label htmlFor="year" className={styles.label}>
Year
</label>
<input
type="number"
id="year"
value={formData.year}
onChange={(e) => handleInputChange('year', e.target.value)}
className={`${styles.input} ${errors.year ? styles.inputError : ''}`}
disabled={isLoading}
min="1800"
max={new Date().getFullYear() + 5}
placeholder="Leave empty to keep existing years"
/>
{errors.year && <span className={styles.error}>{errors.year}</span>}
</div>
{/* Runtime */}
<div className={styles.formGroup}>
<label htmlFor="runtime" className={styles.label}>
Runtime (minutes)
</label>
<input
type="number"
id="runtime"
value={formData.runtime}
onChange={(e) => handleInputChange('runtime', e.target.value)}
className={`${styles.input} ${errors.runtime ? styles.inputError : ''}`}
disabled={isLoading}
min="1"
max="1000"
placeholder="Leave empty to keep existing runtimes"
/>
{errors.runtime && <span className={styles.error}>{errors.runtime}</span>}
</div>
{/* Rated */}
<div className={styles.formGroup}>
<label htmlFor="rated" className={styles.label}>
Rating
</label>
<input
type="text"
id="rated"
value={formData.rated}
onChange={(e) => handleInputChange('rated', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="PG-13"
/>
</div>
{/* Poster URL */}
<div className={styles.formGroup}>
<label htmlFor="poster" className={styles.label}>
Poster URL
</label>
<input
type="url"
id="poster"
value={formData.poster}
onChange={(e) => handleInputChange('poster', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="https://..."
/>
</div>
</div>
{/* Plot */}
<div className={styles.formGroup}>
<label htmlFor="plot" className={styles.label}>
Plot
</label>
<textarea
id="plot"
value={formData.plot}
onChange={(e) => handleInputChange('plot', e.target.value)}
className={styles.textarea}
disabled={isLoading}
rows={4}
placeholder="Leave empty to keep existing plots"
/>
</div>
{/* Lists (comma-separated) */}
<div className={styles.listFields}>
<div className={styles.formGroup}>
<label htmlFor="genres" className={styles.label}>
Genres (comma-separated)
</label>
<input
type="text"
id="genres"
value={formData.genres}
onChange={(e) => handleInputChange('genres', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="e.g. Action, Drama, Comedy"
/>
</div>
<div className={styles.formGroup}>
<label htmlFor="directors" className={styles.label}>
Directors (comma-separated)
</label>
<input
type="text"
id="directors"
value={formData.directors}
onChange={(e) => handleInputChange('directors', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="e.g. Director 1, Director 2"
/>
</div>
<div className={styles.formGroup}>
<label htmlFor="writers" className={styles.label}>
Writers (comma-separated)
</label>
<input
type="text"
id="writers"
value={formData.writers}
onChange={(e) => handleInputChange('writers', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="e.g. Writer 1, Writer 2"
/>
</div>
<div className={styles.formGroup}>
<label htmlFor="cast" className={styles.label}>
Cast (comma-separated)
</label>
<input
type="text"
id="cast"
value={formData.cast}
onChange={(e) => handleInputChange('cast', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="e.g. Actor 1, Actor 2, Actor 3"
/>
</div>
<div className={styles.formGroup}>
<label htmlFor="countries" className={styles.label}>
Countries (comma-separated)
</label>
<input
type="text"
id="countries"
value={formData.countries}
onChange={(e) => handleInputChange('countries', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="e.g. USA, UK, France"
/>
</div>
<div className={styles.formGroup}>
<label htmlFor="languages" className={styles.label}>
Languages (comma-separated)
</label>
<input
type="text"
id="languages"
value={formData.languages}
onChange={(e) => handleInputChange('languages', e.target.value)}
className={styles.input}
disabled={isLoading}
placeholder="e.g. English, Spanish, French"
/>
</div>
</div>
{/* Form Actions */}
<div className={styles.formActions}>
<button
type="button"
onClick={onCancel}
className={`${styles.button} ${styles.cancelButton}`}
disabled={isLoading}
>
Cancel
</button>
<button
type="submit"
className={`${styles.button} ${styles.saveButton}`}
disabled={isLoading}
>
{isLoading ? 'Updating...' : `Update ${selectedCount} Movies`}
</button>
</div>
</form>
</div>
);
}