68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\AboutUs\TermAndPrivacy;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class TermAndPrivacyCreate extends Component
|
|
{
|
|
public $loading = false;
|
|
public $type;
|
|
public $form = [
|
|
'title' => '',
|
|
'details' => '',
|
|
];
|
|
public $errors = [];
|
|
|
|
public function mount($id)
|
|
{
|
|
$this->type = $id; // 1 for Terms, 2 for Privacy
|
|
}
|
|
|
|
public function submit()
|
|
{
|
|
$this->validateForm();
|
|
|
|
$this->loading = true;
|
|
try {
|
|
$params = array_merge($this->form, ['type' => $this->type]);
|
|
$response = Http::post('https://api.example.com/TermsAndPrivacy', $params);
|
|
if ($response->successful()) {
|
|
session()->flash('success', 'New record added.');
|
|
$this->redirect(route('term-privacy.list'));
|
|
}
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', 'Failed to create record: ' . $e->getMessage());
|
|
}
|
|
$this->loading = false;
|
|
}
|
|
|
|
public function validateForm()
|
|
{
|
|
$rules = [
|
|
'form.title' => 'required|max:128',
|
|
'form.details' => 'required|max:32000',
|
|
];
|
|
|
|
$messages = [
|
|
'form.title.required' => 'Title is required!',
|
|
'form.title.max' => 'Maximum character is 128.',
|
|
'form.details.required' => 'Details is required!',
|
|
'form.details.max' => 'Maximum character is 32,000.',
|
|
];
|
|
|
|
$this->errors = [];
|
|
try {
|
|
$this->validate($rules, $messages);
|
|
} catch (\Exception $e) {
|
|
$this->errors = collect($e->errors())->flatten()->toArray();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.about-us.term-and-privacy.term-and-privacy-create');
|
|
}
|
|
} |