-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathOption.php
More file actions
197 lines (167 loc) · 6.15 KB
/
Option.php
File metadata and controls
197 lines (167 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace App\Livewire\Planning\DataExtraction;
use Livewire\Attributes\On;
use Livewire\Component;
use App\Models\Project as ProjectModel;
use App\Models\Project\Planning\DataExtraction\Option as OptionModel;
use App\Utils\ActivityLogHelper as Log;
use App\Utils\ToastHelper;
use Illuminate\Support\Facades\Auth;
use App\Traits\ProjectPermissions;
class Option extends Component
{
use ProjectPermissions;
public $toastMessages = 'project/planning.data-extraction.toasts';
public $currentProject;
public $currentOption;
public $options = [];
public $optionId;
public $questionId = [];
public $description;
// Estado do formulário, indicando se está em modo de edição ou não.
public $form = [
'isEditing' => false,
];
// Regras de validação para os campos do formulário.
protected $rules = [
'description' => 'required|string|regex:/^[\pL\pN\s\.,;:\?"\'\(\)\[\]\{\}\/\\\\_\-+=#@!%&*]+$/u|max:255',
'questionId' => 'required|array',
'questionId.*.value' => 'exists:question_extraction,id',
];
// Mensagens de erro personalizadas para as regras de validação.
protected function messages()
{
return [
'description.required' => 'Este campo é obrigatório',
'questionId.required' => 'Este campo é obrigatório',
];
}
// Inicialização do componente Livewire.
public function mount()
{
$projectId = request()->segment(2);
$this->currentProject = ProjectModel::findOrFail($projectId);
$this->currentOption = null;
$this->options = OptionModel::whereHas('question', function ($query) {
$query->where('id_project', $this->currentProject->id_project);
})->get();
}
// Exibe a mensagem de toast com o tipo e a mensagem fornecidos.
public function toast(string $message, string $type)
{
$this->dispatch('options', ToastHelper::dispatch($type, $message));
}
// Reseta os campos do formulário para o estado inicial.
private function resetFields()
{
$this->optionId = null;
$this->description = null;
$this->questionId = [];
$this->form['isEditing'] = false;
}
// Atualiza a lista de opções de perguntas. ouve o evento 'update-question-select' para atualizar as opções de perguntas.
#[On('update-question-select')]
public function updateOptions()
{
if (!$this->checkEditPermission($this->toastMessages . '.denied')) {
return;
}
// Recarrega as opções do projeto
$this->options = OptionModel::whereHas('question', function ($query) {
$query->where('id_project', $this->currentProject->id_project);
})->get();
$this->dispatch('update-table');
}
// FSubmete o formulário para criar ou atualizar uma opção de pergunta.
public function submit()
{
if (!$this->checkEditPermission($this->toastMessages . '.denied')) {
return;
}
$this->validate();
$updateIf = [
'id_option' => $this->currentOption?->id_option,
];
// Criar ou atualizar a questão, tentar alterar o registro ou criar uma nova no banco de dados, registra a atividade.
try {
$value = $this->form['isEditing'] ? 'Updated the option' : 'Added a option';
$toastMessage = $this->form['isEditing']
? 'Opção atualizada com sucesso!' : 'Opção adicionada com sucesso!';
$updatedOrCreated = OptionModel::updateOrCreate($updateIf, [
'id_de' => $this->questionId["value"],
'description' => $this->description,
]);
Log::logActivity(
action: $value,
description: $updatedOrCreated->description,
projectId: $this->currentProject->id_project
);
$this->updateOptions();
$this->toast(
message: $toastMessage,
type: 'success'
);
} catch (\Exception $e) {
$this->toast(
message: $e->getMessage(),
type: 'error'
);
} finally {
$this->resetFields();
}
}
// Funçao para preencher os campos do formulário com os dados da opção selecionada
#[On('data-extraction-table-edit-option')]
public function edit(string $optionId)
{
if (!$this->checkEditPermission($this->toastMessages . '.denied')) {
return;
}
$this->currentOption = OptionModel::findOrFail($optionId);
$this->optionId = $this->currentOption->id;
$this->description = $this->currentOption->description;
$this->questionId['value'] = $this->currentOption->id_de;
$this->form['isEditing'] = true;
}
// Função para deletar uma opção de pergunta
#[On('data-extraction-table-delete-option')]
public function delete(string $optionId)
{
if (!$this->checkEditPermission($this->toastMessages . '.denied')) {
return;
}
// Busca a opção pelo ID e tenta deletá-la, mostrando mensagens de sucesso ou erro conforme necessário.
try {
$currentOption = OptionModel::findOrFail($optionId);
$currentOption->delete();
Log::logActivity(
action: 'Deleted the option',
description: $currentOption->description,
projectId: $this->currentProject->id_project
);
$this->toast(
message: 'Opção deletada com sucesso',
type: 'success'
);
$this->updateOptions();
} catch (\Exception $e) {
$this->toast(
message: $e->getMessage(),
type: 'error'
);
} finally {
$this->resetFields();
}
}
// Renderiza a view do componente.
public function render()
{
$project = $this->currentProject;
return view(
'livewire.planning.data-extraction.option',
compact(
'project',
)
)->extends('layouts.app');
}
}