86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\AboutUs\TermAndPrivacy;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class TermAndPrivacyEdit extends Component
|
|
{
|
|
public $loading = false;
|
|
public $term;
|
|
public $form = [
|
|
'title' => '',
|
|
'details' => '',
|
|
];
|
|
public $errors = [];
|
|
|
|
public function mount($id)
|
|
{
|
|
$this->fetchTerm($id);
|
|
}
|
|
|
|
public function fetchTerm($id)
|
|
{
|
|
try {
|
|
$response = Http::get("https://api.example.com/TermsAndPrivacy/{$id}");
|
|
$this->term = $response->json()['data'] ?? null;
|
|
if (!$this->term) {
|
|
throw new \Exception('No data found');
|
|
}
|
|
$this->form = [
|
|
'title' => $this->term['title'] ?? '',
|
|
'details' => $this->term['details'] ?? '',
|
|
];
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', 'Failed to load term details: ' . $e->getMessage());
|
|
$this->redirect(route('term-privacy.list'));
|
|
}
|
|
}
|
|
|
|
public function submit()
|
|
{
|
|
$this->validateForm();
|
|
|
|
$this->loading = true;
|
|
try {
|
|
$params = array_merge($this->form, ['type' => $this->term['type']]);
|
|
$response = Http::put("https://api.example.com/TermsAndPrivacy/{$this->term['tp_uuid']}", $params);
|
|
if ($response->successful()) {
|
|
session()->flash('success', 'Record was successfully updated.');
|
|
$this->redirect(route('term-privacy.list'));
|
|
}
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', 'Failed to update 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-edit');
|
|
}
|
|
} |