From 767612179aaabd25d4be0ab9ef0f8ceee0c912b7 Mon Sep 17 00:00:00 2001 From: ajikamaludin Date: Mon, 30 Jan 2023 18:26:05 +0700 Subject: [PATCH] temp --- app/Http/Controllers/CompanyController.php | 78 +++++++++ app/Models/Company.php | 5 + app/Models/Document.php | 7 +- ...22_09_18_034528_create_documents_table.php | 10 +- resources/js/Layouts/AuthenticatedLayout.jsx | 2 +- resources/js/Pages/Company/FormModal.jsx | 158 ++++++++++++++++++ resources/js/Pages/Company/Index.jsx | 122 ++++++++++++++ resources/js/Pages/Setting/Index.jsx | 2 +- routes/web.php | 6 + 9 files changed, 382 insertions(+), 8 deletions(-) create mode 100644 app/Http/Controllers/CompanyController.php create mode 100644 resources/js/Pages/Company/FormModal.jsx create mode 100644 resources/js/Pages/Company/Index.jsx diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php new file mode 100644 index 0000000..1ba2d42 --- /dev/null +++ b/app/Http/Controllers/CompanyController.php @@ -0,0 +1,78 @@ + Company::with(['region'])->paginate(), + 'regions' => Region::all(), + ]); + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + $request->validate([ + 'name' => 'required|string|max:255', + 'short' => 'required|string|max:255', + 'region_id' => 'required|exists:regions,id' + ]); + + Company::create([ + 'region_id' => $request->region_id, + 'name' => $request->name, + 'short' => $request->short + ]); + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param \App\Models\Company $company + * @return \Illuminate\Http\Response + */ + public function update(Request $request, Company $company) + { + $request->validate([ + 'name' => 'required|string|max:255', + 'short' => 'required|string|max:255', + 'region_id' => 'required|exists:regions,id' + ]); + + $company->update([ + 'region_id' => $request->region_id, + 'name' => $request->name, + 'short' => $request->short + ]); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Company $company + * @return \Illuminate\Http\Response + */ + public function destroy(Company $company) + { + $company->users()->delete(); + $company->delete(); + } +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 75d51d7..f9c04f8 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -15,6 +15,11 @@ class Company extends Model "short" ]; + public function region() + { + return $this->belongsTo(Region::class); + } + public function users() { return $this->hasMany(User::class); diff --git a/app/Models/Document.php b/app/Models/Document.php index 853aab1..188862d 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -21,7 +21,6 @@ class Document extends Model "no", "no_doc", "name", - "company_name", "type_id", "category_id", "publisher", @@ -30,10 +29,12 @@ class Document extends Model "due_date", "status", "type", - "group", - "region", "document", "user_id", + "company_id", + // "company_name", + // "group", + // "region", ]; protected $casts = [ diff --git a/database/migrations/2022_09_18_034528_create_documents_table.php b/database/migrations/2022_09_18_034528_create_documents_table.php index 07afc35..1ceb2b7 100644 --- a/database/migrations/2022_09_18_034528_create_documents_table.php +++ b/database/migrations/2022_09_18_034528_create_documents_table.php @@ -17,7 +17,6 @@ return new class extends Migration { $table->string("no")->nullable(); $table->string("no_doc")->nullable(); $table->string("name")->nullable(); - $table->string("company_name")->nullable(); $table->foreignId("type_id")->constrained(); //select jenis $table->foreignId("category_id")->constrained(); //select $table->string("publisher")->nullable(); @@ -26,10 +25,15 @@ return new class extends Migration { $table->timestamp("due_date")->nullable(); //for reminder $table->smallInteger("status")->default(1); //only 1 yes/ 0no $table->smallInteger("type")->default(1); //only 1 tetap/ 0tidak tetap - $table->string("group")->nullable(); - $table->string("region")->nullable(); $table->string("document")->nullable(); $table->foreignId("user_id")->constrained(); + + $table->foreignId("company_id")->constrained(); + // ? + // $table->string("group")->nullable(); + // $table->string("region")->nullable(); + // $table->string("company_name")->nullable(); + $table->timestamps(); }); } diff --git a/resources/js/Layouts/AuthenticatedLayout.jsx b/resources/js/Layouts/AuthenticatedLayout.jsx index d4e0b5f..584104b 100644 --- a/resources/js/Layouts/AuthenticatedLayout.jsx +++ b/resources/js/Layouts/AuthenticatedLayout.jsx @@ -16,7 +16,7 @@ const rs = [ {name: "Perusahaan", show: true, items: [ {name:"Group", route: "groups.index", show: true, permission: 'view-group'}, {name:"Region", route: "regions.index", show: true, permission: 'view-region'}, - {name:"Perusahaan", route: "groups.index", show: true, permission: 'view-company'}, + {name:"Perusahaan", route: "companies.index", show: true, permission: 'view-company'}, ]}, {name: "User", show: true, items: [ {name:"User", route: "users.index", show: true, permission: 'view-user'}, diff --git a/resources/js/Pages/Company/FormModal.jsx b/resources/js/Pages/Company/FormModal.jsx new file mode 100644 index 0000000..a5c2bc6 --- /dev/null +++ b/resources/js/Pages/Company/FormModal.jsx @@ -0,0 +1,158 @@ +import React, { useEffect } from 'react' +import { useForm, usePage } from '@inertiajs/react' +import { toast } from 'react-toastify' + +export default function FormModal(props) { + const { modalState } = props + const { props: { regions } } = usePage() + + const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({ + name: '', + short: '', + region_id: '' + }) + + const handleOnChange = (event) => { + setData(event.target.name, event.target.type === 'checkbox' ? event.target.checked : event.target.value); + } + + const handleReset = () => { + reset() + clearErrors() + modalState.setData(null) + } + + const handleCancel = () => { + handleReset() + modalState.toggle() + } + + const handleSubmit = () => { + const company = modalState.data + if(company !== null) { + put(route('companies.update', company), { + onSuccess: () => + Promise.all([ + handleReset(), + modalState.toggle(), + toast.success('The Data has been changed'), + ]), + }) + return + } + post(route('companies.store'), { + onSuccess: () => + Promise.all([ + handleReset(), + modalState.toggle(), + toast.success('The Data has been saved'), + ]), + }) + } + + useEffect(() => { + const company = modalState.data + if (company !== null) { + setData({ + name: company?.name, + short: company?.short, + region_id: company?.region_id, + }) + } + }, [modalState]) + + return ( +
+
+

Jenis

+
+ + + +
+
+ + + +
+
+ + + +
+
+
+ Simpan +
+
+ Batal +
+
+
+
+ ) +} \ No newline at end of file diff --git a/resources/js/Pages/Company/Index.jsx b/resources/js/Pages/Company/Index.jsx new file mode 100644 index 0000000..bff27f1 --- /dev/null +++ b/resources/js/Pages/Company/Index.jsx @@ -0,0 +1,122 @@ +import React from 'react' +import { Head } from '@inertiajs/react' +import { router } from '@inertiajs/react' +import { toast } from 'react-toastify' + +import { useModalState } from '@/Hooks' +import { hasPermission } from '@/utils' +import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout' +import Pagination from '@/Components/Pagination' +import ModalConfirm from '@/Components/ModalConfirm' +import FormModal from './FormModal' + +export default function Types(props) { + const { data: companies, links } = props.companies + + const formModal = useModalState(false) + const toggle = (company = null) => { + formModal.setData(company) + formModal.toggle() + } + + const confirmModal = useModalState(false) + const handleDelete = (company) => { + confirmModal.setData(company) + confirmModal.toggle() + } + + const onDelete = () => { + const company = confirmModal.data + if(company != null) { + router.delete(route('companies.destroy', company), { + onSuccess: () => toast.success('The Data has been deleted'), + }) + } + } + + const canCreate = hasPermission('create-company', props.auth.user) + const canUpdate = hasPermission('update-company', props.auth.user) + const canDelete = hasPermission('delete-company', props.auth.user) + + return ( + + +
+
+
+
+ {canCreate && ( +
toggle()} + > + Tambah +
+ )} +
+
+ + + + + + + + + + + + {companies?.map((company) => ( + + + + + + + + ))} + +
IdNamaInisialRegion
{company.id}{company.name}{company.short}{company.region.name} + {canUpdate && ( +
+ toggle(company) + } + > + Edit +
+ )} + {canDelete && ( +
+ handleDelete(company) + } + > + Delete +
+ )} +
+
+ +
+
+
+ + + +
+ ) +} \ No newline at end of file diff --git a/resources/js/Pages/Setting/Index.jsx b/resources/js/Pages/Setting/Index.jsx index 80dba88..873ef5d 100644 --- a/resources/js/Pages/Setting/Index.jsx +++ b/resources/js/Pages/Setting/Index.jsx @@ -37,7 +37,7 @@ export default function Dashboard(props) {
group(function () { Route::put('/regions/{region}', [RegionController::class, 'update'])->name('regions.update'); Route::delete('/regions/{region}', [RegionController::class, 'destroy'])->name('regions.destroy'); + Route::get('/companies', [CompanyController::class, 'index'])->name('companies.index'); + Route::post('/companies', [CompanyController::class, 'store'])->name('companies.store'); + Route::put('/companies/{company}', [CompanyController::class, 'update'])->name('companies.update'); + Route::delete('/companies/{company}', [CompanyController::class, 'destroy'])->name('companies.destroy'); + Route::get('/types', [TypeController::class, 'index'])->name('types.index'); Route::post('/types', [TypeController::class, 'store'])->name('types.store'); Route::put('/types/{type}', [TypeController::class, 'update'])->name('types.update');