Browse Source

guestbook display

master
farhan 5 months ago
parent
commit
309b4a1576

+ 15
- 3
app/Http/Controllers/MainController.php View File

@@ -10,15 +10,16 @@ class MainController extends Controller
10 10
 {
11 11
     public function main(Request $request) {
12 12
         $rsvp = RSVP::all();
13
-        $guestbook = GuestBook::all();
13
+        $guestbooks = GuestBook::orderBy('created_at', 'desc')->take(5)->get();
14
+        $totalPages = $guestbooks->count();
14 15
 
15 16
         $host = $request->getHost();
16 17
         if ($host == 'aliffayuna.com') {
17
-            return view('digital-card', compact(['rsvp', 'guestbook']));
18
+            return view('digital-card', compact(['rsvp', 'guestbooks']));
18 19
         } else if ($host == 'rsvp.aliffayuna.com') {
19 20
             return view('rsvp-digital-card');
20 21
         } else {
21
-            return view('digital-card', compact(['rsvp', 'guestbook']));
22
+            return view('digital-card', compact(['rsvp', 'guestbooks', 'totalPages']));
22 23
             // return view('rsvp-digital-card');
23 24
         }
24 25
     }
@@ -45,4 +46,15 @@ class MainController extends Controller
45 46
 
46 47
         return response()->json(['message' => 'Terima kasih atas ucapan anda!']);
47 48
     }
49
+
50
+    public function fetchGuestbook(Request $request)
51
+    {
52
+        $page = $request->input('page', 1); // Default to page 1 if no page is provided
53
+        $guestbook = GuestBook::orderBy('created_at', 'desc')->take(5)->get()->forPage($page, 1);
54
+
55
+        return response()->json([
56
+            'guestbook' => view('partials.guestbook', ['entry' => $guestbook->first()])->render(),
57
+        ]);
58
+    }
59
+
48 60
 }

+ 56
- 4
resources/views/components/guestbook.blade.php View File

@@ -1,8 +1,14 @@
1 1
 <div class="flex flex-col gap-5 w-4/5">
2
-    <div class="text-center pb-3 fade-element">
2
+    <div class="flex flex-col justify-center text-center gap-3 fade-element">
3 3
         <p class="font-medium text-gray-600 pb-3">Ruangan Ucapan</p>
4
-        <p class="font-medium text-sm text-gray-600 italic">Ucapan kepada kedua mempelai</p>
5
-        <p class="font-medium text-sm text-gray-600 italic">-Nama-</p>
4
+        <div id="guestbook-container">
5
+            @include('partials.guestbook', ['entry' => $guestbooks->first()])
6
+        </div>
7
+        <div class="join flex justify-center" id="pagination-buttons">
8
+            @for ($i = 1; $i <= $totalPages; $i++)
9
+                <button class="join-item btn btn-xs {{ $i == 1 ? 'btn-active' : '' }} hidden" data-page="{{ $i }}">{{ $i }}</button>
10
+            @endfor
11
+        </div>
6 12
     </div>
7 13
     <div class="text-center pb-2 fade-element">
8 14
         <a onclick="guestbook.showModal()" class="rounded-full border-solid border-2 border-navbarcolor py-1 px-3 font-medium text-gray-600">
@@ -12,4 +18,50 @@
12 18
     </div>
13 19
     <hr class="border-t-1 border-gray-300 my-5 w-4/5 self-center fade-element">
14 20
     @include('modals.guestbook-modal')
15
-</div>
21
+</div>
22
+
23
+@push('script')
24
+<script>
25
+    $(document).ready(function() {
26
+        var autoSwitchInterval = 4500; // 3 seconds
27
+        var currentPage = 1;
28
+        var totalPages = {{ $totalPages }}; // Total number of pages
29
+
30
+        function fetchGuestbook(page) {
31
+            $.ajax({
32
+                url: '{{ route('guestbook.ajax') }}',
33
+                type: 'GET',
34
+                data: { page: page },
35
+                success: function(response) {
36
+                    $('#guestbook-container').html(response.guestbook);
37
+
38
+                    // Update pagination buttons
39
+                    $('#pagination-buttons .join-item').removeClass('btn-active');
40
+                    $('#pagination-buttons .join-item[data-page="' + page + '"]').addClass('btn-active');
41
+
42
+                    // Update currentPage
43
+                    currentPage = page;
44
+                },
45
+                error: function(xhr) {
46
+                    console.log(xhr.responseText);
47
+                }
48
+            });
49
+        }
50
+
51
+        // Auto-switching every 3 seconds
52
+        setInterval(function() {
53
+            var nextPage = currentPage + 1;
54
+            if (nextPage > totalPages) {
55
+                nextPage = 1;
56
+            }
57
+            fetchGuestbook(nextPage);
58
+        }, autoSwitchInterval);
59
+
60
+        // Handle pagination button click
61
+        $('#pagination-buttons').on('click', '.join-item', function() {
62
+            var page = $(this).data('page');
63
+            fetchGuestbook(page);
64
+        });
65
+    });
66
+</script>
67
+@endpush

+ 4
- 0
resources/views/partials/guestbook.blade.php View File

@@ -0,0 +1,4 @@
1
+@if ($entry)
2
+    <p class="font-medium text-sm text-gray-600 italic fade-in-5">{{ $entry->speech }}</p>
3
+    <p class="font-medium text-sm text-gray-600 italic fade-in-5">-{{ $entry->name }}-</p>
4
+@endif

+ 2
- 1
routes/web.php View File

@@ -16,4 +16,5 @@ use App\Http\Controllers\MainController;
16 16
 
17 17
 Route::get('/', [MainController::class, 'main']);
18 18
 Route::post('/saveRsvp', [MainController::class, 'saveRsvp'])->name('rsvp.save');
19
-Route::post('/saveSpeech', [MainController::class, 'saveSpeech'])->name('speech.save');
19
+Route::post('/saveSpeech', [MainController::class, 'saveSpeech'])->name('speech.save');
20
+Route::get('/guestbook', [MainController::class, 'fetchGuestbook'])->name('guestbook.ajax');

Loading…
Cancel
Save