Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions app/Enums/ProfileSectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Enums;

enum ProfileSectionType: string
{
case Default = 'default';
case Publications = 'publications';
case Appointments = 'appointments';
case Awards = 'awards';
case News = 'news';
case Support = 'support';
case Presentations = 'presentations';
case Projects = 'projects';
case Additionals = 'additionals';
case Affiliations = 'affiliations';
case Activities = 'activities';
case Areas = 'areas';
case Preparation = 'preparation';

public static function values(): array
{
return array_column(self::cases(), 'value');
}

public function perPage(): int
{
return match ($this) {
self::Publications => 8,
self::Appointments => 10,
self::Awards => 10,
self::Affiliations => 10,
self::News => 5,
self::Support => 5,
self::Presentations => 5,
self::Projects => 5,
self::Additionals => 3,
self::Activities => 6,
self::Areas => 4,
self::Preparation => 7,
self::Default => 5,
};
}
}
36 changes: 20 additions & 16 deletions app/Http/Livewire/ProfileDataCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,12 @@
use App\Profile;
use Livewire\Component;
use Livewire\WithPagination;
use App\Enums\ProfileSectionType;

class ProfileDataCard extends Component
{
use WithPagination;

const PER_PAGE_FOR_SECTION = [
'default' => 5,
'publications' => 8,
'appointments' => 10,
'awards' => 10,
'news' => 5,
'support' => 5,
'presentations' => 5,
'projects' => 5,
'additionals' => 3,
'affiliations' => 10
];

protected $paginationTheme = 'bootstrap';
public $profile;
public $data_type;
Expand All @@ -40,17 +28,33 @@ public function mount(Profile $profile, $editable, $data_type, $paginated = true
$this->public_filtered = $public_filtered;
}

public function updatingDataType()
{
$this->validateOnly('data_type', [
'data_type' => 'required|in:' . implode(',', ProfileSectionType::values()),
]);
}

public function data()
{
$data_query = $this->profile->{$this->data_type}();
$section = ProfileSectionType::tryFrom($this->data_type);

if (!$section) {
$this->addError('data_type', 'Invalid section.');
$this->emit('alert', 'Invalid section.', 'danger');

return null;
}

$data_query = $this->profile->{$section->value}();

if ($this->public_filtered) {
$data_query = $data_query->public();
}

if ($this->paginated) {
return $data_query->paginate(
$this::PER_PAGE_FOR_SECTION[$this->data_type] ?? $this::PER_PAGE_FOR_SECTION['default'],
$section->perPage(),
['*'],
$this->data_type
);
Expand All @@ -63,7 +67,7 @@ public function render()
{
$data = $this->data();

if ($data->isEmpty() && !$this->editable) {
if ($data === null || ($data->isEmpty() && !$this->editable)) {
return '';
}

Expand Down
60 changes: 46 additions & 14 deletions tests/Feature/Livewire/ProfileDataCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Livewire\Livewire;
use Tests\Feature\Traits\LoginWithRole;
use Tests\TestCase;
use App\Enums\ProfileSectionType;

class ProfileDataCardTest extends TestCase
{
Expand All @@ -31,7 +32,7 @@ class ProfileDataCardTest extends TestCase
*/
public function testPaginatedItemsOnFirstAndLastPage()
{
$sections = [ 'publications', 'presentations', 'projects', 'additionals' ];
$section_options = [ 'publications', 'presentations', 'projects', 'additionals' ];

$profile = Profile::factory()
->hasData()
Expand All @@ -49,39 +50,43 @@ public function testPaginatedItemsOnFirstAndLastPage()
$user = $this->loginAsUser();
$editable = $user && $user->can('update', $profile);

foreach ($sections as $section) {
foreach ($section_options as $section_opt) {

$section = ProfileSectionType::from($section_opt);

$this->assertNotNull($section, 'Invalid section');

$this->assertTrue(
method_exists($profile, $section),
'Profile does not have method '.$section
method_exists($profile, $section->value),
'Profile does not have method '.$section->value
);

$section_data = $profile->$section;
$section_data = $profile->{$section->value};
$data_count = $section_data->count();
$per_page = ProfileDataCard::PER_PAGE_FOR_SECTION[$section];
$per_page = $section->perPage();

$this->assertDatabaseHas('profile_data', ['type' => $section]);
$this->assertDatabaseHas('profile_data', ['type' => $section->value]);
$this->assertIsIterable($section_data);
$this->assertGreaterThanOrEqual($per_page, $data_count);

$component = Livewire::test(ProfileDataCard::class, ['profile' => $profile, 'editable' => $editable, 'data_type' => $section ])
->assertSet('data_type', $section)
$component = Livewire::test(ProfileDataCard::class, ['profile' => $profile, 'editable' => $editable, 'data_type' => $section_opt])
->assertSet('data_type', $section_opt)
->assertViewHas('data')
->assertSeeHtmlInOrder(["<section id=\"$section\" class=\"card\">", '<h3>', '<div class="entry">'] );
->assertSeeHtmlInOrder(["<section id=\"$section_opt\" class=\"card\">", '<h3>', '<div class="entry">']);

if ($section === 'additionals') {
if ($section_opt === 'additionals') {
$component->assertSee('Additional Information');
} else {
$component->assertSee(ucwords($section));
$component->assertSee(ucwords($section->value));
}

$first_page_items_count = $data_count >= $per_page ? $per_page : $data_count;

$this->assertIsIterable($component->lastRenderedView->data);
$this->assertCount($first_page_items_count, $component->lastRenderedView->data);

$component->call('gotoPage', $component->lastRenderedView->data->lastPage(), $section)
->assertSet('data_type', $section)
$component->call('gotoPage', $component->lastRenderedView->data->lastPage(), $section_opt)
->assertSet('data_type', $section_opt)
->assertViewHas('data');

$last_page_items_count = fmod($data_count, $per_page) > 0 ? fmod($data_count, $per_page) : $per_page;
Expand Down Expand Up @@ -150,4 +155,31 @@ public function testPaginatedItemsOnSecondPage()
}
}

/**
* Test profile section type value
*
* @return void
*/
public function testProfileSectionType()
{
$profile = Profile::factory()->hasData()->create();

$section = 'wrong_data_type_value';

$invalid_section = ProfileSectionType::tryFrom($section);

$this->assertNull($invalid_section, 'Invalid section');

$component = Livewire::test(ProfileDataCard::class, ['profile' => $profile, 'editable' => true, 'data_type' => 'news'])
->assertHasNoErrors()
->assertViewIs("livewire.profile-data-cards.news");

$component
->set('data_type', $section)
->assertHasErrors('data_type')
->assertDontSee('data')
->assertEmitted('alert', 'Invalid section.', 'danger')
->assertDontSeeHtml("<section id=\"$section\" class=\"card\">", '<h3>', '<div class="entry">');
}

}
Loading