Skip to content

Commit

Permalink
Adapt react print to changes in ecamp#2571
Browse files Browse the repository at this point in the history
Fixes the open todo from
ecamp#2571 (comment)
  • Loading branch information
carlobeltrame committed Jul 5, 2022
1 parent b71b6a6 commit 59ac661
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 17 deletions.
17 changes: 17 additions & 0 deletions api/src/Entity/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ public function removeScheduleEntry(ScheduleEntry $scheduleEntry): self {
return $this;
}

/**
* All the content nodes used in some activity which is carried out (has a schedule entry) in this period.
*
* @return ContentNode[]
*/
#[ApiProperty(writable: false, example: '["/content_nodes/1a2b3c4d"]')]
#[RelatedCollectionLink(ContentNode::class, ['period' => '$this'])]
#[Groups(['read'])]
public function getContentNodes(): array {
return array_values(array_unique(array_merge(...array_map(
function (ScheduleEntry $scheduleEntry) {
return $scheduleEntry->activity->getRootContentNode()->getRootDescendants();
},
$this->getScheduleEntries()
)), SORT_REGULAR));
}

/**
* @return MaterialItem[]
*/
Expand Down
40 changes: 40 additions & 0 deletions api/tests/Entity/PeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace App\Tests\Entity;

use App\Entity\Activity;
use App\Entity\Camp;
use App\Entity\ContentNode\ColumnLayout;
use App\Entity\ContentNode\SingleText;
use App\Entity\Day;
use App\Entity\Period;
use App\Entity\ScheduleEntry;
use DateTime;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -33,4 +37,40 @@ public function testFirstDayNumberInPeriod() {
$this->assertEquals(3, $period1->getFirstDayNumber());
$this->assertEquals(1, $period2->getFirstDayNumber());
}

public function testGetContentNodes() {
$camp = new Camp();

$period = new Period();
$period->start = new DateTime('2020-09-14');
$camp->addPeriod($period);
$scheduleEntry1 = new ScheduleEntry();
$scheduleEntry2 = new ScheduleEntry();
$scheduleEntry3 = new ScheduleEntry();
$period->addScheduleEntry($scheduleEntry1);
$period->addScheduleEntry($scheduleEntry2);
$period->addScheduleEntry($scheduleEntry3);

$activity1 = new Activity();
$columnLayout1 = new ColumnLayout();
$singleText1 = new SingleText();
$columnLayout1->addRootDescendant($singleText1);
$columnLayout1->addChild($singleText1);
$activity1->setRootContentNode($columnLayout1);
$scheduleEntry1->activity = $activity1;
$scheduleEntry2->activity = $activity1;

$activity2 = new Activity();
$columnLayout2 = new ColumnLayout();
$singleText2 = new SingleText();
$columnLayout2->addRootDescendant($singleText2);
$columnLayout2->addChild($singleText2);
$activity2->setRootContentNode($columnLayout2);
$scheduleEntry3->activity = $activity2;

$this->assertEquals(
[$singleText1, $columnLayout1, $singleText2, $columnLayout2],
$period->getContentNodes()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ function ColumnLayout(props) {
const columns = props.contentNode.columns
const firstSlot = columns.length ? columns[0].slot : '1'
const lastSlot = columns.length ? columns[columns.length - 1].slot : '1'
const children = props.allContentNodes.items.filter((contentNode) => {
return (
contentNode.parent &&
contentNode.parent()._meta.self === props.contentNode._meta.self
)
})
const children = props.contentNode.children().items

return (
<View style={{ display: 'flex', flexDirection: 'row' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const picassoData = (config) => {
.then((activities) => {
return Promise.all(
activities.items.map((activity) => {
return Promise.all([
activity.activityResponsibles().$loadItems(),
activity.contentNodes().$loadItems(),
])
return activity.activityResponsibles().$loadItems()
})
)
}),
Expand All @@ -35,7 +32,10 @@ const picassoData = (config) => {
.then((periods) => {
return Promise.all(
periods.items.map((period) => {
return period.scheduleEntries().$loadItems()
return Promise.all([
period.scheduleEntries().$loadItems(),
period.contentNodes().$loadItems()
])
})
)
}),
Expand All @@ -58,10 +58,7 @@ const activityData = (config) => {
.then((activities) => {
return Promise.all(
activities.items.map((activity) => {
return Promise.all([
activity.activityResponsibles().$loadItems(),
activity.contentNodes().$loadItems(),
])
return activity.activityResponsibles().$loadItems()
})
)
}),
Expand All @@ -79,7 +76,10 @@ const activityData = (config) => {
.then((periods) => {
return Promise.all(
periods.items.map((period) => {
return period.scheduleEntries().$loadItems()
return Promise.all([
period.scheduleEntries().$loadItems(),
period.contentNodes().$loadItems()
])
})
)
}),
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/components/print/print-react/minimalHalJsonVuex.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function isCollection(object) {

function isEntityReference(object) {
if (!object) return false
return isEqualIgnoringOrder(Object.keys(object), ['href'])
return isEqualIgnoringOrder(Object.keys(object), ['href']) || isVirtualLink(object)
}

function isTemplatedLink(object) {
Expand All @@ -21,6 +21,14 @@ function isTemplatedLink(object) {
)
}

function isVirtualLink(object) {
if (!object) return false
return (
isEqualIgnoringOrder(Object.keys(object), ['href', 'virtual']) &&
object.virtual === true
)
}

function normalizeEntityUri(uriOrEntity) {
if (typeof uriOrEntity === 'function') {
uriOrEntity = uriOrEntity()
Expand Down

0 comments on commit 59ac661

Please sign in to comment.