From 673f4bf57ff6aa3ff7a1dbb13b6fdd515e716cc5 Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Sat, 3 Jun 2023 01:13:26 +0700 Subject: [PATCH] voucher listing --- .../Controllers/Customer/HomeController.php | 15 ++- app/Http/Controllers/VoucherController.php | 3 + app/Models/Voucher.php | 9 +- resources/js/Pages/Home/Index/Index.jsx | 105 +++++++++++------- resources/js/Pages/Home/Index/VoucherCard.jsx | 38 +++++++ resources/js/Pages/Voucher/Index.jsx | 12 ++ 6 files changed, 140 insertions(+), 42 deletions(-) create mode 100644 resources/js/Pages/Home/Index/VoucherCard.jsx 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 ( @@ -69,15 +119,18 @@ export default function Index({ auth: { user }, infos, banners, locations }) { ))} - {/* voucher */}
{/* chips */}
{locations.map((location) => (
handleSelectLoc(location)} key={location.id} - // selected: px-2 py-1 rounded-2xl text-white bg-blue-600 border border-blue-800 - className="px-2 py-1 rounded-2xl bg-blue-100 border border-blue-200" + className={`px-2 py-1 rounded-2xl ${ + location.id === locId + ? 'text-white bg-blue-600 border border-blue-800' + : 'bg-blue-100 border border-blue-200' + }`} > {location.name}
@@ -86,43 +139,17 @@ export default function Index({ auth: { user }, infos, banners, locations }) { {/* voucher */}
- {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((x) => ( + {v.map((voucher) => ( + + ))} + {next_page_url !== null && (
-
- Lawaranet voucher internet sedap -
-
-
-
-
- Jabriel.net -
-
- IDR 10.000 -
-
-
- 50% -
-
- 20.000 -
-
-
-
-
- 8GB -
-
- 3 Hari -
-
-
+ Load more
- ))} + )}
diff --git a/resources/js/Pages/Home/Index/VoucherCard.jsx b/resources/js/Pages/Home/Index/VoucherCard.jsx new file mode 100644 index 0000000..4d4f242 --- /dev/null +++ b/resources/js/Pages/Home/Index/VoucherCard.jsx @@ -0,0 +1,38 @@ +import { formatIDR } from '@/utils' + +export default function VoucherCard({ voucher }) { + return ( +
+
{voucher.location.name}
+
+
+
+
+ {voucher.location.name} +
+
+ IDR {formatIDR(voucher.price)} +
+ {+voucher.discount !== 0 && ( +
+
+ {voucher.discount}% +
+
+ {formatIDR(voucher.display_price)} +
+
+ )} +
+
+
+ {voucher.display_quota} +
+
+ {voucher.display_expired} +
+
+
+
+ ) +} diff --git a/resources/js/Pages/Voucher/Index.jsx b/resources/js/Pages/Voucher/Index.jsx index 318d0d2..394d5f8 100644 --- a/resources/js/Pages/Voucher/Index.jsx +++ b/resources/js/Pages/Voucher/Index.jsx @@ -96,6 +96,12 @@ export default function Index(props) { > No + + Lokasi + {index + 1} + + {voucher.location.name} +