From fa0d534a8e175fc195d066e6c3dc243e704f26d2 Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Fri, 2 Jun 2023 13:23:48 +0700 Subject: [PATCH] bank done --- app/Http/Controllers/AccountController.php | 63 ++++++++ app/Http/Middleware/Authenticate.php | 6 +- database/seeders/DummySeeder.php | 19 +++ database/seeders/PermissionSeeder.php | 5 + resources/js/Layouts/Partials/routes.js | 10 +- resources/js/Pages/Account/FormModal.jsx | 108 +++++++++++++ resources/js/Pages/Account/Index.jsx | 172 +++++++++++++++++++++ routes/admin.php | 7 + vite.config.js | 10 ++ 9 files changed, 398 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/AccountController.php create mode 100644 resources/js/Pages/Account/FormModal.jsx create mode 100644 resources/js/Pages/Account/Index.jsx diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php new file mode 100644 index 0000000..21e9e4e --- /dev/null +++ b/app/Http/Controllers/AccountController.php @@ -0,0 +1,63 @@ +paginate(); + + return inertia('Account/Index', [ + 'query' => $query + ]); + } + + public function store(Request $request) + { + $request->validate([ + 'name' => 'required|string', + 'bank_name' => 'required|string', + 'holder_name' => 'required|string', + 'account_number' => 'required|string', + ]); + + Account::create([ + 'name' => $request->name, + 'bank_name' => $request->bank_name, + 'holder_name' => $request->holder_name, + 'account_number' => $request->account_number, + ]); + + session()->flash('message', ['type' => 'success', 'message' => 'Item has been saved']); + } + + public function update(Request $request, Account $account) + { + $request->validate([ + 'name' => 'required|string', + 'bank_name' => 'required|string', + 'holder_name' => 'required|string', + 'account_number' => 'required|string', + ]); + + $account->update([ + 'name' => $request->name, + 'bank_name' => $request->bank_name, + 'holder_name' => $request->holder_name, + 'account_number' => $request->account_number, + ]); + + session()->flash('message', ['type' => 'success', 'message' => 'Item has been updated']); + } + + public function destroy(Account $account) + { + $account->delete(); + + session()->flash('message', ['type' => 'success', 'message' => 'Item has been deleted']); + } +} diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index a826c51..418ce73 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -14,7 +14,11 @@ class Authenticate extends Middleware */ protected function redirectTo($request) { - if (! $request->expectsJson()) { + if (!$request->expectsJson()) { + $uri = $request->getRequestUri(); + if (str_contains($uri, 'admin')) { + return route('admin.login'); + } return route('customer.login'); } } diff --git a/database/seeders/DummySeeder.php b/database/seeders/DummySeeder.php index 6df9eac..4204bde 100644 --- a/database/seeders/DummySeeder.php +++ b/database/seeders/DummySeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\Account; use App\Models\Banner; use App\Models\Info; use Illuminate\Database\Seeder; @@ -17,6 +18,7 @@ class DummySeeder extends Seeder { $this->info(); $this->banner(); + $this->account(); } public function info() @@ -38,4 +40,21 @@ class DummySeeder extends Seeder ]); } } + + public function account() + { + $banks = [ + ['name' => 'BRI', 'bank_name' => 'Bank Rakyat Indonesia', 'holder_name' => 'Aji Kamaludin', 'account_number' => '187391738129'], + ['name' => 'Jago', 'bank_name' => 'Bank Jago', 'holder_name' => 'Aji Kamaludin', 'account_number' => '718297389172'] + ]; + + foreach ($banks as $bank) { + Account::create([ + 'name' => $bank['name'], + 'bank_name' => $bank['bank_name'], + 'holder_name' => $bank['holder_name'], + 'account_number' => $bank['account_number'], + ]); + } + } } diff --git a/database/seeders/PermissionSeeder.php b/database/seeders/PermissionSeeder.php index fa3ac08..6bb62fe 100644 --- a/database/seeders/PermissionSeeder.php +++ b/database/seeders/PermissionSeeder.php @@ -78,6 +78,11 @@ class PermissionSeeder extends Seeder ['id' => Str::ulid(), 'label' => 'Update Coin Reward', 'name' => 'update-coin-reward'], ['id' => Str::ulid(), 'label' => 'View Coin Reward', 'name' => 'view-coin-reward'], ['id' => Str::ulid(), 'label' => 'Delete Coin Reward', 'name' => 'delete-coin-reward'], + + ['id' => Str::ulid(), 'label' => 'Create Bank Account', 'name' => 'create-account'], + ['id' => Str::ulid(), 'label' => 'Update Bank Account', 'name' => 'update-account'], + ['id' => Str::ulid(), 'label' => 'View Bank Account', 'name' => 'view-account'], + ['id' => Str::ulid(), 'label' => 'Delete Bank Account', 'name' => 'delete-account'], ]; foreach ($permissions as $permission) { diff --git a/resources/js/Layouts/Partials/routes.js b/resources/js/Layouts/Partials/routes.js index 991bbc7..cde5bdb 100644 --- a/resources/js/Layouts/Partials/routes.js +++ b/resources/js/Layouts/Partials/routes.js @@ -5,7 +5,7 @@ import { HiUserGroup, HiInformationCircle, } from 'react-icons/hi' -import { HiQuestionMarkCircle } from 'react-icons/hi2' +import { HiBanknotes, HiQuestionMarkCircle } from 'react-icons/hi2' export default [ { @@ -16,6 +16,14 @@ export default [ active: 'dashboard', permission: 'view-dashboard', }, + { + name: 'Bank Akun', + show: true, + icon: HiBanknotes, + route: route('account.index'), + active: 'account.*', + permission: 'view-account', + }, { name: 'Banner', show: true, diff --git a/resources/js/Pages/Account/FormModal.jsx b/resources/js/Pages/Account/FormModal.jsx new file mode 100644 index 0000000..47a05c5 --- /dev/null +++ b/resources/js/Pages/Account/FormModal.jsx @@ -0,0 +1,108 @@ +import React, { useEffect } from 'react' +import Modal from '@/Components/Modal' +import { useForm } from '@inertiajs/react' +import Button from '@/Components/Button' +import FormInput from '@/Components/FormInput' +import RoleSelectionInput from '../Role/SelectionInput' + +import { isEmpty } from 'lodash' + +export default function FormModal(props) { + const { modalState } = props + const { data, setData, post, put, processing, errors, reset, clearErrors } = + useForm({ + name: '', + bank_name: '', + holder_name: '', + account_number: '', + }) + + const handleOnChange = (event) => { + setData( + event.target.name, + event.target.type === 'checkbox' + ? event.target.checked + ? 1 + : 0 + : event.target.value + ) + } + + const handleReset = () => { + modalState.setData(null) + reset() + clearErrors() + } + + const handleClose = () => { + handleReset() + modalState.toggle() + } + + const handleSubmit = () => { + const account = modalState.data + if (account !== null) { + put(route('account.update', account), { + onSuccess: () => handleClose(), + }) + return + } + post(route('account.store'), { + onSuccess: () => handleClose(), + }) + } + + useEffect(() => { + const account = modalState.data + if (isEmpty(account) === false) { + setData({ + name: account.name, + bank_name: account.bank_name, + holder_name: account.holder_name, + account_number: account.account_number, + }) + return + } + }, [modalState]) + + return ( + + + + + +
+ + +
+
+ ) +} diff --git a/resources/js/Pages/Account/Index.jsx b/resources/js/Pages/Account/Index.jsx new file mode 100644 index 0000000..a1ed482 --- /dev/null +++ b/resources/js/Pages/Account/Index.jsx @@ -0,0 +1,172 @@ +import React, { useEffect, useState } from 'react' +import { router } from '@inertiajs/react' +import { usePrevious } from 'react-use' +import { Head } from '@inertiajs/react' +import { Button, Dropdown } from 'flowbite-react' +import { HiPencil, HiTrash } from 'react-icons/hi' +import { useModalState } from '@/hooks' + +import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout' +import Pagination from '@/Components/Pagination' +import ModalConfirm from '@/Components/ModalConfirm' +import FormModal from './FormModal' +import { hasPermission } from '@/utils' + +export default function Account(props) { + const { + query: { links, data }, + auth, + } = props + + const confirmModal = useModalState() + const formModal = useModalState() + + const toggleFormModal = (account = null) => { + formModal.setData(account) + formModal.toggle() + } + + const handleDeleteClick = (account) => { + confirmModal.setData(account) + confirmModal.toggle() + } + + const onDelete = () => { + if (confirmModal.data !== null) { + router.delete(route('account.destroy', confirmModal.data.id)) + } + } + + const canCreate = hasPermission(auth, 'create-account') + const canUpdate = hasPermission(auth, 'update-account') + const canDelete = hasPermission(auth, 'delete-account') + + return ( + + + +
+
+
+
+ {canCreate && ( + + )} +
+
+
+ + + + + + + + + + {data.map((account) => ( + + + + + + + ))} + +
+ Nama + + Atas Nama Rekening + + Nomor Rekening + +
+ {account.name} + + {account.holder_name} + + {account.account_number} + + + {canUpdate && ( + + toggleFormModal( + account + ) + } + > +
+ +
+ Ubah +
+
+
+ )} + {canDelete && ( + + handleDeleteClick( + account + ) + } + > +
+ +
+ Hapus +
+
+
+ )} +
+
+
+
+ +
+
+
+
+
+ + +
+ ) +} diff --git a/routes/admin.php b/routes/admin.php index 2027157..a0f309a 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -1,5 +1,6 @@ name('info.update'); Route::delete('/infos/{info}', [InfoController::class, 'destroy'])->name('info.destroy'); + // Account + Route::get('/accounts', [AccountController::class, 'index'])->name('account.index'); + Route::post('/accounts', [AccountController::class, 'store'])->name('account.store'); + Route::put('/accounts/{account}', [AccountController::class, 'update'])->name('account.update'); + Route::delete('/accounts/{account}', [AccountController::class, 'destroy'])->name('account.destroy'); + // upload Route::post('/upload', [GeneralController::class, 'upload'])->name('post.upload'); diff --git a/vite.config.js b/vite.config.js index ad98020..9a193e0 100644 --- a/vite.config.js +++ b/vite.config.js @@ -14,4 +14,14 @@ export default defineConfig({ }), react(), ], + // build: { + // // chunkSizeWarningLimit: 1024 * 2048, //2MB + // rollupOptions: { + // output: { + // manualChunks: { + // chunk1: ['tinymce', /tinymce/], + // }, + // }, + // }, + // }, })