From 984e0bd67fb5b2ed695a9d6267bf6bebfe845c1b Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Wed, 14 Jun 2023 21:14:01 +0700 Subject: [PATCH] add default setting --- app/Http/Controllers/SettingController.php | 41 ++++++++++++ resources/js/Pages/Setting/Index.jsx | 76 ++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 app/Http/Controllers/SettingController.php create mode 100644 resources/js/Pages/Setting/Index.jsx diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php new file mode 100644 index 0000000..0b6a273 --- /dev/null +++ b/app/Http/Controllers/SettingController.php @@ -0,0 +1,41 @@ + Setting::all(), + ]); + } + + public function update(Request $request) + { + $request->validate([ + 'app_name' => 'required|string', + ]); + + DB::beginTransaction(); + + foreach ($request->input() as $key => $value) { + Setting::updateOrCreate( + ['key' => $key], + [ + 'value' => $value, + 'type' => 'text', + ] + ); + } + + DB::commit(); + + return redirect()->route('setting.index') + ->with('message', ['type' => 'success', 'message' => 'Setting saved']); + } +} diff --git a/resources/js/Pages/Setting/Index.jsx b/resources/js/Pages/Setting/Index.jsx new file mode 100644 index 0000000..6dc5892 --- /dev/null +++ b/resources/js/Pages/Setting/Index.jsx @@ -0,0 +1,76 @@ +import React from 'react' +import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout' +import FormInput from '@/Components/FormInput' +import Button from '@/Components/Button' +import { Head, useForm } from '@inertiajs/react' +import TextArea from '@/Components/TextArea' +import { isEmpty } from 'lodash' + +const extractValue = (set, key) => { + const find = set.find((s) => s.key === key) + if (isEmpty(find) === false) { + if (find.type === 'image') { + return find?.url + } + return find?.value + } + return '' +} + +export default function Setting(props) { + const { setting } = props + + const { data, setData, post, processing, errors } = useForm({ + app_name: extractValue(setting, 'app_name'), + }) + + const handleOnChange = (event) => { + setData( + event.target.name, + event.target.type === 'checkbox' + ? event.target.checked + ? 1 + : 0 + : event.target.value + ) + } + + const handleSubmit = () => { + post(route('setting.update')) + } + + return ( + + + +
+
+
+
Setting
+ +
+ +
+
+
+
+
+ ) +}