Browse Source

merge with latest change

master
azri 4 months ago
parent
commit
6e81c817be

+ 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', 'totalPages']));
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
 }

+ 55
- 3
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 ">
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 ">
8 14
         <a onclick="guestbook.showModal()" class="rounded-full border-solid border-2 border-navbarcolor py-1 px-3 font-medium text-gray-600">
@@ -20,3 +26,49 @@
20 26
     <hr class="border-t-1 border-gray-300 my-5 w-4/5 self-center ">
21 27
     @include('modals.guestbook-modal')
22 28
 </div>
29
+
30
+@push('script')
31
+<script>
32
+    $(document).ready(function() {
33
+        var autoSwitchInterval = 4500; // 3 seconds
34
+        var currentPage = 1;
35
+        var totalPages = {{ $totalPages }}; // Total number of pages
36
+
37
+        function fetchGuestbook(page) {
38
+            $.ajax({
39
+                url: '{{ route('guestbook.ajax') }}',
40
+                type: 'GET',
41
+                data: { page: page },
42
+                success: function(response) {
43
+                    $('#guestbook-container').html(response.guestbook);
44
+
45
+                    // Update pagination buttons
46
+                    $('#pagination-buttons .join-item').removeClass('btn-active');
47
+                    $('#pagination-buttons .join-item[data-page="' + page + '"]').addClass('btn-active');
48
+
49
+                    // Update currentPage
50
+                    currentPage = page;
51
+                },
52
+                error: function(xhr) {
53
+                    console.log(xhr.responseText);
54
+                }
55
+            });
56
+        }
57
+
58
+        // Auto-switching every 3 seconds
59
+        setInterval(function() {
60
+            var nextPage = currentPage + 1;
61
+            if (nextPage > totalPages) {
62
+                nextPage = 1;
63
+            }
64
+            fetchGuestbook(nextPage);
65
+        }, autoSwitchInterval);
66
+
67
+        // Handle pagination button click
68
+        $('#pagination-buttons').on('click', '.join-item', function() {
69
+            var page = $(this).data('page');
70
+            fetchGuestbook(page);
71
+        });
72
+    });
73
+</script>
74
+@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