Skip to content

Commit

Permalink
Merge pull request #396 from code4romania/fix-social-sheet
Browse files Browse the repository at this point in the history
Fix social sheet
  • Loading branch information
gheorghelupu17 authored Dec 10, 2024
2 parents fe50111 + da5877f commit 97f6406
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use App\Forms\Components\DatePicker;
use App\Forms\Components\Repeater;
use App\Forms\Components\Select;
use App\Models\Children;
use App\Models\InterventionService;
use App\Services\Breadcrumb\InterventionPlanBreadcrumb;
use Awcodes\FilamentTableRepeater\Components\TableRepeater;
Expand Down Expand Up @@ -479,44 +478,36 @@ public static function getSchemaForSocialAssistance(?InterventionService $interv
]),

Section::make(__('intervention_plan.headings.children_details'))
->visible(fn () => ! $interventionService || $interventionService?->beneficiary->children->count())
->headerActions([
Action::make('view_children_identity')
->label(__('intervention_plan.actions.view_children_identity'))
->icon('heroicon-o-arrow-top-right-on-square')
->url(fn () => BeneficiaryResource::getUrl('view_identity', [
'record' => $interventionService->beneficiary,
'tab' => \sprintf('-%s-tab', Str::slug(__('beneficiary.section.identity.tab.children'))),
]))
->visible(fn () => $interventionService)
->openUrlInNewTab()
->link(),
])
->afterStateHydrated(function (Set $set, array $state) use ($interventionService) {
if (! $interventionService) {
return;
}
$childrenState = $state['data']['children'];
$beneficiaryChildren = $interventionService->beneficiary->children;

$emptyState = true;
foreach ($childrenState as &$childState) {
$childState = array_filter($childState);
if (\count($childState) > 1) {
$emptyState = false;
$beneficiaryChild = $beneficiaryChildren->filter(fn (Children $child) => $child->id === $childState['id'])
->first()
?->toArray() ??
[];

$childState = array_merge($childState, $beneficiaryChild);
}
}

if ($emptyState) {
$set('data.children', $beneficiaryChildren->toArray());
$childrenState = $state['data']['children'] ?? [];
$childrenState = collect($childrenState);
$beneficiaryChildren = $interventionService->beneficiary->children;

return;
$children = [];
foreach ($beneficiaryChildren as $child) {
$childState = $childrenState->filter(fn (array $childState) => isset($childState['id']) && $childState['id'] === $child->id)
->first() ?? [];
$children[] = array_merge($child->toArray(), $childState);
}

$set('data.children', $childrenState);
$set('data.children', $children);
})
->schema([
Repeater::make('data.children')
Expand All @@ -526,7 +517,7 @@ public static function getSchemaForSocialAssistance(?InterventionService $interv
->addAction(fn (Action $action) => $action->hidden())
->schema([
Grid::make()
->columns(7)
->columns(15)
->schema([
Placeholder::make('count')
->label(__('intervention_plan.labels.count'))
Expand All @@ -538,6 +529,7 @@ public static function getSchemaForSocialAssistance(?InterventionService $interv

TextInput::make('name')
->label(__('intervention_plan.labels.children_name'))
->columnSpan(3)
->disabled(),

TextInput::make('age')
Expand All @@ -551,14 +543,17 @@ public static function getSchemaForSocialAssistance(?InterventionService $interv

TextInput::make('current_address')
->label(__('field.current_address'))
->columnSpan(3)
->disabled(),

TextInput::make('status')
->label(__('field.child_status'))
->columnSpan(3)
->disabled(),

TextInput::make('workspace')
->label(__('field.workspace'))
->columnSpan(3)
->disabled(),
]),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Enums\ProtectionMeasuringType;
use App\Enums\SocialRelationship;
use App\Enums\Ternary;
use App\Filament\Organizations\Resources\BeneficiaryResource;
use App\Filament\Organizations\Resources\InterventionPlanResource;
use App\Infolists\Components\EnumEntry;
use App\Infolists\Components\SectionHeader;
Expand All @@ -27,6 +28,7 @@
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Str;

class CounselingSheetWidget extends InfolistWidget
{
Expand Down Expand Up @@ -268,7 +270,7 @@ private function getSchemaForPsychologicalAssistance(): array
];
}

public static function getSchemaForSocialAssistance(): array
public function getSchemaForSocialAssistance(): array
{
return [
Section::make(__('intervention_plan.headings.family_relationship'))
Expand Down Expand Up @@ -377,12 +379,24 @@ public static function getSchemaForSocialAssistance(): array
]),

Section::make(__('intervention_plan.headings.children_details'))
->visible(fn () => $this->record->beneficiary->children->count())
->headerActions([
Action::make('view_children_identity')
->label(__('intervention_plan.actions.view_children_identity'))
->icon('heroicon-o-arrow-top-right-on-square')
->url(fn () => BeneficiaryResource::getUrl('view_identity', [
'record' => $this->record->beneficiary,
'tab' => \sprintf('-%s-tab', Str::slug(__('beneficiary.section.identity.tab.children'))),
]))
->openUrlInNewTab()
->link(),
])
->schema([
RepeatableEntry::make('data.children')
->hiddenLabel()
->schema([
Grid::make()
->columns(7)
->columns(15)
->schema([
TextEntry::make('count')
->label(__('intervention_plan.labels.count'))
Expand All @@ -398,7 +412,8 @@ public static function getSchemaForSocialAssistance(): array
$index = (int) explode('.', $component->getStatePath())[3];

return $record->beneficiary->children->get($index)->name;
}),
})
->columnSpan(3),

TextEntry::make('age')
->label(__('intervention_plan.labels.children_age'))
Expand All @@ -423,23 +438,26 @@ public static function getSchemaForSocialAssistance(): array
$index = (int) explode('.', $component->getStatePath())[3];

return $record->beneficiary->children->get($index)->current_address;
}),
})
->columnSpan(3),

TextEntry::make('status')
->label(__('field.child_status'))
->state(function (TextEntry $component, $record) {
$index = (int) explode('.', $component->getStatePath())[3];

return $record->beneficiary->children->get($index)->status;
}),
})
->columnSpan(3),

TextEntry::make('workspace')
->label(__('field.workspace'))
->state(function (TextEntry $component, $record) {
$index = (int) explode('.', $component->getStatePath())[3];

return $record->beneficiary->children->get($index)->workspace;
}),
})
->columnSpan(3),
]),

Grid::make()
Expand Down

0 comments on commit 97f6406

Please sign in to comment.