From 6e880dda99b1e7f158d7e507c796f56eff7d9b78 Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Sat, 3 Jun 2023 01:25:57 +0700 Subject: [PATCH] add dummy voucher --- .../Controllers/Customer/HomeController.php | 2 +- app/Models/Voucher.php | 2 +- database/seeders/DummySeeder.php | 31 +++++++++++++++++++ resources/js/Pages/Home/Index/Index.jsx | 30 ++++++++++-------- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Customer/HomeController.php b/app/Http/Controllers/Customer/HomeController.php index a54187d..f4f6631 100644 --- a/app/Http/Controllers/Customer/HomeController.php +++ b/app/Http/Controllers/Customer/HomeController.php @@ -29,7 +29,7 @@ class HomeController extends Controller 'banners' => $banners, 'locations' => $locations, 'vouchers' => $vouchers->paginate(10), - '_location_id' => $request->location_id + '_location_id' => $request->location_id ?? '' ]); } diff --git a/app/Models/Voucher.php b/app/Models/Voucher.php index 096412d..fe363d2 100644 --- a/app/Models/Voucher.php +++ b/app/Models/Voucher.php @@ -71,6 +71,6 @@ class Voucher extends Model public function location() { - return $this->belongsTo(Location::class); + return $this->belongsTo(Location::class)->withTrashed(); } } diff --git a/database/seeders/DummySeeder.php b/database/seeders/DummySeeder.php index 0806881..384b113 100644 --- a/database/seeders/DummySeeder.php +++ b/database/seeders/DummySeeder.php @@ -6,7 +6,11 @@ use App\Models\Account; use App\Models\Banner; use App\Models\Info; use App\Models\Location; +use App\Models\Voucher; +use App\Services\GeneralService; use Illuminate\Database\Seeder; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; class DummySeeder extends Seeder { @@ -21,6 +25,7 @@ class DummySeeder extends Seeder $this->banner(); $this->account(); $this->location(); + $this->voucher(); } public function info() @@ -71,4 +76,30 @@ class DummySeeder extends Seeder ]); } } + + public function voucher() + { + $batchId = Str::ulid(); + $vouchers = GeneralService::script_parser(file_get_contents(public_path('example.md'))); + + $location = Location::first(); + + DB::beginTransaction(); + foreach ($vouchers as $voucher) { + Voucher::create([ + 'location_id' => $location->id, + 'username' => $voucher['username'], + 'password' => $voucher['password'], + 'discount' => 10, + 'display_price' => 100000, + 'quota' => $voucher['quota'], + 'profile' => $voucher['profile'], + 'comment' => $voucher['comment'], + 'expired' => 30, + 'expired_unit' => 'Hari', + 'batch_id' => $batchId, + ]); + } + DB::commit(); + } } diff --git a/resources/js/Pages/Home/Index/Index.jsx b/resources/js/Pages/Home/Index/Index.jsx index 669dd12..bc301cb 100644 --- a/resources/js/Pages/Home/Index/Index.jsx +++ b/resources/js/Pages/Home/Index/Index.jsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react' +import React, { useState } from 'react' import { Head, router, usePage } from '@inertiajs/react' import CustomerLayout from '@/Layouts/CustomerLayout' @@ -30,14 +30,16 @@ const GuestBanner = () => { ) } -export default function Index({ - auth: { user }, - infos, - banners, - locations, - vouchers: { data, next_page_url }, - _location_id, -}) { +export default function Index(props) { + const { + auth: { user }, + infos, + banners, + locations, + vouchers: { data, next_page_url }, + _location_id, + } = props + const [locId, setLocId] = useState(_location_id) const [v, setV] = useState(data) @@ -48,12 +50,14 @@ export default function Index({ const handleSelectLoc = (loc) => { if (loc.id === locId) { setLocId('') + fetch('') return } setLocId(loc.id) + fetch(loc.id) } - const loadMore = () => { + const handleNextPage = () => { router.get( next_page_url, { @@ -70,7 +74,7 @@ export default function Index({ ) } - useEffect(() => { + const fetch = (locId) => { router.get( route(route().current()), { location_id: locId }, @@ -82,7 +86,7 @@ export default function Index({ }, } ) - }, [locId]) + } return ( @@ -144,7 +148,7 @@ export default function Index({ ))} {next_page_url !== null && (
Load more