Skip to content

Commit

Permalink
Merge pull request #702 from waynestate/feature/move-modular-promos-2
Browse files Browse the repository at this point in the history
Feature/move modular promos 2 - Making components global
  • Loading branch information
breakdancingcat committed Apr 29, 2024
2 parents b7f707d + 4270038 commit 68a7313
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 105 deletions.
34 changes: 1 addition & 33 deletions app/Http/Controllers/ChildpageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,14 @@

use Illuminate\View\View;
use Illuminate\Http\Request;
use Contracts\Repositories\ModularPageRepositoryContract;

class ChildpageController extends Controller
{
/**
* Construct the controller.
*
*/
public function __construct(
ModularPageRepositoryContract $components
) {
$this->components = $components;
}

/**
* Display the childpage view.
*/
public function index(Request $request): View
{
$components['components'] = [];

if(!empty($request->data['base']['data'])) {
$components['components'] = $this->components->getModularComponents($request->data['base']);

// Set hero from components
$hero = collect($components['components'])->reject(function ($data, $component_name) {
return !str_contains($component_name, 'hero');
})->toArray();
}

if(!empty($hero)) {
$hero_key = array_key_first($hero);

$request->data['base']['hero'] = $components['components'][$hero_key]['data'];

config(['base.hero_full_controllers' => ['ChildpageController']]);

unset($components['components'][$hero_key]);
}

return view('childpage', merge($request->data, $components));
return view('childpage', merge($request->data));
}
}
29 changes: 25 additions & 4 deletions app/Repositories/PromoRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Contracts\Repositories\RequestDataRepositoryContract;
use Contracts\Repositories\PromoRepositoryContract;
use Contracts\Repositories\ModularPageRepositoryContract;
use Illuminate\Cache\Repository;
use Waynestate\Api\Connector;
use Waynestate\Promotions\ParsePromos;
Expand All @@ -22,11 +23,16 @@ class PromoRepository implements RequestDataRepositoryContract, PromoRepositoryC
/**
* Construct the repository.
*/
public function __construct(Connector $wsuApi, ParsePromos $parsePromos, Repository $cache)
{
public function __construct(
Connector $wsuApi,
ParsePromos $parsePromos,
Repository $cache,
ModularPageRepositoryContract $components
) {
$this->wsuApi = $wsuApi;
$this->parsePromos = $parsePromos;
$this->cache = $cache;
$this->components = $components;
}

/**
Expand Down Expand Up @@ -117,7 +123,7 @@ public function getRequestData(array $data)

$promos = $this->parsePromos->parse($promos, $group_reference, $group_config);

$global_promos = $this->manipulateGlobalPromos($promos, $groups);
$global_promos = $this->manipulateGlobalPromos($promos, $groups, $data);

return $global_promos;
}
Expand Down Expand Up @@ -164,7 +170,7 @@ public function createGlobalPromoGroupConfig(array $data, array $config, array $
/**
* {@inheritdoc}
*/
public function manipulateGlobalPromos(array $promos, array $groups)
public function manipulateGlobalPromos(array $promos, array $groups, array $data)
{
// Override the site's social icons if it doesn't have any
if (empty($promos['social']) && !empty($promos['main_social'])) {
Expand All @@ -191,6 +197,21 @@ public function manipulateGlobalPromos(array $promos, array $groups)
unset($promos['main_social']);
unset($promos['main_contact']);

// Add modular components into global data
$promos['components'] = $this->components->getModularComponents($data);

// Set hero from components
$hero = collect($promos['components'])->reject(function ($data, $component_name) {
return !str_contains($component_name, 'hero');
})->toArray();

if(!empty($hero)) {
$hero_key = array_key_first($hero);
$promos['hero'] = $promos['components'][$hero_key]['data'];
config(['base.hero_full_controllers' => $data['page']['controller']]);
unset($promos['components'][$hero_key]);
}

return $promos;
}
}
2 changes: 1 addition & 1 deletion contracts/Repositories/PromoRepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public function createGlobalPromoGroupConfig(array $data, array $config, array $
/**
* {@inheritdoc}
*/
public function manipulateGlobalPromos(array $promos, array $groups);
public function manipulateGlobalPromos(array $promos, array $groups, array $data);
}
6 changes: 3 additions & 3 deletions resources/views/childpage.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
@section('content')
@include('components.page-title', ['title' => $base['page']['title']])

@if(empty($components['page-content-row']) && empty($components['page-content-column']))
@if(empty($base['components']['page-content-row']) && empty($base['components']['page-content-column']))
<div class="content">
{!! $base['page']['content']['main'] !!}
</div>
@endif

@if(!empty($components))
@if(!empty($base['components']))
<div class="grid grid-cols-1 md:grid-cols-2 items-start gap-y-8 sm:gap-x-4 lg:gap-x-8 mt-8 mb-4">
@foreach($components as $componentName => $component)
@foreach($base['components'] as $componentName => $component)
@if(!empty($component['data']) && !empty($component['component']['filename']))
@if(\View::exists('components/'.$component['component']['filename']))
<div class="col-span-2 {{ str_contains($component['component']['filename'], 'column') ? 'md:col-span-1' : 'md:col-span-2' }}">
Expand Down
6 changes: 3 additions & 3 deletions resources/views/layouts/contained-hero.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
<main class="w-full {{$base['show_site_menu'] === true ? 'mt:w-3/4' : '' }} content-area mb-8 print:w-full" tabindex="-1">

@if(!empty($base['hero']) && !in_array($base['page']['controller'], config('base.hero_full_controllers')))
@include('components.hero', ['data' => $base['hero']])

@yield('under-hero')
<div class="mt:px-4">
@include('components.hero', ['data' => $base['hero']])
</div>
@endif

@if(!empty($base['breadcrumbs']))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Repositories;

use Contracts\Repositories\PromosExtendedRepositoryContract;
use Contracts\Repositories\PromoExtendedRepositoryContract;

class PromosExtendedRepository extends PromoRepository implements PromosExtendedRepositoryContract
class PromoExtendedRepository extends PromoRepository implements PromoExtendedRepositoryContract
{
/**
* {@inheritdoc}
Expand Down
21 changes: 21 additions & 0 deletions stubs/PromoExtendedRepositoryContract.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Contracts\Repositories;

interface PromoExtendedRepositoryContract
{
/**
* {@inheritdoc}
*/
public function createGlobalPromoGroupReference(array $data, array $config, array $groups);

/**
* {@inheritdoc}
*/
public function createGlobalPromoGroupConfig(array $data, array $config, array $groups);

/**
* {@inheritdoc}
*/
public function manipulateGlobalPromos(array $promos, array $groups);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Styleguide\Repositories;

use App\Repositories\PromosExtendedRepository as Repository;
use App\Repositories\PromoExtendedRepository as Repository;
use Faker\Factory;
use Factories\GenericPromo;

class PromosExtendedRepository extends Repository
class PromoExtendedRepository extends Repository
{
/**
* Construct the factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function index(Request $request): View
</tbody>
</table>
';
$components['components'] = [
$request->data['base']['components'] = [
'accordion' => [
'data' => [
0 => [
Expand Down Expand Up @@ -282,23 +282,23 @@ public function index(Request $request): View
]
];

if(!empty($components['components'])) {
if(!empty($request->data['base']['components'])) {
// Set hero from components
$hero = collect($components['components'])->reject(function ($data, $component_name) {
$hero = collect($request->data['base']['components'])->reject(function ($data, $component_name) {
return !str_contains($component_name, 'hero');
})->toArray();
}

if(!empty($hero)) {
$hero_key = array_key_first($hero);

$request->data['base']['hero'] = $components['components'][$hero_key]['data'];
$request->data['base']['hero'] = $request->data['base']['components'][$hero_key]['data'];

unset($components['components'][$hero_key]);
unset($request->data['base']['components'][$hero_key]);

config(['base.hero_full_controllers' => ['ChildpageWithComponentsController']]);
config(['base.hero_full_controllers' => [$request->data['base']['page']['controller']]]);
}

return view('childpage', merge($request->data, $components));
return view('childpage', merge($request->data));
}
}
50 changes: 0 additions & 50 deletions tests/Unit/Http/Controllers/ChildpageControllerTest.php

This file was deleted.

45 changes: 45 additions & 0 deletions tests/Unit/Repositories/PromoRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,49 @@ public function restrict_rotating_hero(): void
$this->assertEquals($group_config['hero'], '|limit:'.config('base.hero_rotating_limit'));
$this->assertTrue((in_array($data['page']['controller'], config('base.hero_rotating_controllers'))));
}

#[Test]
public function replace_global_hero_with_component_hero(): void
{
$page_id = $this->faker->numberbetween(10, 50);
$promo_group_id = $this->faker->numberbetween(1, 3);

// Fake return
$return['promotions'] = app(GenericPromo::class)->create(1, false, [
'promo_group_id' => $promo_group_id,
'page_id' => $page_id,
'group' => [
'promo_group_id' => $promo_group_id,
],
]);

// Create a fake data request
$data = app(Page::class)->create(1, true, [
'page' => [
'controller' => 'ChildpageController',
'id' => $page_id,
],
'hero' => app(GenericPromo::class)->create(2, false),
'data' => [
'modular-hero-1' => json_encode([
'id' => $promo_group_id,
'config' => 'limit:2',
'columns' => '',
'singlePromoView' => true,
'showExcerpt' => true,
'showDescription' => true,
]),
],
]);

// Mock the connector and set the return
$wsuApi = Mockery::mock(Connector::class);
$wsuApi->shouldReceive('sendRequest')->with('cms.promotions.listing', Mockery::type('array'))->once()->andReturn($return);

// Get the promos
$promos = app(PromoRepository::class, ['wsuApi' => $wsuApi])->getRequestData($data);

// Assert that the (empty) modular hero component replaces the global hero
$this->assertTrue($promos['hero'] == []);
}
}

0 comments on commit 68a7313

Please sign in to comment.