Skip to content

Commit 5967829

Browse files
author
Itamar Junior
committed
feat: add edit and delete functionality to service contracts
1 parent 9a0acdc commit 5967829

File tree

3 files changed

+165
-32
lines changed

3 files changed

+165
-32
lines changed

app/Livewire/Company/Service/ContractManager.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class ContractManager extends Component
3030

3131
public bool $showCreateModal = false;
3232

33+
public bool $showEditModal = false;
34+
public bool $showDeleteModal = false;
35+
36+
public ?CompanyServiceContract $editingContract = null;
37+
public ?CompanyServiceContract $deletingContract = null;
38+
3339
public function mount()
3440
{
3541
$this->companies = Company::where('business_id', Auth::user()->current_business_id)->orderBy('name')->get();
@@ -86,6 +92,72 @@ public function create()
8692
$this->loadContracts();
8793
session()->flash('success', 'Service contract created.');
8894
}
95+
96+
public function edit($id)
97+
{
98+
$this->editingContract = CompanyServiceContract::findOrFail($id);
99+
100+
$this->company_id = $this->editingContract->company_id;
101+
$this->service_category_id = $this->editingContract->service_category_id;
102+
$this->updatedServiceCategoryId($this->service_category_id); // Load related providers
103+
$this->service_provider_id = $this->editingContract->service_provider_id;
104+
$this->budget = $this->editingContract->budget;
105+
$this->start_date = $this->editingContract->start_date;
106+
$this->next_due_date = $this->editingContract->next_due_date;
107+
$this->status = $this->editingContract->status;
108+
$this->notes = $this->editingContract->notes;
109+
110+
$this->showEditModal = true;
111+
}
112+
113+
public function update()
114+
{
115+
$this->validate([
116+
'company_id' => 'required|exists:companies,id',
117+
'service_category_id' => 'required|exists:service_categories,id',
118+
'service_provider_id' => 'required|exists:service_providers,id',
119+
'budget' => 'nullable|numeric',
120+
'start_date' => 'nullable|date',
121+
'next_due_date' => 'nullable|date',
122+
'status' => 'required|in:active,inactive,terminated',
123+
]);
124+
125+
if (! $this->editingContract) return;
126+
127+
$this->editingContract->update([
128+
'company_id' => $this->company_id,
129+
'service_category_id' => $this->service_category_id,
130+
'service_provider_id' => $this->service_provider_id,
131+
'budget' => $this->budget,
132+
'start_date' => $this->start_date,
133+
'next_due_date' => $this->next_due_date,
134+
'status' => $this->status,
135+
'notes' => $this->notes,
136+
]);
137+
138+
$this->showEditModal = false;
139+
$this->editingContract = null;
140+
$this->loadContracts();
141+
session()->flash('success', 'Contract updated.');
142+
}
143+
144+
public function confirmDelete($id)
145+
{
146+
$this->deletingContract = CompanyServiceContract::findOrFail($id);
147+
$this->showDeleteModal = true;
148+
}
149+
150+
public function delete()
151+
{
152+
if (! $this->deletingContract) return;
153+
154+
$this->deletingContract->delete();
155+
$this->deletingContract = null;
156+
$this->showDeleteModal = false;
157+
$this->loadContracts();
158+
session()->flash('success', 'Contract deleted.');
159+
}
160+
89161
public function render()
90162
{
91163
return view('livewire.company.service.contract-manager');

resources/views/livewire/company/service/contract-manager.blade.php

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
<div class="text-xs">{{ ucfirst($contract->status) }}</div>
2626
</div>
2727
</div>
28+
<div class="flex gap-2 mt-2 text-right">
29+
<flux:button size="sm" variant="outline" wire:click="edit({{ $contract->id }})">
30+
{{ __('Edit') }}
31+
</flux:button>
32+
<flux:button size="sm" variant="outline" class="!text-red-600 hover:!bg-red-100"
33+
wire:click="confirmDelete({{ $contract->id }})">
34+
{{ __('Delete') }}
35+
</flux:button>
36+
</div>
2837
</div>
2938
@empty
3039
<p class="text-sm text-gray-500">{{ __('No contracts created yet.') }}</p>
@@ -36,38 +45,7 @@
3645
<x-ui.modal wire:model="showCreateModal">
3746
<div class="mt-3 text-center sm:mt-5">
3847
<h3 class="text-base font-semibold text-gray-900">{{ __('Create Service Contract') }}</h3>
39-
<div class="mt-4 space-y-4 text-left">
40-
<flux:select wire:model.defer="company_id" :label="__('Company')">
41-
<option value="">{{ __('Select Company') }}</option>
42-
@foreach ($companies as $company)
43-
<option value="{{ $company->id }}">{{ $company->name }}</option>
44-
@endforeach
45-
</flux:select>
46-
47-
<flux:select wire:model.live="service_category_id" :label="__('Service Category')">
48-
<option value="">{{ __('Select Category') }}</option>
49-
@foreach ($categories as $category)
50-
<option value="{{ $category->id }}">{{ $category->name }}</option>
51-
@endforeach
52-
</flux:select>
53-
54-
<flux:select wire:model.defer="service_provider_id" :label="__('Service Provider')">
55-
<option value="">{{ __('Select Provider') }}</option>
56-
@foreach ($providers as $provider)
57-
<option value="{{ $provider->id }}">{{ $provider->name }}</option>
58-
@endforeach
59-
</flux:select>
60-
61-
<flux:input wire:model.defer="budget" :label="__('Budget (€)')" type="number" step="0.01" />
62-
<flux:input wire:model.defer="start_date" :label="__('Start Date')" type="date" />
63-
<flux:input wire:model.defer="next_due_date" :label="__('Next Due Date')" type="date" />
64-
<flux:select wire:model.defer="status" :label="__('Status')">
65-
<option value="active">{{ __('Active') }}</option>
66-
<option value="inactive">{{ __('Inactive') }}</option>
67-
<option value="terminated">{{ __('Terminated') }}</option>
68-
</flux:select>
69-
<flux:input wire:model.defer="notes" :label="__('Notes')" />
70-
</div>
48+
@include('livewire.company.service.partials.contract-form')
7149
</div>
7250

7351
<div class="mt-5 sm:mt-6 sm:grid sm:grid-cols-2 sm:gap-3">
@@ -81,5 +59,53 @@
8159
</div>
8260
</x-ui.modal>
8361
@endif
62+
63+
@if ($showEditModal)
64+
<x-ui.modal wire:model="showEditModal">
65+
<div class="mt-3 text-center sm:mt-5">
66+
<h3 class="text-base font-semibold text-gray-900">{{ __('Edit Service Contract') }}</h3>
67+
@include('livewire.company.service.partials.contract-form')
68+
</div>
69+
<div class="mt-5 sm:grid sm:grid-cols-2 sm:gap-3">
70+
<flux:button type="button" variant="outline" class="w-full"
71+
wire:click="$set('showEditModal', false)">
72+
{{ __('Cancel') }}
73+
</flux:button>
74+
<flux:button type="button" class="w-full" wire:click="update" wire:keydown.enter="update">
75+
{{ __('Update Contract') }}
76+
</flux:button>
77+
</div>
78+
</x-ui.modal>
79+
@endif
80+
81+
@if ($showDeleteModal)
82+
<x-ui.modal wire:model="showDeleteModal">
83+
<div class="mx-auto flex size-12 items-center justify-center rounded-full bg-red-100">
84+
<svg class="size-6 text-red-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
85+
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
86+
</svg>
87+
</div>
88+
<div class="mt-3 text-center">
89+
<h3 class="text-base font-semibold text-gray-900">
90+
{{ __('Delete Contract') }}
91+
</h3>
92+
<p class="text-sm text-gray-500 mt-2">
93+
{{ __('Are you sure you want to delete this contract? This action cannot be undone.') }}
94+
</p>
95+
</div>
96+
<div class="mt-5 sm:grid sm:grid-cols-2 sm:gap-3">
97+
<flux:button type="button" variant="outline" class="w-full"
98+
wire:click="$set('showDeleteModal', false)">
99+
{{ __('Cancel') }}
100+
</flux:button>
101+
<flux:button type="button" variant="outline" class="!text-red-600 hover:!bg-red-100"
102+
wire:click="delete">
103+
{{ __('Yes, delete') }}
104+
</flux:button>
105+
</div>
106+
</x-ui.modal>
107+
@endif
108+
109+
84110
</x-contract.layout>
85111
</section>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@props(['companies', 'categories', 'providers'])
2+
<div class="mt-4 space-y-4 text-left">
3+
<flux:select wire:model.defer="company_id" :label="__('Company')">
4+
<option value="">{{ __('Select Company') }}</option>
5+
@foreach ($companies as $company)
6+
<option value="{{ $company->id }}">{{ $company->name }}</option>
7+
@endforeach
8+
</flux:select>
9+
10+
<flux:select wire:model.live="service_category_id" :label="__('Service Category')">
11+
<option value="">{{ __('Select Category') }}</option>
12+
@foreach ($categories as $category)
13+
<option value="{{ $category->id }}">{{ $category->name }}</option>
14+
@endforeach
15+
</flux:select>
16+
17+
<flux:select wire:model.defer="service_provider_id" :label="__('Service Provider')">
18+
<option value="">{{ __('Select Provider') }}</option>
19+
@foreach ($providers as $provider)
20+
<option value="{{ $provider->id }}">{{ $provider->name }}</option>
21+
@endforeach
22+
</flux:select>
23+
24+
<flux:input wire:model.defer="budget" :label="__('Budget (€)')" type="number" step="0.01" />
25+
<flux:input wire:model.defer="start_date" :label="__('Start Date')" type="date" />
26+
<flux:input wire:model.defer="next_due_date" :label="__('Next Due Date')" type="date" />
27+
28+
<flux:select wire:model.defer="status" :label="__('Status')">
29+
<option value="active">{{ __('Active') }}</option>
30+
<option value="inactive">{{ __('Inactive') }}</option>
31+
<option value="terminated">{{ __('Terminated') }}</option>
32+
</flux:select>
33+
34+
<flux:input wire:model.defer="notes" :label="__('Notes')" />
35+
</div>

0 commit comments

Comments
 (0)