add default setting

v1
Aji Kamaludin 10 months ago
parent f523351449
commit 984e0bd67f
No known key found for this signature in database
GPG Key ID: 19058F67F0083AD3

@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SettingController extends Controller
{
public function index()
{
return inertia('Setting/Index', [
'setting' => 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']);
}
}

@ -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 (
<AuthenticatedLayout
auth={props.auth}
errors={props.errors}
flash={props.flash}
page={'Setting'}
action={''}
>
<Head title="Setting" />
<div>
<div className="mx-auto sm:px-6 lg:px-8">
<div className="overflow-hidden p-4 shadow-sm sm:rounded-lg bg-white dark:bg-gray-800 flex flex-col">
<div className="text-xl font-bold mb-4">Setting</div>
<FormInput
name="app_name"
value={data.app_name}
onChange={handleOnChange}
label="App Name"
error={errors.app_name}
/>
<div className="mt-2">
<Button
onClick={handleSubmit}
processing={processing}
>
Simpan
</Button>
</div>
</div>
</div>
</div>
</AuthenticatedLayout>
)
}
Loading…
Cancel
Save