feat: add static data option for recipes

This commit is contained in:
Benjamin Nussbaum 2025-06-26 13:24:04 +02:00
parent 3568f60b9b
commit c7ba1f80af
2 changed files with 47 additions and 34 deletions

View file

@ -22,7 +22,7 @@ new class extends Component {
protected $rules = [
'name' => 'required|string|max:255',
'data_stale_minutes' => 'required|integer|min:1',
'data_strategy' => 'required|string|in:polling,webhook',
'data_strategy' => 'required|string|in:polling,webhook,static',
'polling_url' => 'required_if:data_strategy,polling|nullable|url',
'polling_verb' => 'required|string|in:get,post',
'polling_header' => 'nullable|string|max:255',
@ -53,14 +53,14 @@ new class extends Component {
'name' => $this->name,
'data_stale_minutes' => $this->data_stale_minutes,
'data_strategy' => $this->data_strategy,
'polling_url' => $this->polling_url,
'polling_url' => $this->polling_url ?? null,
'polling_verb' => $this->polling_verb,
'polling_header' => $this->polling_header,
]);
$this->reset(['name', 'data_stale_minutes', 'data_strategy', 'polling_url', 'polling_verb', 'polling_header']);
$this->refreshPlugins();
Flux::modal('add-plugin')->close();
}
@ -116,35 +116,37 @@ new class extends Component {
</div>
<div class="mb-4">
<flux:input label="Data is stale after minutes" wire:model="data_stale_minutes"
id="data_stale_minutes"
class="block mt-1 w-full" type="number" name="data_stale_minutes" autofocus/>
</div>
<div class="mb-4">
<flux:radio.group wire:model="data_strategy" label="Data Strategy" variant="segmented">
<flux:radio.group wire:model.live="data_strategy" label="Data Strategy" variant="segmented">
<flux:radio value="polling" label="Polling"/>
<flux:radio value="webhook" label="Webhook"/>
<flux:radio value="static" label="Static"/>
</flux:radio.group>
</div>
<div class="mb-4">
<flux:input label="Polling URL" wire:model="polling_url" id="polling_url"
placeholder="https://example.com/api"
class="block mt-1 w-full" type="text" name="polling_url" autofocus/>
</div>
@if($data_strategy === 'polling')
<div class="mb-4">
<flux:input label="Polling URL" wire:model="polling_url" id="polling_url"
placeholder="https://example.com/api"
class="block mt-1 w-full" type="text" name="polling_url" autofocus/>
</div>
<div class="mb-4">
<flux:radio.group wire:model="polling_verb" label="Polling Verb" variant="segmented">
<flux:radio value="get" label="GET"/>
<flux:radio value="post" label="POST"/>
</flux:radio.group>
</div>
<div class="mb-4">
<flux:radio.group wire:model="polling_verb" label="Polling Verb" variant="segmented">
<flux:radio value="get" label="GET"/>
<flux:radio value="post" label="POST"/>
</flux:radio.group>
</div>
<div class="mb-4">
<flux:input label="Polling Header" wire:model="polling_header" id="polling_header"
class="block mt-1 w-full" type="text" name="polling_header" autofocus/>
</div>
<div class="mb-4">
<flux:input label="Polling Header" wire:model="polling_header" id="polling_header"
class="block mt-1 w-full" type="text" name="polling_header" autofocus/>
</div>
<div class="mb-4">
<flux:input label="Data is stale after minutes" wire:model.live="data_stale_minutes"
id="data_stale_minutes"
class="block mt-1 w-full" type="number" name="data_stale_minutes" autofocus/>
</div>
@endif
<div class="flex">
<flux:spacer/>

View file

@ -77,10 +77,11 @@ new class extends Component {
protected array $rules = [
'name' => 'required|string|max:255',
'data_stale_minutes' => 'required|integer|min:1',
'data_strategy' => 'required|string|in:polling,webhook',
'data_strategy' => 'required|string|in:polling,webhook,static',
'polling_url' => 'required_if:data_strategy,polling|nullable|url',
'polling_verb' => 'required|string|in:get,post',
'polling_header' => 'nullable|string|max:255',
'data_payload' => 'required_if:data_strategy,static|nullable|json',
'blade_code' => 'nullable|string',
'checked_devices' => 'array',
'playlist_name' => 'required_if:selected_playlist,new|string|max:255',
@ -94,6 +95,12 @@ new class extends Component {
{
abort_unless(auth()->user()->plugins->contains($this->plugin), 403);
$validated = $this->validate();
// If static strategy is selected, parse and save the JSON data
if ($this->data_strategy === 'static' && isset($validated['data_payload'])) {
$validated['data_payload'] = json_decode($validated['data_payload'], true);
}
$this->plugin->update($validated);
}
@ -451,16 +458,11 @@ HTML;
name="name" autofocus/>
</div>
<div class="mb-4">
<flux:input label="Data is stale after minutes" wire:model="data_stale_minutes"
id="data_stale_minutes"
class="block mt-1 w-full" type="number" name="data_stale_minutes" autofocus/>
</div>
<div class="mb-4">
<flux:radio.group wire:model.live="data_strategy" label="Data Strategy" variant="segmented">
<flux:radio value="polling" label="Polling"/>
<flux:radio value="webhook" label="Webhook"/>
<flux:radio value="static" label="Static"/>
</flux:radio.group>
</div>
@ -495,7 +497,12 @@ HTML;
placeholder="Authorization: Bearer ey.*******&#10;Content-Type: application/json"
/>
</div>
@else
<div class="mb-4">
<flux:input label="Data is stale after minutes" wire:model="data_stale_minutes"
id="data_stale_minutes"
class="block mt-1 w-full" type="number" name="data_stale_minutes" autofocus/>
</div>
@elseif($data_strategy === 'webhook')
<div class="mb-4">
<flux:input
label="Webhook URL"
@ -510,6 +517,10 @@ HTML;
<p>Send JSON payload with key <code>merge_variables</code> to the webhook URL. The payload
will be merged with the plugin data.</p>
</div>
@elseif($data_strategy === 'static')
<div>
<p>Enter static JSON data in the Data Payload field.</p>
</div>
@endif
<div class="flex">
@ -523,7 +534,7 @@ HTML;
<flux:badge icon="clock" size="sm" variant="pill" class="ml-2">{{ $this->data_payload_updated_at?->diffForHumans() ?? 'Never' }}</flux:badge>
<flux:textarea wire:model="data_payload" id="data_payload"
class="block mt-1 w-full font-mono" type="text" name="data_payload"
readonly rows="24"/>
:readonly="$data_strategy !== 'static'" rows="24"/>
</div>
</div>
<flux:separator/>