Adding Per-Generation Auto-Publish Toggles to a Post Generator
Adding control over auto-publishing directly within a post generator form can significantly enhance user experience. This post details how we implemented per-generation toggles for platforms like LinkedIn and Dev.to, providing users with granular control over their content distribution.
The Problem
Previously, auto-publishing settings were managed at the user preference level. This meant that every post generated would automatically be published (or not) based on these global settings. This lacked flexibility for users who sometimes wanted to generate content without immediately publishing it to all connected platforms.
The Solution
We've enhanced our Post Generator form to include toggle switches for each supported platform (e.g., LinkedIn, Dev.to). This allows users to decide, on a per-generation basis, whether a post should be automatically published to each platform. Here's how it works:
- New Toggle Switches: The Post Generator form now includes toggle switches for each platform. These switches control auto-publishing for the current post generation.
- Integration Status: When an integration (e.g., a connection to LinkedIn) is not yet configured, a "Connect" button is displayed instead of the toggle. This button links directly to the Integrations page, streamlining the setup process.
- Job Handling: The background job responsible for post generation (
GeneratePostJob) now utilizes the toggle values provided directly from the form. This ensures that the user's explicit choices are respected. For backward compatibility, the job falls back to the user's global preferences if the form values are not provided (though this should ideally be phased out).
Illustrative Code Example (PHP)
Here's an example of how the toggle values might be handled within the GeneratePostJob:
<?php
namespace App\Jobs;
use App\Models\Post;
use App\Services\PlatformPublisher;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class GeneratePostJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $postData;
protected $userId;
protected $platformSettings;
public function __construct(array $postData, int $userId, array $platformSettings)
{
$this->postData = $postData;
$this->userId = $userId;
$this->platformSettings = $platformSettings;
}
public function handle(PlatformPublisher $publisher)
{
$post = Post::create([
'user_id' => $this->userId,
'content' => $this->postData['content'],
'title' => $this->postData['title']
]);
$publishToLinkedIn = $this->postData['linkedin_auto_publish'] ?? $this->platformSettings['linkedin_default'] ?? false;
$publishToDevTo = $this->postData['devto_auto_publish'] ?? $this->platformSettings['devto_default'] ?? false;
if ($publishToLinkedIn) {
$publisher->publishToLinkedIn($post);
}
if ($publishToDevTo) {
$publisher->publishToDevTo($post);
}
}
}
In this example, $this->postData contains the form values, and $this->platformSettings contains the user's default preferences. The ?? operator is used to provide a fallback mechanism, ensuring that if a per-generation toggle is not set, the user's default preference is used instead.
Benefits
- Increased Flexibility: Users have fine-grained control over where their generated content is published.
- Improved User Experience: The "Connect" button directly links to the integration setup, simplifying the process of connecting to new platforms.
- Backward Compatibility: Existing users are not immediately affected, as the system falls back to their existing preferences.
Conclusion
By adding per-generation auto-publish toggles, we've provided our users with greater flexibility and control over their content distribution. This enhancement streamlines the publishing process and improves the overall user experience. When implementing similar features, consider providing clear visual cues for integration status and ensure backward compatibility to minimize disruption for existing users.