Skip to content

fix bulk resend#33

Merged
Baspa merged 1 commit intobackstagephp:mainfrom
cntabana:fix/bulk-resend
Mar 19, 2025
Merged

fix bulk resend#33
Baspa merged 1 commit intobackstagephp:mainfrom
cntabana:fix/bulk-resend

Conversation

@cntabana
Copy link
Contributor

Issue Summary:
The current implementation of the bulk resend action form in FilamentPHP does not correctly extract and pass email addresses (to, cc, bcc) from the selected records. Instead, the form opens without pre-filling these fields, requiring manual input.

What This PR Does:
This PR updates the ->form(fn ($records) => self::getBulkResendForm($records)) function to dynamically extract email addresses from the selected records and pass them into the form. This ensures that when the resend form modal appears, the recipient fields (to, cc, bcc) are pre-filled with unique email addresses from the selected records.

Changes & Fixes:
Extracts emails from selected records
Uses array_keys($record->to ?: []), array_keys($record->cc ?: []), and array_keys($record->bcc ?: []) to retrieve email addresses from JSON fields.
Ensures that even if some records do not have cc or bcc, it will not cause errors.

Code Implementation:

->form(fn ($records) => self::getBulkResendForm($records))
private static function getBulkResendForm($records): array
    {
        $extractEmails = function ($records, $field) {
            return collect($records)
                ->map(fn ($record) => array_keys($record->{$field} ?? []))
                ->flatten()
                ->unique()
                ->toArray();
        };


        $toEmails = $extractEmails($records, 'to');
        $ccEmails = $extractEmails($records, 'cc');
        $bccEmails = $extractEmails($records, 'bcc');

        return [
            TagsInput::make('to')
                ->placeholder(__('Recipient(s)'))
                ->label(__('Recipient(s)'))
                ->default($toEmails)
                ->required()
                ->nestedRecursiveRules(['email:rfc,dns']),

            TagsInput::make('cc')
                ->placeholder(__('CC'))
                ->label(__('CC'))
                ->default($ccEmails)
                ->nestedRecursiveRules(['nullable', 'email:rfc,dns']),

            TagsInput::make('bcc')
                ->placeholder(__('BCC'))
                ->label(__('BCC'))
                ->default($bccEmails)
                ->nestedRecursiveRules(['nullable', 'email:rfc,dns']),
        ];
    }

Why This Fix Is Important?
✅ Ensures recipient fields are auto-filled – No need for manual entry.
✅ Fixes the issue of empty form fields – The previous implementation did not correctly pass emails into the form.
✅ Makes the code more maintainable & efficient – Avoids unnecessary repetition when extracting email addresses.
✅ Enhances user experience – Users can now bulk resend emails without extra effort.

@Baspa
Copy link
Contributor

Baspa commented Mar 19, 2025

Great, thanks @cntabana! Added in v2.3.3.

@Baspa Baspa merged commit 167b918 into backstagephp:main Mar 19, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants