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
27 changes: 27 additions & 0 deletions src/Commands/SheafInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Sheaf\Cli\Services\PackageInitializationService;
use Sheaf\Cli\Support\InitializationConfig;
use Illuminate\Console\Command;
use Sheaf\Cli\Services\ComponentHttpClient;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\text;
Expand Down Expand Up @@ -64,6 +65,7 @@ public function handle()
if ($result) {
$this->displaySuccess($configuration);
$this->displayNextStep($configuration);
$this->subscribeToNewsletter();
return Command::SUCCESS;
}

Expand Down Expand Up @@ -206,6 +208,31 @@ protected function validateCssFileName(?string $input): ?string
return null;
}

/**
* Ask user to subscribe to the news letter
*/
protected function subscribeToNewsletter()
{

$email = text(
label: 'Get notified when we release new components',
placeholder: 'your.email@example.com',
hint: 'No spam, just useful updates',
validate: fn($input) => $input && !filter_var($input, FILTER_VALIDATE_EMAIL)
? 'Please enter a valid email address'
: null
);

if (empty($email)) {
return;
}
try {
new ComponentHttpClient()->subscribeUserToNewsletter($email);
$this->info("✓ Thanks for subscribing! We'll keep you updated with new components and features.");
} catch (\Throwable $th) {
$this->warn("We couldn't subscribe your email right now, but you can always subscribe later at https://sheafui.dev");
}
}

/**
* Display the Sheaf package banner
Expand Down
18 changes: 16 additions & 2 deletions src/Services/ComponentHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct()
$this->token = SheafConfig::getUserToken();
}

public function fetchComponentFilesPath(string $name) {
public function fetchComponentFilesPath(string $name)
{
$url = "{$this->baseUrl}/api/cli/components/$name/files";

$response = Http::asJson()->get($url);
Expand All @@ -33,7 +34,6 @@ public function fetchComponentFilesPath(string $name) {
'success' => true,
'data' => $response->collect()
];

}
public function fetchResources(string $componentName)
{
Expand All @@ -59,4 +59,18 @@ public function fetchResources(string $componentName)
];
}

public function subscribeUserToNewsletter(string $email)
{
try {
$url = "{$this->baseUrl}/api/cli/newsletter/subscribe?email={$email}";

$result = Http::asJson()->get($url);

if (!$result['success']) {
throw new Exception("Failed to subscribe to the news letter");
}
} catch (\Throwable $th) {
throw new Exception("Failed to subscribe to the news letter");
}
}
}
2 changes: 2 additions & 0 deletions tests/Feature/Commands/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
$command->expectsQuestion("The $mainJsFile is not exists, do you want to create it?", true);
}

$command->expectsQuestion("Get notified when we release new components", null);

$command->assertExitCode(0);

$command->run();
Expand Down