Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion resources/dist/build/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"resources/js/addon.js": {
"file": "assets/addon-Dnt1-tBd.js",
"file": "assets/addon-54LLqdMz.js",
"name": "addon",
"src": "resources/js/addon.js",
"isEntry": true
Expand Down
4 changes: 2 additions & 2 deletions resources/js/addon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MailerLiteFormFieldsFieldtype from './components/fieldtypes/MailerLiteFormFieldsFieldtype.vue';
import FormFieldsFieldtype from './components/fieldtypes/FormFieldsFieldtype.vue';

Statamic.booting(() => {
Statamic.$components.register('mailer_lite_form_fields-fieldtype', MailerLiteFormFieldsFieldtype);
Statamic.$components.register('form_fields-fieldtype', FormFieldsFieldtype);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Statamic\Fields\Field;
use Statamic\Fields\Fieldtype;

class MailerLiteFormFields extends Fieldtype
class FormFields extends Fieldtype
{
protected $selectable = false;

Expand Down
8 changes: 0 additions & 8 deletions src/Fieldtypes/MailerLiteFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ class MailerLiteFields extends Relationship

protected $indexComponent = 'text';

protected $canCreate = false;

protected $canEdit = false;

protected $canSearch = false;

protected $statusIcons = false;

/** @var array<string, array<int, array{id: string, title: string}>> */
private static array $cache = [];

Expand Down
76 changes: 35 additions & 41 deletions src/Fieldtypes/MailerLiteGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,17 @@ class MailerLiteGroups extends Relationship

protected $indexComponent = 'text';

/** @var array<string, array<int, array{id: string, title: string}>> */
private static array $cache = [];

protected function toItemArray($id)
{
$mailerLite = $this->mailerLite();

if (! $mailerLite) {
return $this->invalidItemArray($id);
}

$response = $mailerLite->groups->find($id);

if (! isset($response['body']['data'])) {
return $this->invalidItemArray($id);
}

$group = $response['body']['data'];

return [
'id' => $group['id'],
'title' => $group['name'],
];
return collect($this->groups())->firstWhere('id', $id) ?? $this->invalidItemArray($id);
}

public function getIndexItems($request)
{
$mailerLite = $this->mailerLite();

if (! $mailerLite) {
return collect();
}

$response = $mailerLite->groups->get([
'limit' => 100,
'sort' => 'name',
]);

if (! isset($response['body']['data'])) {
return collect();
}

return collect($response['body']['data'])->map(fn (array $group) => [
'id' => $group['id'],
'title' => $group['name'],
]);
return collect($this->groups());
}

protected function getColumns()
Expand All @@ -78,14 +46,40 @@ public function preProcessIndex($data)
})->join(', ');
}

private function mailerLite(): ?MailerLite
/**
* The selectable MailerLite groups, memoized per request so resolving
* selected values does not hit the API once per value.
*
* @return array<int, array{id: string, title: string}>
*/
private function groups(): array
{
$apiKey = Addon::get('concept7/statamic-mailerlite')->setting('api_key');

if (! $apiKey) {
return null;
if (blank($apiKey)) {
return [];
}

$cacheKey = hash('sha256', $apiKey);

if (isset(self::$cache[$cacheKey])) {
return self::$cache[$cacheKey];
}

return new MailerLite(['api_key' => $apiKey]);
return self::$cache[$cacheKey] = rescue(function () use ($apiKey): array {
$response = (new MailerLite(['api_key' => $apiKey]))
->groups
->get([
'limit' => 100,
'sort' => 'name',
]);

return collect($response['body']['data'] ?? [])
->map(fn (array $group): array => [
'id' => $group['id'],
'title' => $group['name'],
])
->all();
}, [], report: false);
}
}
8 changes: 7 additions & 1 deletion src/StatamicMailerLiteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,24 @@ public static function configFields(): array
'instructions' => __('Subscribers will be added to this MailerLite group.'),
'mode' => 'select',
'max_items' => 1,
'show_when' => [
'mailerlite_enabled' => true,
],
],
'mailerlite_field_mapping' => [
'type' => 'grid',
'mode' => 'table',
'display' => __('Field mapping'),
'instructions' => __('Map each form field to a MailerLite subscriber field.'),
'add_row' => __('Add mapping'),
'show_when' => [
'mailerlite_enabled' => true,
],
'fields' => [
[
'handle' => 'form_field',
'field' => [
'type' => 'mailer_lite_form_fields',
'type' => 'form_fields',
'display' => __('Form field'),
],
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Concept7\StatamicMailerLite\Fieldtypes\MailerLiteFormFields;
use Concept7\StatamicMailerLite\Fieldtypes\FormFields;
use Illuminate\Routing\Route;
use Statamic\Contracts\Forms\Form as FormContract;
use Statamic\Facades\Blueprint;
Expand All @@ -19,7 +19,7 @@

request()->setRouteResolver(fn (): Route => $route);

$options = (new MailerLiteFormFields)->preload()['options'];
$options = (new FormFields)->preload()['options'];

expect(collect($options)->pluck('value')->all())->toBe([
'email',
Expand All @@ -32,7 +32,7 @@
});

it('preloads an empty option list when the route has no form', function () {
expect((new MailerLiteFormFields)->preload()['options'])->toBe([]);
expect((new FormFields)->preload()['options'])->toBe([]);
});

it('preloads an empty option list when a form handle cannot be resolved', function () {
Expand All @@ -41,5 +41,5 @@

request()->setRouteResolver(fn (): Route => $route);

expect((new MailerLiteFormFields)->preload()['options'])->toBe([]);
expect((new FormFields)->preload()['options'])->toBe([]);
});
10 changes: 9 additions & 1 deletion tests/Feature/MailerLiteConfigFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@
$formField = collect($fields['mailerlite_field_mapping']['fields'])
->firstWhere('handle', 'form_field')['field'];

expect($formField['type'])->toBe('mailer_lite_form_fields');
expect($formField['type'])->toBe('form_fields');
});

it('only shows the group and field mapping when sync is enabled', function () {
$fields = StatamicMailerLiteServiceProvider::configFields();

expect($fields['mailerlite_enabled'])->not->toHaveKey('show_when');
expect($fields['mailerlite_group']['show_when'])->toBe(['mailerlite_enabled' => true]);
expect($fields['mailerlite_field_mapping']['show_when'])->toBe(['mailerlite_enabled' => true]);
});
13 changes: 13 additions & 0 deletions tests/Feature/MailerLiteGroupsFieldtypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Concept7\StatamicMailerLite\Fieldtypes\MailerLiteGroups;
use Statamic\Facades\Addon;

it('returns no groups when no api key is configured', function () {
$addon = Mockery::mock();
$addon->shouldReceive('setting')->with('api_key')->andReturn(null);

Addon::shouldReceive('get')->with('concept7/statamic-mailerlite')->andReturn($addon);

expect((new MailerLiteGroups)->getIndexItems(request()))->toBeEmpty();
});