Skip to content

Commit

Permalink
fix: hidden field issues are fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sriramkanakam87 committed Nov 15, 2024
1 parent bf48f25 commit 8d224dd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
40 changes: 29 additions & 11 deletions app/Filament/Dashboard/Resources/ReportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function form(Form $form): Form
])
->inline()
->hidden(function (string $operation, $get) {
return $get('type') == 'change' ? true : false;
return $operation == 'create' ? $get('type') != null : true;
})
->columnSpan(2),
Actions::make([
Expand All @@ -82,11 +82,23 @@ public static function form(Form $form): Form
if ($record['is_change']) {
self::$approved_changes = self::prepareApprovedChanges($record, $livewire);
$key_value_fields = getChangesToDisplayModal(self::$approved_changes);
array_unshift($key_value_fields, Textarea::make('reason'));
array_unshift($key_value_fields,
Textarea::make('reason')
->helperText(function ($get) {
if (count(self::$approved_changes) <= 1) {
return new HtmlString('<span style="color:red"> You must select at least one change to approve </span>');
} else {
return null;
}
})
->disabled(count(self::$approved_changes) <= 1)
);

return $key_value_fields;
} else {
return [Textarea::make('reason')];
return [
Textarea::make('reason'),
];
}
})
->hidden(function (Get $get, string $operation) {
Expand All @@ -95,6 +107,11 @@ public static function form(Form $form): Form
->action(function (array $data, Report $record, Molecule $molecule, $set, $livewire): void {
self::approveReport($data, $record, $molecule, $livewire);
$set('status', 'approved');
})
->modalSubmitAction(function () {
if (count(self::$approved_changes) <= 1) {
return false;
}
}),
Action::make('reject')
->color('danger')
Expand All @@ -113,7 +130,7 @@ public static function form(Form $form): Form
->url(fn (string $operation, $record): string => $operation === 'create' ? env('APP_URL').'/compounds/'.request()->compound_id : env('APP_URL').'/compounds/'.$record->mol_id_csv)
->openUrlInNewTab()
->hidden(function (Get $get, string $operation) {
return ! request()->has('type');
return ! $get('type');
}),
])
->hidden(function (Get $get) {
Expand All @@ -131,13 +148,13 @@ public static function form(Form $form): Form
->options(function () {
return getReportTypes();
})
->hidden(function (string $operation) {
return $operation != 'create' || request()->has('type');
->hidden(function (string $operation, $get) {
return $operation != 'create' || $get('type');
}),
TextInput::make('title')
->hintIcon('heroicon-m-question-mark-circle', tooltip: 'Title of the report. This is required.')
->default(function () {
if (request()->type == 'change' && request()->has('compound_id')) {
->default(function ($get) {
if ($get('type') == 'change' && request()->has('compound_id')) {
return 'Request changes to '.request()->compound_id;
}
})
Expand Down Expand Up @@ -393,8 +410,9 @@ public static function form(Form $form): Form
shouldOpenInNewTab: true,
),
)
->hidden(function (string $operation, $record) {
return $operation == 'create' && request()->has('type') && request()->type == 'change' ? true : false;
->hidden(function (string $operation, $get, ?Report $record) {
return true;
// return $operation == 'create' && $get('type') && $get('type') == 'change' ? true : false;
}),
Select::make('collections')
->hintIcon('heroicon-m-question-mark-circle', tooltip: 'Select the Collections you want to report. This will help our Curators in reviewing your report.')
Expand Down Expand Up @@ -483,7 +501,7 @@ public static function form(Form $form): Form
})
->live()
->hidden(function (Get $get, string $operation) {
if ($operation != 'create' || request()->has('type')) {
if ($operation != 'create' || $get('type')) {
return true;
}
if (! request()->has('compound_id') && $get('report_type') != 'molecule') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class CreateReport extends CreateRecord

protected $molecule;

protected $is_change = false;

protected $mol_id_csv = null;

protected $report_type = null;

protected $evidence = null;

public function getTitle(): string
{
$title = 'Create Report';
Expand All @@ -30,6 +38,8 @@ public function getTitle(): string

protected function beforeFill(): void
{
$this->data['type'] = request()->type;

if (request()->has('compound_id')) {
$this->molecule = Molecule::where('identifier', request()->compound_id)->first();
}
Expand All @@ -39,10 +49,12 @@ protected function afterFill(): void
{
$request = request();
$this->data['compound_id'] = $request->compound_id;
$this->data['type'] = $request->type;

if ($request->type == 'change') {
$this->data['type'] = $request->type;
$this->data['is_change'] = true;
$this->is_change = true;

$this->data['existing_geo_locations'] = $this->molecule->geo_locations->pluck('name')->toArray();
$this->data['existing_synonyms'] = $this->molecule->synonyms;
$this->data['existing_cas'] = array_values($this->molecule->cas ?? []);
Expand Down Expand Up @@ -73,6 +85,14 @@ protected function afterFill(): void
}
}

protected function afterValidate(): void
{
$this->mol_id_csv = $this->data['mol_id_csv'];
$this->is_change = $this->data['is_change'];
$this->report_type = $this->data['report_type'];
$this->evidence = $this->data['evidence'];
}

protected function beforeCreate(): void
{
if ($this->data['report_type'] == 'collection') {
Expand All @@ -98,6 +118,11 @@ protected function mutateFormDataBeforeCreate(array $data): array
{
$data['user_id'] = auth()->id();
$data['status'] = 'submitted';
$data['mol_id_csv'] = $this->mol_id_csv;
$data['is_change'] = $this->is_change;
$data['report_type'] = $this->report_type;
$data['evidence'] = $this->evidence;

if ($data['is_change']) {
$suggested_changes = [];
$suggested_changes['existing_geo_locations'] = $data['existing_geo_locations'];
Expand Down

0 comments on commit 8d224dd

Please sign in to comment.