Skip to content

Commit

Permalink
Add tests and advanced availability support
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Nov 28, 2024
1 parent c00a7ce commit 1c632d6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 20 deletions.
50 changes: 34 additions & 16 deletions resources/views/livewire/components/availability-dates.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class="absolute inset-y-0 end-0 flex z-1 items-center pe-3.5 cursor-pointer"
Alpine.data('datepicker', () => ({
mode: $wire.calendar,
dates: $wire.data.dates,
advanced: $wire.advanced,
advancedSelected: $wire.entangle('data.advanced'),
calendar: null,
minPeriod: {{ config('resrv-config.minimum_reservation_period_in_days', 0) }},
maxPeriod: {{ config('resrv-config.maximum_reservation_period_in_days', 30) }},
Expand All @@ -67,8 +69,23 @@ class="absolute inset-y-0 end-0 flex z-1 items-center pe-3.5 cursor-pointer"
const minDate = dayjs().add({{ config('resrv-config.minimum_days_before') }}, 'day').format('YYYY-MM-DD');
if (this.showAvailaiblityOnCalendar !== false) {
this.availabilityCalendar = await $wire.availabilityCalendar();
if (this.advanced !== false && this.advancedSelected === null) {
this.availabilityCalendar = [];
} else {
this.availabilityCalendar = await $wire.availabilityCalendar();
}
}
this.$watch('advancedSelected', async (value, oldValue) => {
if (this.showAvailaiblityOnCalendar !== false && value !== null) {
this.availabilityCalendar = await $wire.availabilityCalendar();
} else if (this.showAvailaiblityOnCalendar !== false && value === null) {
this.availabilityCalendar = [];
}
this.calendar.update({
dates: false,
});
});
this.calendar = new window.calendar(this.$refs.dateInput, {
type: this.mode === 'range' ? 'multiple' : 'default',
Expand All @@ -83,7 +100,7 @@ class="absolute inset-y-0 end-0 flex z-1 items-center pe-3.5 cursor-pointer"
onCreateDateEls: (self, dateEl) => {
if (this.showAvailaiblityOnCalendar !== false) {
this.addPriceToEachDate(self, dateEl);
this.addPriceToEachDate(dateEl);
}
if (this.disabledDays !== false) {
this.disableDays(dateEl);
Expand Down Expand Up @@ -111,8 +128,8 @@ class="absolute inset-y-0 end-0 flex z-1 items-center pe-3.5 cursor-pointer"
this.dateChanged(self.context.selectedDates);
},
onUpdate(self, event) {
self.set({ selectedDates: [] });
onUpdate(self) {
//self.set({ selectedDates: [] });
}
});
Expand Down Expand Up @@ -154,20 +171,21 @@ class="absolute inset-y-0 end-0 flex z-1 items-center pe-3.5 cursor-pointer"
}
},
addPriceToEachDate(self, dateEl) {
addPriceToEachDate(dateEl) {
const button = dateEl.querySelector('button');
const date = dateEl.dataset.vcDate;
if (this.availabilityCalendar[date] === undefined) return;
if (this.availabilityCalendar[date]?.available === 0) {
button.style.cssText = 'cursor: default; opacity: 0.5; text-decoration: line-through;';
button.onclick = (event) => event.stopPropagation();
return;
}
button.insertAdjacentHTML(
'beforeend',
`<span class="vc-price">{{ config('resrv-config.currency_symbol') }}${Math.round(this.availabilityCalendar[date]?.price)}</span>`
);
if (Object.values(this.availabilityCalendar).length > 0) {
if (this.availabilityCalendar[date] === undefined || this.availabilityCalendar[date]?.available === 0) {
button.style.cssText = 'cursor: default; opacity: 0.5; text-decoration: line-through;';
button.onclick = (event) => event.stopPropagation();
return;
}
button.insertAdjacentHTML(
'beforeend',
`<span class="vc-price">{{ config('resrv-config.currency_symbol') }}${Math.round(this.availabilityCalendar[date]?.price)}</span>`
);
}
},
disableDays(dateEl) {
Expand Down Expand Up @@ -205,14 +223,14 @@ class="absolute inset-y-0 end-0 flex z-1 items-center pe-3.5 cursor-pointer"
},
resetDates() {
this.calendar.set({ selectedDates: [] });
this.calendar.update({
dates: true,
});
this.$refs.dateInput.value = '';
$wire.clearDates();
$dispatch('availability-search-cleared');
},
}));
</script>
@endscript
4 changes: 2 additions & 2 deletions src/Livewire/Traits/HandlesAvailabilityQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public function getAvailabilityCalendar(): array

return app(Availability::class)
->where('statamic_id', $entry)
->where('date', '>=', now())
->where('date', '>=', now()->startOfDay())
->when($this->advanced && $this->data->advanced, function ($query) {
return $query->where('advanced', $this->data->advanced);
return $query->where('property', $this->data->advanced);
})
->get(['date', 'available', 'price', 'property'])
->groupBy('date')
Expand Down
2 changes: 1 addition & 1 deletion tests/Livewire/AvailabilityResultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AvailabilityResultsTest extends TestCase

public $option;

public function setUp(): void
protected function setUp(): void
{
parent::setUp();
$this->date = now()->add(1, 'day')->setTime(12, 0, 0);
Expand Down
57 changes: 56 additions & 1 deletion tests/Livewire/AvailabilitySearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
use Illuminate\Support\Facades\Config;
use Livewire\Livewire;
use Reach\StatamicResrv\Livewire\AvailabilitySearch;
use Reach\StatamicResrv\Tests\CreatesEntries;
use Reach\StatamicResrv\Tests\TestCase;
use Statamic\Facades;

class AvailabilitySearchTest extends TestCase
{
use CreatesEntries;

public $date;

public function setUp(): void
public $entries;

public $advancedEntries;

protected function setUp(): void
{
parent::setUp();
$this->date = now()->setTime(12, 0, 0);
$this->entries = $this->createEntries();
$this->advancedEntries = $this->createAdvancedEntries();
$this->travelTo(today()->setHour(12));
}

public function test_renders_successfully()
Expand Down Expand Up @@ -346,4 +356,49 @@ public function test_can_set_a_custom_value()
->set('data.customer.adults', 2)
->assertSet('data.customer', ['adults' => 2]);
}

// Test availability calendar returns data when enabled
public function test_availability_calendar_returns_data_when_enabled()
{
$component = Livewire::test(AvailabilitySearch::class, [
'entry' => $this->entries->first()->id(),
'showAvailaiblityOnCalendar' => true,
])
->assertSet('showAvailaiblityOnCalendar', true)
->call('availabilityCalendar');

$calendar = $component->effects['returns'][0];

$key = $this->date->format('Y-m-d').' 00:00:00';

$this->assertIsArray($calendar);
$this->assertArrayHasKey($key, $calendar);
$this->assertEquals(50, $calendar[$key]['price']);
$this->assertEquals(1, $calendar[$key]['available']);
}

// Test availability calendar returns data when enabled with advanced availability
public function test_availability_calendar_with_advanced_availability()
{
$component = Livewire::test(AvailabilitySearch::class, [
'entry' => $this->advancedEntries->first()->id(),
'showAvailaiblityOnCalendar' => true,
])
->assertSet('showAvailaiblityOnCalendar', true)
->set('data.dates', [
'date_start' => $this->date,
'date_end' => $this->date->copy()->add(1, 'day'),
])
->set('data.advanced', 'test')
->call('availabilityCalendar');

$calendar = $component->effects['returns'][0];

$key = $this->date->format('Y-m-d').' 00:00:00';

$this->assertIsArray($calendar);
$this->assertArrayHasKey($key, $calendar);
$this->assertEquals(50, $calendar[$key]['price']);
$this->assertEquals(1, $calendar[$key]['available']);
}
}

0 comments on commit 1c632d6

Please sign in to comment.