diff --git a/app/Http/Controllers/Customer/HomeController.php b/app/Http/Controllers/Customer/HomeController.php
index ae3b66a..a54187d 100644
--- a/app/Http/Controllers/Customer/HomeController.php
+++ b/app/Http/Controllers/Customer/HomeController.php
@@ -6,19 +6,30 @@ use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Info;
use App\Models\Location;
+use App\Models\Voucher;
+use Illuminate\Http\Request;
class HomeController extends Controller
{
- public function index()
+ public function index(Request $request)
{
$infos = Info::where('is_publish', 1)->orderBy('updated_at', 'desc')->get();
$banners = Banner::orderBy('updated_at', 'desc')->get();
$locations = Location::get();
+ $vouchers = Voucher::with(['location'])
+ ->groupBy('batch_id')
+ ->orderBy('updated_at', 'desc');
+
+ if ($request->location_id != '') {
+ $vouchers->where('location_id', $request->location_id);
+ }
return inertia('Home/Index/Index', [
'infos' => $infos,
'banners' => $banners,
- 'locations' => $locations
+ 'locations' => $locations,
+ 'vouchers' => $vouchers->paginate(10),
+ '_location_id' => $request->location_id
]);
}
diff --git a/app/Http/Controllers/VoucherController.php b/app/Http/Controllers/VoucherController.php
index 3dd4885..abd38b2 100644
--- a/app/Http/Controllers/VoucherController.php
+++ b/app/Http/Controllers/VoucherController.php
@@ -6,6 +6,7 @@ use App\Models\Voucher;
use App\Services\GeneralService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Str;
class VoucherController extends Controller
{
@@ -131,6 +132,7 @@ class VoucherController extends Controller
'expired_unit' => 'required|string',
]);
+ $batchId = Str::ulid();
$vouchers = GeneralService::script_parser($request->script);
DB::beginTransaction();
@@ -146,6 +148,7 @@ class VoucherController extends Controller
'comment' => $voucher['comment'],
'expired' => $request->expired,
'expired_unit' => $request->expired_unit,
+ 'batch_id' => $batchId,
]);
}
DB::commit();
diff --git a/app/Models/Voucher.php b/app/Models/Voucher.php
index 767e9ed..096412d 100644
--- a/app/Models/Voucher.php
+++ b/app/Models/Voucher.php
@@ -27,7 +27,7 @@ class Voucher extends Model
'batch_id',
];
- protected $appends = ['display_quota'];
+ protected $appends = ['display_quota', 'display_expired'];
protected static function booted(): void
{
@@ -62,6 +62,13 @@ class Voucher extends Model
});
}
+ public function displayExpired(): Attribute
+ {
+ return Attribute::make(get: function () {
+ return $this->expired . ' ' . $this->expired_unit;
+ });
+ }
+
public function location()
{
return $this->belongsTo(Location::class);
diff --git a/resources/js/Pages/Home/Index/Index.jsx b/resources/js/Pages/Home/Index/Index.jsx
index ea53d34..669dd12 100644
--- a/resources/js/Pages/Home/Index/Index.jsx
+++ b/resources/js/Pages/Home/Index/Index.jsx
@@ -1,9 +1,10 @@
-import React from 'react'
+import React, { useEffect, useState } from 'react'
import { Head, router, usePage } from '@inertiajs/react'
import CustomerLayout from '@/Layouts/CustomerLayout'
import { HiOutlineBell } from 'react-icons/hi2'
import UserBanner from './UserBanner'
+import VoucherCard from './VoucherCard'
const GuestBanner = () => {
const {
@@ -29,11 +30,60 @@ const GuestBanner = () => {
)
}
-export default function Index({ auth: { user }, infos, banners, locations }) {
+export default function Index({
+ auth: { user },
+ infos,
+ banners,
+ locations,
+ vouchers: { data, next_page_url },
+ _location_id,
+}) {
+ const [locId, setLocId] = useState(_location_id)
+ const [v, setV] = useState(data)
+
const handleBanner = (banner) => {
router.get(route('home.banner', banner))
}
+ const handleSelectLoc = (loc) => {
+ if (loc.id === locId) {
+ setLocId('')
+ return
+ }
+ setLocId(loc.id)
+ }
+
+ const loadMore = () => {
+ router.get(
+ next_page_url,
+ {
+ location_id: locId,
+ },
+ {
+ replace: true,
+ preserveState: true,
+ only: ['vouchers'],
+ onSuccess: (res) => {
+ setV(v.concat(res.props.vouchers.data))
+ },
+ }
+ )
+ }
+
+ useEffect(() => {
+ router.get(
+ route(route().current()),
+ { location_id: locId },
+ {
+ replace: true,
+ preserveState: true,
+ onSuccess: (res) => {
+ setV(res.props.vouchers.data)
+ },
+ }
+ )
+ }, [locId])
+
return (