forked from mongodb/docs-sample-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
760 lines (666 loc) · 26.7 KB
/
page.tsx
File metadata and controls
760 lines (666 loc) · 26.7 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
'use client';
import { useState, useEffect } from 'react';
import { useSearchParams, useRouter } from 'next/navigation';
import pageStyles from "./page.module.css";
import movieStyles from "./movies.module.css";
import { MovieCard, Pagination, PageSizeSelector, AddMovieForm, BatchEditMovieForm, SearchMovieModal } from "../components";
import { ErrorDisplay, LoadingSpinner } from "../components/ui";
import { fetchMovies, createMovie, createMoviesBatch, deleteMoviesBatch, updateMoviesBatch, searchMovies, vectorSearchMovies } from "../lib/api";
import { Movie } from "../types/movie";
import { APP_CONFIG, ROUTES } from "../lib/constants";
import type { SearchParams } from "../components/SearchMovieModal";
/**
* Movies Page Component
*
* Main page for browsing movies with the following features:
* - Regular movie browsing with URL-based pagination
* - MongoDB text search with server-side pagination
* - Vector search with client-side pagination
* - CRUD operations (create, update, delete movies)
* - Batch operations on selected movies
*
*/
export default function Movies() {
const searchParams = useSearchParams();
const router = useRouter();
const [movies, setMovies] = useState<Movie[]>([]);
const [hasNextPage, setHasNextPage] = useState(false);
const [hasPrevPage, setHasPrevPage] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [isCreating, setIsCreating] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [isUpdating, setIsUpdating] = useState(false);
const [isSearching, setIsSearching] = useState(false);
const [showAddForm, setShowAddForm] = useState(false);
const [showBatchEditForm, setShowBatchEditForm] = useState(false);
const [showSearchModal, setShowSearchModal] = useState(false);
const [selectedMovies, setSelectedMovies] = useState<Set<string>>(new Set());
const [showDeleteConfirmation, setShowDeleteConfirmation] = useState(false);
const [error, setError] = useState<string | null>(null);
const [successMessage, setSuccessMessage] = useState<string | null>(null);
const [searchResults, setSearchResults] = useState<Movie[]>([]);
const [allVectorSearchResults, setAllVectorSearchResults] = useState<Movie[]>([]);
const [vectorSearchPage, setVectorSearchPage] = useState(1);
const [vectorSearchPageSize, setVectorSearchPageSize] = useState(20);
const [searchHasNextPage, setSearchHasNextPage] = useState(false);
const [searchHasPrevPage, setSearchHasPrevPage] = useState(false);
const [searchTotalCount, setSearchTotalCount] = useState(0);
const [isSearchMode, setIsSearchMode] = useState(false);
const [searchPage, setSearchPage] = useState(1);
const [searchLimit, setSearchLimit] = useState(20);
const [currentSearchParams, setCurrentSearchParams] = useState<SearchParams | null>(null);
const page = parseInt(searchParams.get('page') || '1');
const limit = Math.min(
parseInt(searchParams.get('limit') || APP_CONFIG.defaultMovieLimit.toString()),
APP_CONFIG.maxMovieLimit
);
const skip = (page - 1) * limit;
const loadMovies = async () => {
setIsLoading(true);
setError(null);
try {
const result = await fetchMovies(limit, skip);
setMovies(result.movies);
setHasNextPage(result.hasNextPage);
setHasPrevPage(result.hasPrevPage);
} catch (err) {
setError('Failed to load movies. Make sure the server is running on port 3001.');
setMovies([]);
}
setIsLoading(false);
};
useEffect(() => {
loadMovies();
}, [page, limit]);
const handleAddMovie = () => {
setShowAddForm(true);
setError(null);
setSuccessMessage(null);
};
const handleCancelAdd = () => {
setShowAddForm(false);
setError(null);
setSuccessMessage(null);
};
const handleSaveMovie = async (moviesData: Omit<Movie, '_id'>[]) => {
setIsCreating(true);
setError(null);
setSuccessMessage(null);
// Choose between single and batch creation based on number of movies
if (moviesData.length === 1) {
// Single movie creation
const result = await createMovie(moviesData[0]);
if (result.success) {
setSuccessMessage('Movie created successfully!');
setShowAddForm(false);
// Redirect to the new movie's page after a brief delay
setTimeout(() => {
router.push(ROUTES.movie(result.movieId!));
}, 2000);
} else {
setError(result.error || 'Failed to create movie');
}
} else {
// Batch movie creation
const result = await createMoviesBatch(moviesData);
if (result.success) {
setSuccessMessage(`Successfully created ${result.insertedCount} movies!`);
setShowAddForm(false);
// Refresh the movies list after batch creation
setTimeout(() => {
loadMovies();
}, 2000);
} else {
setError(result.error || 'Failed to create movies');
}
}
setIsCreating(false);
};
const handleMovieSelection = (movieId: string, isSelected: boolean) => {
setSelectedMovies(prev => {
const newSelection = new Set(prev);
if (isSelected) {
newSelection.add(movieId);
} else {
newSelection.delete(movieId);
}
return newSelection;
});
};
const handleBatchDelete = () => {
if (selectedMovies.size > 0) {
setShowDeleteConfirmation(true);
}
};
const handleBatchUpdate = () => {
if (selectedMovies.size > 0) {
setShowBatchEditForm(true);
setError(null);
setSuccessMessage(null);
}
};
const handleCancelBatchEdit = () => {
setShowBatchEditForm(false);
setError(null);
setSuccessMessage(null);
};
const handleSaveBatchEdit = async (updateData: Partial<Movie>) => {
setIsUpdating(true);
setError(null);
setSuccessMessage(null);
const movieIds = Array.from(selectedMovies);
const result = await updateMoviesBatch(movieIds, updateData);
if (result.success) {
setSuccessMessage(`Successfully updated ${result.modifiedCount} out of ${result.matchedCount} movies!`);
setShowBatchEditForm(false);
setSelectedMovies(new Set()); // Clear selection
// Refresh the movies list
setTimeout(() => {
loadMovies();
}, 1500);
} else {
setError(result.error || 'Failed to update movies');
}
setIsUpdating(false);
};
const confirmBatchDelete = async () => {
setIsDeleting(true);
setError(null);
setSuccessMessage(null);
setShowDeleteConfirmation(false);
const movieIds = Array.from(selectedMovies);
const result = await deleteMoviesBatch(movieIds);
if (result.success) {
setSuccessMessage(`Successfully deleted ${result.deletedCount} movies!`);
setSelectedMovies(new Set()); // Clear selection
// Refresh the movies list
setTimeout(() => {
loadMovies();
}, 1500);
} else {
setError(result.error || 'Failed to delete movies');
}
setIsDeleting(false);
};
const cancelBatchDelete = () => {
setShowDeleteConfirmation(false);
};
const handleSearch = () => {
setShowSearchModal(true);
setError(null);
setSuccessMessage(null);
};
const handleCancelSearch = () => {
setShowSearchModal(false);
setError(null);
setSuccessMessage(null);
};
const handleSearchSubmit = async (searchParams: SearchParams) => {
setIsSearching(true);
setError(null);
setSuccessMessage(null);
try {
let result;
if (searchParams.searchType === 'vector-search') {
// Vector Search: Use the limit from search params as the fetch limit
const vectorSearchParams = {
q: searchParams.q!,
limit: searchParams.limit || 50, // This is how many results to fetch from backend
};
result = await vectorSearchMovies(vectorSearchParams);
if (result.success) {
const allResults = result.movies || [];
const pageSize = APP_CONFIG.vectorSearchPageSize; // Fixed page size for UI display
setAllVectorSearchResults(allResults);
setVectorSearchPage(1);
setVectorSearchPageSize(pageSize);
// Calculate first page of results for immediate display
const firstPageResults = allResults.slice(0, pageSize);
setSearchResults(firstPageResults);
// Set pagination state for vector search
setSearchTotalCount(allResults.length);
}
} else {
// MongoDB Search
const searchSkip = 0; // Always start from page 1 for new searches
const searchLimitToUse = searchParams.limit || 20;
const searchParamsWithPagination = {
...searchParams,
limit: searchLimitToUse,
skip: searchSkip,
};
result = await searchMovies(searchParamsWithPagination);
if (result.success) {
setSearchResults(result.movies || []);
setSearchHasNextPage(result.hasNextPage || false);
setSearchHasPrevPage(result.hasPrevPage || false);
setSearchTotalCount(result.totalCount || 0);
}
}
if (result.success) {
setIsSearchMode(true);
setSearchPage(1);
setSearchLimit(searchParams.limit || 20);
setCurrentSearchParams(searchParams);
setShowSearchModal(false);
setSelectedMovies(new Set()); // Clear selection when switching to search mode
if (searchParams.searchType === 'vector-search') {
const returnedCount = result.movies?.length || 0;
if (returnedCount === 0) {
setSuccessMessage('Vector search completed, but no movies matched your query. Try different search terms.');
} else {
setSuccessMessage(`Found ${returnedCount} results using vector search.`);
}
} else {
// MongoDB text search success message
const totalCount = (result as any).totalCount || 0;
if (totalCount === 0) {
setSuccessMessage('Search completed, but no movies matched your criteria. Try different search terms.');
} else {
setSuccessMessage(`Found ${totalCount} results using MongoDB search.`);
}
}
} else {
setError(result.error || 'Failed to search movies');
}
} catch (err) {
setError('An unexpected error occurred while searching');
}
setIsSearching(false);
};
/**
* Clears search mode and returns to regular movie browsing
* Resets all search-related state including vector search pagination
*/
const handleClearSearch = () => {
setIsSearchMode(false);
setSearchResults([]);
setAllVectorSearchResults([]);
setVectorSearchPage(1);
setSearchHasNextPage(false);
setSearchHasPrevPage(false);
setSearchTotalCount(0);
setSearchPage(1);
setCurrentSearchParams(null);
setSelectedMovies(new Set());
setError(null);
setSuccessMessage(null);
// The current movies will show the regular paginated results
};
// Get the movies to display based on current mode
const displayMovies = isSearchMode ? searchResults : movies;
/**
* Helper function for vector search client-side pagination
* Calculates pagination data for the current vector search results
*/
const getVectorSearchPageData = () => {
if (!isSearchMode || currentSearchParams?.searchType !== 'vector-search') {
return { paginatedResults: [], hasNext: false, hasPrev: false, totalPages: 0 };
}
const totalResults = allVectorSearchResults.length;
const totalPages = Math.ceil(totalResults / vectorSearchPageSize);
const hasNext = vectorSearchPage < totalPages;
const hasPrev = vectorSearchPage > 1;
const startIndex = (vectorSearchPage - 1) * vectorSearchPageSize;
const endIndex = startIndex + vectorSearchPageSize;
const paginatedResults = allVectorSearchResults.slice(startIndex, endIndex);
return {
paginatedResults,
hasNext,
hasPrev,
totalPages
};
};
/**
* Handles page navigation for vector search results (client-side pagination)
* Slices the cached results and updates the display
*/
const handleVectorSearchPageChange = (newPage: number) => {
if (!isSearchMode || currentSearchParams?.searchType !== 'vector-search') return;
const totalPages = Math.ceil(allVectorSearchResults.length / vectorSearchPageSize);
if (newPage < 1 || newPage > totalPages) return;
setVectorSearchPage(newPage);
// Update the displayed results based on the new page
const startIndex = (newPage - 1) * vectorSearchPageSize;
const endIndex = startIndex + vectorSearchPageSize;
const paginatedResults = allVectorSearchResults.slice(startIndex, endIndex);
setSearchResults(paginatedResults);
setSearchHasNextPage(newPage < totalPages);
setSearchHasPrevPage(newPage > 1);
// Clear selection and scroll to top for better UX
setSelectedMovies(new Set());
window.scrollTo({ top: 0, behavior: 'smooth' });
};
const handleSearchPageChange = async (newPage: number) => {
if (!currentSearchParams) return;
// This function handles MongoDB search pagination (server-side)
// Vector search uses handleVectorSearchPageChange for client-side pagination
if (currentSearchParams.searchType === 'vector-search') {
return;
}
// Validate page number and prevent invalid navigation
if (newPage < 1) return;
if (isSearching) return;
if (newPage > searchPage && !searchHasNextPage) return;
setIsSearching(true);
setError(null);
const searchSkip = (newPage - 1) * searchLimit;
const searchParamsWithPagination = {
...currentSearchParams,
limit: searchLimit,
skip: searchSkip,
};
try {
const result = await searchMovies(searchParamsWithPagination);
if (result.success) {
setSearchResults(result.movies || []);
setSearchHasNextPage(result.hasNextPage || false);
setSearchHasPrevPage(result.hasPrevPage || false);
setSearchTotalCount(result.totalCount || 0);
setSearchPage(newPage);
// Clear any previously selected movies when changing pages
setSelectedMovies(new Set());
// Scroll to top to show new results
window.scrollTo({ top: 0, behavior: 'smooth' });
} else {
setError(result.error || 'Failed to load search results');
}
} catch (error) {
console.error('Search pagination error:', error);
setError('Failed to load search results');
}
setIsSearching(false);
};
if (isLoading && !showAddForm) {
return (
<div className={pageStyles.page}>
<main className={pageStyles.main}>
<div style={{ textAlign: 'center', padding: '2rem' }}>
<LoadingSpinner />
<p>Loading movies...</p>
</div>
</main>
</div>
);
}
return (
<div className={pageStyles.page}>
<main className={pageStyles.main}>
<div className={movieStyles.pageHeader}>
<h1 className={movieStyles.pageTitle}>
{isSearchMode ? `Search Results` : 'Movies'}
</h1>
<div className={movieStyles.headerActions}>
{/* Search and Clear Search Controls */}
{!showAddForm && !showBatchEditForm && !showSearchModal && (
<div className={movieStyles.searchControls}>
{isSearchMode && (
<button
onClick={handleClearSearch}
className={movieStyles.clearSearchButton}
type="button"
>
← Back to All Movies
</button>
)}
<button
onClick={handleSearch}
disabled={isSearching}
className={movieStyles.searchButton}
type="button"
>
{isSearching ? 'Searching...' : 'Search Movies'}
</button>
</div>
)}
{/* Batch Selection Controls */}
{!showAddForm && !showBatchEditForm && !showSearchModal && displayMovies.length > 0 && (
<div className={movieStyles.selectionControls}>
{selectedMovies.size > 0 && (
<>
<button
onClick={handleBatchUpdate}
disabled={isUpdating}
className={movieStyles.batchUpdateButton}
type="button"
>
{isUpdating ? 'Updating...' : `Update ${selectedMovies.size} Selected`}
</button>
<button
onClick={handleBatchDelete}
disabled={isDeleting}
className={movieStyles.batchDeleteButton}
type="button"
>
{isDeleting ? 'Deleting...' : `Delete ${selectedMovies.size} Selected`}
</button>
</>
)}
</div>
)}
{!isSearchMode && (
<button
onClick={handleAddMovie}
disabled={showAddForm || showBatchEditForm || showSearchModal || isCreating}
className={movieStyles.addButton}
type="button"
>
{isCreating ? 'Creating...' : '+ Add Movie'}
</button>
)}
</div>
</div>
{/* Success/Error Messages */}
{successMessage && (
<div className={movieStyles.successMessage}>
{successMessage}
</div>
)}
{error && (
<div className={movieStyles.errorMessage}>
{error}
</div>
)}
{/* Add Movie Form */}
{showAddForm && (
<AddMovieForm
onSave={handleSaveMovie}
onCancel={handleCancelAdd}
isLoading={isCreating}
/>
)}
{/* Search Movie Modal */}
{showSearchModal && (
<SearchMovieModal
onSearch={handleSearchSubmit}
onCancel={handleCancelSearch}
isLoading={isSearching}
/>
)}
{/* Batch Edit Movie Form */}
{showBatchEditForm && (
<BatchEditMovieForm
selectedCount={selectedMovies.size}
onSave={handleSaveBatchEdit}
onCancel={handleCancelBatchEdit}
isLoading={isUpdating}
/>
)}
{/* Page Size Selector - only show for regular mode */}
{!showAddForm && !showBatchEditForm && !showSearchModal && !isSearchMode && <PageSizeSelector currentLimit={limit} />}
{/* Movies Content */}
{!showAddForm && !showBatchEditForm && !showSearchModal && (
<>
{error && displayMovies.length === 0 ? (
<ErrorDisplay
message={error}
onRetry={isSearchMode ? () => handleSearchSubmit(currentSearchParams!) : loadMovies}
/>
) : displayMovies.length === 0 ? (
<div className={movieStyles.noMovies}>
<p>
{isSearchMode
? 'No movies found matching your search criteria. Try different search terms.'
: 'No movies found. Make sure the server is running on port 3001.'
}
</p>
</div>
) : (
<>
<div className={movieStyles.moviesGrid}>
{displayMovies.map((movie) => (
<MovieCard
key={movie._id}
movie={movie}
isSelected={selectedMovies.has(movie._id)}
onSelectionChange={handleMovieSelection}
showCheckbox={!showAddForm && !showBatchEditForm && !showSearchModal}
/>
))}
</div>
{/* Show pagination based on current mode */}
{isSearchMode ? (
currentSearchParams?.searchType === 'mongodb-search' ? (
<nav className={movieStyles.pagination} aria-label="Search results pagination">
<div className={movieStyles.paginationContainer}>
{/* Previous Button */}
{searchHasPrevPage && !isSearching ? (
<button
onClick={() => handleSearchPageChange(searchPage - 1)}
className={movieStyles.pageButton}
disabled={isSearching}
aria-label="Go to previous page"
>
← Previous
</button>
) : (
<span className={`${movieStyles.pageButton} ${movieStyles.disabled}`}>
← Previous
</span>
)}
{/* Current Page Info */}
<div className={movieStyles.pageInfo}>
Page {searchPage} {searchTotalCount > 0 ? `of ${Math.ceil(searchTotalCount / searchLimit)}` : ''}
</div>
{/* Next Button */}
{searchHasNextPage && !isSearching ? (
<button
onClick={() => handleSearchPageChange(searchPage + 1)}
className={movieStyles.pageButton}
disabled={isSearching}
aria-label="Go to next page"
>
Next →
</button>
) : (
<span className={`${movieStyles.pageButton} ${movieStyles.disabled}`}>
Next →
</span>
)}
</div>
{/* Additional Info */}
<div className={movieStyles.additionalInfo}>
{searchLimit} movies per page • {searchTotalCount} total results
</div>
</nav>
) : (
/* Vector search results with client-side pagination */
(() => {
const { hasNext, hasPrev, totalPages } = getVectorSearchPageData();
if (totalPages > 1) {
return (
<nav className={movieStyles.pagination} aria-label="Vector search results pagination">
<div className={movieStyles.paginationContainer}>
{/* Previous Button */}
{hasPrev ? (
<button
onClick={() => handleVectorSearchPageChange(vectorSearchPage - 1)}
className={movieStyles.pageButton}
aria-label="Go to previous page"
>
← Previous
</button>
) : (
<span className={`${movieStyles.pageButton} ${movieStyles.disabled}`}>
← Previous
</span>
)}
{/* Current Page Info */}
<div className={movieStyles.pageInfo}>
Page {vectorSearchPage} of {totalPages}
</div>
{/* Next Button */}
{hasNext ? (
<button
onClick={() => handleVectorSearchPageChange(vectorSearchPage + 1)}
className={movieStyles.pageButton}
aria-label="Go to next page"
>
Next →
</button>
) : (
<span className={`${movieStyles.pageButton} ${movieStyles.disabled}`}>
Next →
</span>
)}
</div>
{/* Additional Info */}
<div className={movieStyles.additionalInfo}>
{vectorSearchPageSize} movies per page • {allVectorSearchResults.length} total results
</div>
</nav>
);
} else {
return (
<div className={movieStyles.searchInfo}>
Showing {allVectorSearchResults.length} results (vector search)
</div>
);
}
})()
)
) : (
<Pagination
currentPage={page}
hasNextPage={hasNextPage}
hasPrevPage={hasPrevPage}
limit={limit}
/>
)}
</>
)}
</>
)}
{/* Batch Delete Confirmation Dialog */}
{showDeleteConfirmation && (
<div className={movieStyles.confirmationOverlay}>
<div className={movieStyles.confirmationDialog}>
<h3 className={movieStyles.confirmationTitle}>Confirm Batch Delete</h3>
<p className={movieStyles.confirmationMessage}>
Are you sure you want to delete {selectedMovies.size} selected movie{selectedMovies.size !== 1 ? 's' : ''}?
This action cannot be undone.
</p>
<div className={movieStyles.confirmationActions}>
<button
onClick={cancelBatchDelete}
className={movieStyles.cancelButton}
type="button"
>
Cancel
</button>
<button
onClick={confirmBatchDelete}
className={movieStyles.confirmDeleteButton}
type="button"
disabled={isDeleting}
>
{isDeleting ? 'Deleting...' : 'Delete'}
</button>
</div>
</div>
</div>
)}
</main>
</div>
);
}